|
from tensorflow.keras.preprocessing.sequence import pad_sequences |
|
from tensorflow.keras.layers import Dense, Embedding, Flatten, Dropout |
|
from tensorflow.keras.optimizers import Adam |
|
from tensorflow.keras.models import Sequential |
|
from tqdm import tqdm |
|
import numpy as np |
|
import csv |
|
|
|
dataset = "dataset.csv" |
|
inp_len = 32 |
|
|
|
X = [] |
|
y = [] |
|
|
|
with open(dataset, 'r') as f: |
|
csv_reader = csv.reader(f) |
|
for row in tqdm(csv_reader): |
|
if row == []: continue |
|
label = int(row[0]) |
|
text = row[1] |
|
text = [ord(char) for char in text] |
|
X.append(text) |
|
y.append(label) |
|
|
|
X = np.array(pad_sequences(X, maxlen=inp_len, padding='post')) |
|
y = np.array(y) |
|
|
|
model = Sequential() |
|
model.add(Embedding(input_dim=1500, output_dim=128, input_length=inp_len)) |
|
model.add(Flatten()) |
|
model.add(Dropout(0.2)) |
|
model.add(Dense(512, activation="tanh")) |
|
model.add(Dropout(0.5)) |
|
model.add(Dense(200, activation="selu")) |
|
model.add(Dense(128, activation="softplus")) |
|
model.add(Dense(1, activation="softplus")) |
|
|
|
model.compile(optimizer=Adam(learning_rate=0.00001), loss="mse", metrics=["accuracy",]) |
|
|
|
model.fit(X, y, epochs=2, batch_size=4, workers=4, use_multiprocessing=True) |
|
|
|
model.save("net.h5") |
|
|