forked from regevazran/twitter_classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_network.py
36 lines (29 loc) · 1.37 KB
/
neural_network.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
from defines import *
def create_network(X_train_shape):
input_dim = X_train_shape # Number of features
first_layer_out_shape = int(2*X_train_shape/3)
second_layer_out_shape = int(X_train_shape / 3)
model = Sequential()
model.add(layers.Dense(first_layer_out_shape, input_dim=input_dim, activation='relu'))
model.add(layers.Dense(second_layer_out_shape, activation='sigmoid'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
model.summary()
checkpoint_path = "neural_networks/cp.ckpt"
# Create a callback that saves the model's weights
cp_callback = keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
return model, cp_callback
def train_net(model, X_train, Y_train, cp_callback,new_fit = 0):
if new_fit:
model.fit(X_train, Y_train, epochs = 10,batch_size = 10, callbacks=[cp_callback])
else:
model.load_weights("neural_networks/cp.ckpt")
return model
def evaluate_net(model,X_test, Y_test):
_, accuracy = model.evaluate(X_test, Y_test)
print("results of classification by neural network with word to vec:")
print('Accuracy: %.2f' % (accuracy * 100))