In [12]:
from __future__ import print_function
import numpy as np
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM, Convolution1D, MaxPooling1D
from keras.layers import Dropout
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from six.moves import cPickle as pickle
from keras.utils.np_utils import to_categorical
from keras.callbacks import ModelCheckpoint, EarlyStopping, LambdaCallback
# fix random seed for reproducibility
seed=7
np.random.seed(seed)
import tensorflow as tf
tf.set_random_seed(seed)
NAME="LSTM-10.2"
split_valid_test=False
In [13]:
# Dane wejściowe
with open("X-sequences.pickle", 'rb') as f:
X = pickle.load(f)
with open("Y.pickle", 'rb') as f:
Y = pickle.load(f)
In [14]:
np.unique(Y)
Out[14]:
W tej wersji eksperymentu, Y zawiera root_service - 44 unikalne kategorie główne. Zamieńmy je na liczby z przedziału 0-43
In [15]:
root_services=np.sort(np.unique(Y))
# skonstruuj odwrtotny indeks kategorii głównych
services_idx={root_services[i]: i for i in range(len(root_services))}
In [16]:
# Zamień
Y=[services_idx[y] for y in Y]
In [17]:
Y=to_categorical(Y)
Y.shape
Out[17]:
In [18]:
top_words = 5000
classes=Y[0,].shape[0]
print(classes)
In [19]:
# max_length (98th percentile is 476), padd the rest
max_length=500
X=sequence.pad_sequences(X, maxlen=max_length)
X_train=X[0:][::2] # even X_test=X[1:][::2] # odds
Y_train=np.array(Y[0:][::2]) # even Y_test=np.array(Y[1:][::2]) # odds
if split_valid_test:
# Split "test" in half for validation and final testing
X_valid=X_test[:len(X_test)/2]
Y_valid=Y_test[:len(Y_test)/2]
X_test=X_test[len(X_test)/2:]
Y_test=Y_test[len(Y_test)/2:]
else: X_valid=X_test Y_valid=Y_test
In [20]:
# create the model
embedding_vecor_length = 100
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_length))
model.add(Dropout(0.2))
model.add(Conv1D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=16, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
# model.add(Convolution1D(nb_filter=1024, filter_length=3, border_mode='same', activation='relu'))
# model.add(MaxPooling1D(pool_length=2))
# model.add(Convolution1D(nb_filter=512, filter_length=3, border_mode='same', activation='relu'))
# model.add(MaxPooling1D(pool_length=2))
model.add(LSTM(100))
model.add(Dropout(0.3))
model.add(Dense(classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
# Callbacks
early_stop_cb = EarlyStopping(monitor='val_loss', patience=20, verbose=1)
checkpoit_cb = ModelCheckpoint(NAME+".h5", save_best_only=True)
# Print the batch number at the beginning of every batch.
batch_print_cb = LambdaCallback(on_batch_begin=lambda batch, logs: print (".",end=''),
on_epoch_end=lambda batch, logs: print (batch))
# Plot the loss after every epoch.
plot_loss_cb = LambdaCallback(on_epoch_end=lambda epoch, logs:
print (epoch, logs))
#plt.plot(np.arange(epoch), logs['loss']))
print("done")
In [21]:
history = model.fit(
X,#_train,
Y,#_train,
initial_epoch=200,
epochs=300,
batch_size=1024,
#validation_data=(X_valid,Y_valid),
validation_split=0.3,
callbacks=[early_stop_cb, checkpoit_cb, batch_print_cb, plot_loss_cb],
verbose=0
)
#history=model.fit(X_train, Y_train, validation_data=(X_test, Y_test), nb_epoch=3, batch_size=512)
In [22]:
import matplotlib.pyplot as plt
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='lower right')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper right')
# plt.title('model loss (log scale)')
# plt.yscale('log')
plt.show()
In [25]:
history2 = model.fit(
X,#_train,
Y,#_train,
initial_epoch=10000,
epochs=10050,
batch_size=1024,
#validation_data=(X_valid,Y_valid),
validation_split=0.3,
callbacks=[early_stop_cb, checkpoit_cb, batch_print_cb, plot_loss_cb],
verbose=0
)
In [15]:
score=model.evaluate(X_test,Y_test, verbose=0)
print("OOS %s: %.2f%%" % (model.metrics_names[1], score[1]*100))
print("OOS %s: %.2f" % (model.metrics_names[0], score[0]))
In [26]:
import matplotlib.pyplot as plt
# summarize history for accuracy
plt.plot(history2.history['acc'])
plt.plot(history2.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='lower right')
plt.show()
# summarize history for loss
plt.plot(history2.history['loss'])
plt.plot(history2.history['val_loss'])
plt.title('model loss (log scale)')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper right')
plt.yscale('log')
plt.show()
In [15]:
history3 = model.fit(
X,#_train,
Y,#_train,
initial_epoch=60,
epochs=90,
batch_size=1024,
#validation_data=(X_valid,Y_valid),
validation_split=0.3,
callbacks=[early_stop_cb, checkpoit_cb, batch_print_cb, plot_loss_cb],
verbose=0
)
In [16]:
import matplotlib.pyplot as plt
# summarize history for accuracy
plt.plot(history3.history['acc'])
plt.plot(history3.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='lower right')
plt.show()
# summarize history for loss
plt.plot(history3.history['loss'])
plt.plot(history3.history['val_loss'])
plt.title('model loss (log scale)')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper right')
plt.yscale('log')
plt.show()
In [ ]: