-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter-based-lstm2.py
171 lines (129 loc) · 5.72 KB
/
character-based-lstm2.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# # Char-level Recurrent Neural Network
#
# Let's try and build an LSTM-based neural network that uses chars instead of words as its input.
# from https://www.kaggle.com/mamamot/character-based-lstm
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from collections import Counter
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import pickle
from sklearn.model_selection import train_test_split
from sklearn.metrics import log_loss, confusion_matrix
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers import LSTM, Dense, Bidirectional, BatchNormalization, Dropout
from keras import optimizers
from keras.utils import to_categorical
import os
# some parameters
BATCH_SIZE = 1024 # batch size for the network
EPOCH_NUMBER = 1 # number of epochs to train
THRESHOLD = 5 # symbols appearing fewer times will be replaced by a placeholder
train = pd.read_csv('sanction_train.csv', encoding='iso-8859-1')
train.index = train['Index']
x_train = train['Name']
y_train = train['Label']
# An important statistic is the average length of the comment:
x_train.apply(lambda x: len(x)).describe()
# Get counts of unique symbols in the training set:
unique_symbols = Counter()
for _, message in x_train.iteritems():
unique_symbols.update(message)
print("Unique symbols:", len(unique_symbols))
# Find symbols that appear fewer times than the threshold:
uncommon_symbols = list()
for symbol, count in unique_symbols.items():
if count < THRESHOLD:
uncommon_symbols.append(symbol)
print("Uncommon symbols:", len(uncommon_symbols))
# Replace them with a placeholder:
DUMMY = uncommon_symbols[0]
tr_table = str.maketrans("".join(uncommon_symbols), DUMMY * len(uncommon_symbols))
x_train = x_train.apply(lambda x: x.translate(tr_table))
# We will need the number of unique symbols further down when we will decide on the dimensionality of inputs.
num_unique_symbols = len(unique_symbols) - len(uncommon_symbols) + 1
tokenizer = Tokenizer(
char_level=True,
filters=None,
lower=False,
num_words=num_unique_symbols
)
tokenizer.fit_on_texts(x_train)
sequences = tokenizer.texts_to_sequences(x_train)
with open("tokenizer.pkl", "wb") as f:
pickle.dump(tokenizer, f)
# Pad the input: I use the 500 lenght, just a bit over the median length.
padded_sequences = pad_sequences(sequences, maxlen=500)
# I will take just a bit of the data as the validation set to see that the network converges:
#x_train, x_val, y_train, y_val = train_test_split(padded_sequences, y_train, stratify=y_train['Label'], test_size=0.25)
x_train = padded_sequences
x_val = padded_sequences
y_val = y_train
# So, let's define the model!
model = Sequential()
model.add(LSTM(150, input_shape=(500, num_unique_symbols), activation="tanh", return_sequences=True))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(LSTM(100, input_shape=(500, num_unique_symbols), activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(100, activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(100, activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(100, activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(100, activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(100, activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(100, activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(100, activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(50, activation="tanh"))
model.add(BatchNormalization())
#model.add(Dropout(0.4))
model.add(Dense(1, activation="sigmoid"))
x_val = to_categorical(np.array(x_val), num_classes=num_unique_symbols)
# Load model weights
model.load_weights('lstm-char2_weight_ep1.h5')
checkpoint = ModelCheckpoint(filepath=os.path.join("my_weights_best.h5"), monitor='val_loss', save_best_only=True, mode='auto')
# Let's track the performance using the custom function that will be used for the leaderboard:
sgd = optimizers.SGD(lr=0.01, momentum=0.9)
model.compile(optimizer=sgd, loss="binary_crossentropy")
def kaggle_loss(y_true, y_pred):
total_loss = 0
for i in range(y_true.shape[0]):
total_loss += log_loss(y_true, y_pred)
return total_loss / y_true.shape[0]
with open("res_2.txt", "w") as f:
for epoch in range(EPOCH_NUMBER):
print("Epoch", epoch)
for i in range(0, len(x_train), BATCH_SIZE):
batch = x_train[i:i+BATCH_SIZE]
batch = to_categorical(batch, num_classes=num_unique_symbols)
y_batch = y_train.iloc[i:i+BATCH_SIZE]
model.fit(batch, y_batch, batch_size=256, validation_data=(batch, y_batch), callbacks=[checkpoint])
y_pred = np.round(model.predict_proba(x_val), 0)
y_pred = y_pred.astype(np.int64)
res = kaggle_loss(y_val, y_pred)
print("Loss:", res)
f.write("{}: {}\n".format(epoch, res))
model.save("lstm-char2_ep{}.h5".format(epoch + 1))
model.save_weights("lstm-char2_weight_ep{}.h5".format(epoch + 1))
print('################### with best weights #####################')
model.load_weights('my_weights_best.h5')
y_pred = np.round(model.predict_proba(x_val), 0)
y_pred = y_pred.astype(np.int64)
res = kaggle_loss(y_val, y_pred)
cm = confusion_matrix(y_val, y_pred)
print('\nConfusion Matrix : \n', cm)