In [1]:
from __future__ import print_function
import numpy as np
from keras.datasets import imdb
from keras.models import Sequential, Model
from keras.layers import Dense, Input
from keras.layers import LSTM, Convolution1D, MaxPooling1D, GlobalAveragePooling1D, concatenate, Lambda, BatchNormalization
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
import matplotlib.pyplot as plt
# fix random seed for reproducibility
seed=7
np.random.seed(seed)
import tensorflow as tf
tf.set_random_seed(seed)
NAME="MODEL-11-conv-1.0"
split_valid_test=False
# Read data
with open("Atmosfera-Incidents-2017.pickle", 'rb') as f:
incidents = pickle.load(f)
# Skonwertuj root_service do intów i zapisz
Y=[int(i) for i in incidents[1:,3]]
with open("Y.pickle", 'wb') as f:
pickle.dump(Y, f, pickle.HIGHEST_PROTOCOL)
In [2]:
# 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 [3]:
# Zostaw tylko poniższe kategorie, pozostale zmień na -1
lista = [2183,
#325,
37, 859, 2655, 606, 412, 2729, 1683, 1305]
# Y=[y if y in lista else -1 for y in Y]
mask = [y in lista for y in Y]
import itertools
X = np.array(list(itertools.compress(X, mask)))
Y = np.array(list(itertools.compress(Y, mask)))
In [4]:
np.unique(Y)
Out[4]:
W tej wersji eksperymentu, Y zawiera root_service - 44 unikalne kategorie główne. Zamieńmy je na liczby z przedziału 0-43
In [5]:
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 [6]:
# Zamień
Y=[services_idx[y] for y in Y]
In [7]:
Y=to_categorical(Y)
Y.shape
Out[7]:
In [8]:
top_words = 5000
classes=Y[0,].shape[0]
print(classes)
In [9]:
# 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 [10]:
# create the model
embedding_vecor_length = 60
_input = Input(shape=(max_length,), name='input')
embedding=Embedding(top_words, embedding_vecor_length, input_length=max_length)(_input)
conv1 = Conv1D(filters=128, kernel_size=1, padding='same', activation='relu')
conv2 = Conv1D(filters=128, kernel_size=2, padding='same', activation='relu')
conv3 = Conv1D(filters=128, kernel_size=3, padding='same', activation='relu')
conv4 = Conv1D(filters=128, kernel_size=4, padding='same', activation='relu')
conv5 = Conv1D(filters=32, kernel_size=5, padding='same', activation='relu')
conv6 = Conv1D(filters=32, kernel_size=6, padding='same', activation='relu')
conv1 = conv1(embedding)
glob1 = GlobalAveragePooling1D()(conv1)
conv2 = conv2(embedding)
glob2 = GlobalAveragePooling1D()(conv2)
conv3 = conv3(embedding)
glob3 = GlobalAveragePooling1D()(conv3)
conv4 = conv4(embedding)
glob4 = GlobalAveragePooling1D()(conv4)
conv5 = conv5(embedding)
glob5 = GlobalAveragePooling1D()(conv5)
conv6 = conv6(embedding)
glob6 = GlobalAveragePooling1D()(conv6)
merge = concatenate([glob1, glob2, glob3, glob4, glob5, glob6])
x = Dropout(0.2)(merge)
x = BatchNormalization()(x)
x = Dense(300, activation='relu')(x)
x = Dropout(0.2)(x)
x = BatchNormalization()(x)
pred = Dense(classes, activation='softmax')(x)
model = Model(inputs=[_input], outputs=pred)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])#, decay=0.0000001)
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 [11]:
history = model.fit(
X,#_train,
Y,#_train,
# initial_epoch=1200,
epochs=1500,
batch_size=2048,
#validation_data=(X_valid,Y_valid),
validation_split=0.25,
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 [12]:
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 [60]:
history2 = model.fit(
X,#_train,
Y,#_train,
initial_epoch=10000,
epochs=10010,
batch_size=1024,
#validation_data=(X_valid,Y_valid),
validation_split=0.1,
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 [50]:
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 [ ]: