-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
131 lines (105 loc) · 4.06 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Import statements
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Get data from csv file (in same folder location)
train_set = pd.read_csv("trainset.csv")
dev_set = pd.read_csv("devset.csv")
'''
print(train_set.shape)
print(train_set.head())
print(dev_set.shape)
print(dev_set.head())
'''
# Split the datasets
X_train = train_set.Text
y_train = train_set.Sentiment
X_dev = dev_set.Text
y_dev = dev_set.Sentiment
# Print out basic information about the dataset
print(X_train.head())
print(y_train.head())
print(X_dev.head())
print(y_dev.head())
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
vocab_size = 5000
oov_token = "<OOV>"
max_len = 280
tokenizer = Tokenizer(num_words=vocab_size, oov_token=oov_token)
tokenizer.fit_on_texts(X_train)
word_index = tokenizer.word_index
train_seqs = tokenizer.texts_to_sequences(X_train)
dev_seqs = tokenizer.texts_to_sequences(X_dev)
train_padded = pad_sequences(train_seqs, maxlen=max_len, truncating="post", padding="post")
dev_padded = pad_sequences(dev_seqs, maxlen=max_len)
'''
# MODEL: default
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, 16, input_length=max_len),
tf.keras.layers.GlobalAveragePooling1D(),
tf.keras.layers.Dropout(0.3),
#tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])
'''
#'''
# MODEL: CNN
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, 16, input_length=max_len),
tf.keras.layers.SeparableConv1D(16, 5, activation='relu', bias_initializer='random_uniform', depthwise_initializer='random_uniform', padding='same'),
tf.keras.layers.GlobalMaxPooling1D(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
])
class ResetStatesCallback(tf.keras.callbacks.Callback):
def on_epoch_begin(self, epoch, logs):
self.model.reset_states()
reset_states = ResetStatesCallback()
model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(0.001), metrics=['accuracy'])
model.summary()
#'''
'''
# MODEL: BIDIRECTIONAL LSTM
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, 16, input_length=max_len),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(16, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(16)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(0.001), metrics=['accuracy'])
'''
early_stopping = tf.keras.callbacks.EarlyStopping(patience=50)
model_checkpoint = tf.keras.callbacks.ModelCheckpoint("maincheckpoint.h5", save_best_only=True)
epochs = 15
# history = model.fit(train_padded, y_train, epochs=epochs, validation_data=(dev_padded, y_dev))
history = model.fit(train_padded, epochs=epochs, validation_data=(dev_padded, y_dev), callbacks=[early_stopping, model_checkpoint, reset_states])
model = tf.keras.models.load_model("maincheckpoint.h5")
predictions = model.predict(X_dev)
print("accuracy: ", tf.metrics.binary_accuracy(y_dev, predictions))
plt.plot(history.history["accuracy"])
plt.plot(history.history["val_accuracy"])
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend(["accuracy", "val_accuracy"])
plt.show()
plt.plot(history.history["loss"])
plt.plot(history.history["val_loss"])
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend(["loss", "val_loss"])
plt.show()
def predict_sentence(model, sentence):
sample_seq = tokenizer.texts_to_sequences(sentence)
padded = pad_sequences(sample_seq, padding='post', maxlen=max_len)
classes = model.predict(padded)
for x in range(len(padded)):
print(sentence[x])
print(classes[x])
print("----------")
sentences = ["dap me up bro", "i hate you and ur mum", "this is why she left you", "thanks for the gold kind stranger"]
predict_sentence(model, sentences)