-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFitSentModel.py
More file actions
156 lines (81 loc) · 2.61 KB
/
FitSentModel.py
File metadata and controls
156 lines (81 loc) · 2.61 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import os
import re
import pickle
import pandas as pd
import numpy as np
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM, Dropout, Embedding, SpatialDropout1D, TimeDistributed
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
# In[3]:
N_CLASSES = 2 # 0 (negative), 4 (positive)
MAX_FEATURES = 3000
# In[4]:
clean_pattern = re.compile('[^a-zA-z0-9\s]')
# In[5]:
df = pd.read_csv('trainingandtestdata/training.1600000.processed.noemoticon.csv',
header=None,
encoding='latin-1')
# In[6]:
df.columns = ['Polarity', 'ID', 'Date', 'Q', 'User', 'Text']
df = df[['Polarity', 'Text']]
# In[7]:
df['Text'] = df.Text.str.replace(clean_pattern, '') .str.replace('rt', ' ')
# In case neutral tweets are there,
# they don't exist in the current file anyway
df = df[df.Polarity != 2]
df.loc[df.Polarity == 4, 'Polarity'] = 1
# In[8]:
df = shuffle(df)
# In[9]:
try:
with open('tokenizer.pkl', 'rb') as f:
tokenizer = pickle.load(f)
except FileNotFoundError:
print('fitting tokenizer')
tokenizer = Tokenizer(num_words=MAX_FEATURES, split=' ')
tokenizer.fit_on_texts(df.Text.values)
# In[10]:
X = pad_sequences(tokenizer.texts_to_sequences(df.Text.values))
Y = to_categorical(df.Polarity.values, num_classes=2)
# In[11]:
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.25)
# In[12]:
#with open('tokenizer.pkl', 'wb') as f:
# pickle.dump(tokenizer, f, protocol=pickle.HIGHEST_PROTOCOL)
# ##### Specify Model
# In[13]:
HIDDEN_SZ = 128
EMBEDDING_SZ = 256
XLABELS = X.shape[1]
# In[14]:
model = Sequential()
model.add(Embedding(MAX_FEATURES, EMBEDDING_SZ, input_length=XLABELS))
model.add(SpatialDropout1D(rate=0.4))
model.add(LSTM(HIDDEN_SZ, return_sequences=True))
model.add(LSTM(HIDDEN_SZ, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(N_CLASSES, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
# ##### Fit Model
# In[15]:
BATCH_SIZE = 32
N_EPOCHS = 20
# In[ ]:
model.fit(x=Xtrain,
y=Ytrain,
validation_split=0.1,
batch_size=BATCH_SIZE,
verbose=1,
epochs=N_EPOCHS)
# ###### Serialize Model and Dictionary
# In[ ]:
model.save('sentmodel.h5')
# In[ ]:
#model.evaluate(x=Xtrain)