-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
66 lines (49 loc) · 1.55 KB
/
classifier.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.utils import np_utils
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.preprocessing import LabelEncoder
import numpy as np
import pandas as pd
from keras.models import Sequential
from .tobow import tobow
np.random.seed(7)
model_path = "classifier.json"
weights_path = "weights.h5"
encoder_path = "encoder.npy"
training_data = "data/training/training-data.csv"
training_label = "data/training/training-label.csv"
X_df = pd.read_csv(training_data, header=None)
X = X_df.values
Y_df = pd.read_csv(training_label, header=None)
Y = Y_df.values
dummy_x = []
for text in X:
dummy_x.append(np.array(tobow(text[0])[0]))
bow = np.array(dummy_x)
#encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
np.save(encoder_path, encoder.classes_)
encoded_Y = encoder.transform(Y)
#convert integers to one hot encoded variables
dummy_y = np_utils.to_categorical(encoded_Y)
def baseline_model():
model = Sequential()
model.add(Dense(5000, input_shape=(len(bow[0]), ), activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(len(dummy_y[0]), activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
model = baseline_model()
#train
model.fit(bow, dummy_y, epochs=30)
#save to json
model_json = model.to_json()
with open(model_path, "w") as json_file:
json_file.write(model_json)
model.save_weights(weights_path)
print("Model has been saved")