Manul, da
commited on
Commit
•
57a8195
1
Parent(s):
7012f5d
Create net.py
Browse files
net.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
2 |
+
from tensorflow.keras.layers import Dense, Embedding, Flatten, Dropout
|
3 |
+
from tensorflow.keras.optimizers import Adam
|
4 |
+
from tensorflow.keras.models import Sequential
|
5 |
+
from tqdm import tqdm
|
6 |
+
import numpy as np
|
7 |
+
import csv
|
8 |
+
|
9 |
+
dataset = "dataset.csv"
|
10 |
+
inp_len = 32
|
11 |
+
|
12 |
+
X = []
|
13 |
+
y = []
|
14 |
+
|
15 |
+
with open(dataset, 'r') as f:
|
16 |
+
csv_reader = csv.reader(f)
|
17 |
+
for row in tqdm(csv_reader):
|
18 |
+
if row == []: continue
|
19 |
+
label = int(row[0])
|
20 |
+
text = row[1]
|
21 |
+
text = [ord(char) for char in text]
|
22 |
+
X.append(text)
|
23 |
+
y.append(label)
|
24 |
+
|
25 |
+
X = np.array(pad_sequences(X, maxlen=inp_len, padding='post'))
|
26 |
+
y = np.array(y)
|
27 |
+
|
28 |
+
model = Sequential()
|
29 |
+
model.add(Embedding(input_dim=1500, output_dim=128, input_length=inp_len))
|
30 |
+
model.add(Flatten())
|
31 |
+
model.add(Dropout(0.2))
|
32 |
+
model.add(Dense(512, activation="tanh"))
|
33 |
+
model.add(Dropout(0.5))
|
34 |
+
model.add(Dense(200, activation="selu"))
|
35 |
+
model.add(Dense(128, activation="softplus"))
|
36 |
+
model.add(Dense(1, activation="softplus"))
|
37 |
+
|
38 |
+
model.compile(optimizer=Adam(learning_rate=0.00001), loss="mse", metrics=["accuracy",])
|
39 |
+
|
40 |
+
model.fit(X, y, epochs=2, batch_size=4, workers=4, use_multiprocessing=True)
|
41 |
+
|
42 |
+
model.save("net.h5")
|