-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsarcasm_nn_code.py
113 lines (57 loc) · 2.33 KB
/
sarcasm_nn_code.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from artificial.Natural_lang_process.nlp_2_sarcasm import *
from keras.models import Sequential
from keras.layers import Embedding, GlobalAvgPool1D, Dense
import matplotlib.pyplot as plt
import numpy as np
# model constructor
def sarcastic_model():
# model
model = Sequential()
# adding model required layers (vocab lenght 10k, output_dim 128 , input lenght 152)
model.add(Embedding(input_dim=40000, output_dim=128, input_length=200))
model.add(GlobalAvgPool1D())
model.add(Dense(24, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['acc'])
return model
model_ = sarcastic_model()
def start_training(sarcasm_model):
# start_training function to train data sets
history = sarcasm_model.fit(training_padded, np.array(train_labels), epochs=30, validation_data=(testing_padded,
np.array(test_labels)),
verbose=1)
# saving model
sarcasm_model.save("model_weights/Sentiment_model.h5")
return history
# train and return history values to plotting
trained_history_value = start_training(model_)
def plotting(history_values):
# visualize the acc loss
# main acc loss values
acc = history_values.history["acc"]
loss = history_values.history["loss"]
# validation values
val_acc = history_values.history["val_acc"]
val_loss = history_values.history["val_loss"]
# epoch num for x label
epoch_num = range(1, 31)
"""PLOT 1"""
plt.subplot(1, 2, 1)
# accuracy
plt.plot(epoch_num, acc, label="Accuracy")
plt.savefig("plots/accuracy.png")
# loss
plt.subplot(1, 2, 2)
plt.plot(epoch_num, loss, label="Loss")
plt.savefig("plots/loss.png")
"""PLOT 2"""
# val_accuracy
plt.subplot(2, 2, 1)
plt.plot(epoch_num, val_acc, label="val_accuracy")
plt.savefig("plots/val_acc")
# val_loss
plt.subplot(2, 2, 2)
plt.plot(epoch_num, val_loss, label="val_loss")
plt.savefig("plots/val_loss")
# run plotting function with trained hist values
plotting(trained_history_value)