File size: 1,161 Bytes
4fc359d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Dense, Embedding, Flatten, Dropout
from tensorflow.keras.models import Sequential
import numpy as np
import csv

dataset = "dataset.csv"
inp_len = 16

X = []
y = []

with open(dataset, 'r') as f:
    csv_reader = csv.reader(f)
    next(csv_reader)  # Skip the header row if it exists
    
    for row in csv_reader:
        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=256, input_length=inp_len))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(512, activation="tanh"))
model.add(Dropout(0.5))
model.add(Dense(256, activation="selu"))
model.add(Dense(256, activation="softplus"))
model.add(Dense(1, activation="softplus"))

model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy",])

model.fit(X, y, epochs=64, batch_size=4, workers=2, use_multiprocessing=True)

model.save("net.h5")