In [1]:
# windows only hack for graphviz path
import os
for path in os.environ['PATH'].split(os.pathsep):
if path.endswith("Library\\bin"):
os.environ['PATH']+=os.pathsep+os.path.join(path, 'graphviz')
In [2]:
import keras
from keras.models import Sequential
from PIL import Image
import numpy as np
In [3]:
import gzip
import pickle
with gzip.open("../Week02/mnist.pkl.gz", 'rb') as f:
train_set, validation_set, test_set = pickle.load(f, encoding='latin1')
train_X, train_y = train_set
validation_X, validation_y = validation_set
test_X, test_y = test_set
train_Y = np.eye(10)[train_y]
test_Y = np.eye(10)[test_y]
validation_Y = np.eye(10)[validation_y]
# or
# from keras.datasets import mnist
# from keras.utils import np_utils
# (train_X, train_y), (test_X, test_y) = mnist.load_data()
# train_Y = np_utils.to_categorical(train_y, 10)
# test_Y = np_utils.to_categorical(test_y, 10)
In [4]:
from keras.layers import Dense, Activation
model = Sequential()
model.add(Dense(units=10, input_dim=784))
model.add(Activation('softmax'))
In [5]:
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
In [6]:
from IPython.display import SVG, display
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
Out[6]:
In [7]:
model.fit(train_X, train_Y, validation_data=(validation_X, validation_Y), batch_size=128, epochs=15)
Out[7]:
In [8]:
# 預測看看 test_X 前 20 筆
model.predict_classes(test_X[:20])
Out[8]:
In [9]:
# 對答案
test_y[:20]
Out[9]:
In [10]:
# 看看 test accuracy
model.evaluate(test_X, test_Y)
Out[10]:
我們之前的網路架構
試著架出這樣的網路
然後訓練看看
開頭幾行可以這樣寫
from keras.layers import Dense, Activation, Conv2D, MaxPool2D, Reshape
model = Sequential()
model.add(Reshape((28, 28, 1), input_shape=(784,) ))
model.add(Conv2D(filters=32, kernel_size=(3,3), padding='same', activation="relu"))
In [11]:
# 參考答案
#%load q_keras_cnn.py