TensorFlow + Kerasを使ってみた9(畳み込み層の重みをC のヘッダに変換)
TensorFlow + Kerasを使ってみた10(バイアスをC のヘッダに変換)
Layer (type) Output Shape Param #
=================================================================
conv2d_22 (Conv2D) (None, 6, 52, 2) 52
_________________________________________________________________
activation_64 (Activation) (None, 6, 52, 2) 0
_________________________________________________________________
max_pooling2d_22 (MaxPooling (None, 3, 26, 2) 0
_________________________________________________________________
flatten_22 (Flatten) (None, 156) 0
_________________________________________________________________
dense_43 (Dense) (None, 100) 15700
_________________________________________________________________
activation_65 (Activation) (None, 100) 0
_________________________________________________________________
dense_44 (Dense) (None, 3) 303
_________________________________________________________________
activation_66 (Activation) (None, 3) 0
=================================================================
Total params: 16,055
Trainable params: 16,055
Non-trainable params: 0
conv_layer_bias = [-0.3386867 -0.5459513]
np.max(conv_layer_weight) = 0.6478661894798279
np.min(conv_layer_weight) = -0.44461962580680847
np.max(abs_conv_layer_weight) = 0.6478661894798279
np.min(abs_conv_layer_weight) = 0.01398547738790512
np.max(conv_layer_bias) = -0.3386867046356201
np.min(conv_layer_bias) = -0.5459513068199158
np.max(abs_conv_layer_bias) = 0.5459513068199158
np.min(abs_conv_layer_bias) = 0.3386867046356201
conv_output = (26550, 6, 52, 2)
np.std(conv_output) = 0.2930081784725189
np.max(conv_output) = 1.9703364372253418
np.min(conv_output) = -0.6579744815826416
np.max(abs_conv) = 1.9703364372253418
np.min(abs_conv) = 7.748603820800781e-07
np.max(dence_layer1_weight) = 0.6284171938896179
np.min(dence_layer1_weight) = -0.452088862657547
np.max(abs_dence_layer1_weight) = 0.6284171938896179
np.min(abs_dence_layer1_weight) = 2.8568319976329803e-07
np.max(dence_layer1_bias) = 0.11784756183624268
np.min(dence_layer1_bias) = -0.08143308758735657
np.max(abs_dence_layer1_bias) = 0.11784756183624268
np.min(abs_dence_layer1_bias) = 0.0
dence_layer1_output = (26550, 100)
np.std(dence_layer1_output) = 0.9401099681854248
np.max(dence_layer1_output) = 5.256106853485107
np.min(dence_layer1_output) = -5.39329195022583
np.max(abs_dence_layer1_output) = 5.39329195022583
np.min(abs_dence_layer1_output) = 8.740462362766266e-07
np.max(dence_layer2_weight) = 1.2727510929107666
np.min(dence_layer2_weight) = -1.23631751537323
np.max(abs_dence_layer2_weight) = 1.2727510929107666
np.min(abs_dence_layer2_weight) = 0.002743939170613885
np.max(dence_layer2_bias) = 0.009167053736746311
np.min(dence_layer2_bias) = -0.028219975531101227
np.max(abs_dence_layer2_bias) = 0.028219975531101227
np.min(abs_dence_layer2_bias) = 0.007371054962277412
dence_layer2_output = (26550, 3)
np.std(dence_layer2_output) = 5.037158012390137
np.max(dence_layer2_output) = 17.514923095703125
np.min(dence_layer2_output) = -16.942663192749023
np.max(abs_dence_layer2_output) = 17.514923095703125
np.min(abs_dence_layer2_output) = 0.00031781941652297974
(x_train, y_train), (x_test, y_test) = load_mnist(normalize=False, flatten=False)
print('x_train shape:', x_train.shape)
print('y_train shape:', y_train.shape)
# トレーニングデータとテストデータをシャッフル
trainzip = list(zip(y_train, x_train))
np.random.shuffle(trainzip)
y_train, x_train = zip(*trainzip)
x_train = np.asarray(x_train)
y_train = np.asarray(y_train)
testzip = list(zip(y_test, x_test))
np.random.shuffle(testzip)
y_test, x_test = zip(*testzip)
x_test = np.asarray(x_test)
y_test = np.asarray(y_test)
x_train shape: (34650, 1, 10, 56)
y_train shape: (34650,)
x_train shape: (34650, 10, 56, 1)
34650 train samples
26550 test samples
Train on 34650 samples, validate on 26550 samples
Epoch 1/8
34650/34650 [==============================] - 6s 170us/step - loss: 0.5659 - acc: 0.7551 - val_loss: 0.4968 - val_acc: 0.7847
Epoch 2/8
34650/34650 [==============================] - 5s 152us/step - loss: 0.2709 - acc: 0.8988 - val_loss: 0.2481 - val_acc: 0.9006
Epoch 3/8
34650/34650 [==============================] - 5s 152us/step - loss: 0.1726 - acc: 0.9386 - val_loss: 0.1631 - val_acc: 0.9456
Epoch 4/8
34650/34650 [==============================] - 5s 158us/step - loss: 0.1273 - acc: 0.9540 - val_loss: 0.2028 - val_acc: 0.9261
Epoch 5/8
34650/34650 [==============================] - 5s 158us/step - loss: 0.1025 - acc: 0.9616 - val_loss: 0.1651 - val_acc: 0.9376
Epoch 6/8
34650/34650 [==============================] - 6s 163us/step - loss: 0.0861 - acc: 0.9673 - val_loss: 0.1170 - val_acc: 0.9532
Epoch 7/8
34650/34650 [==============================] - 5s 154us/step - loss: 0.0760 - acc: 0.9705 - val_loss: 0.1007 - val_acc: 0.9618
Epoch 8/8
34650/34650 [==============================] - 5s 153us/step - loss: 0.0672 - acc: 0.9733 - val_loss: 0.0957 - val_acc: 0.9647
# My Mnist CNN (Convolution layerの特徴マップは5個)
# Conv2D - ReLU - MaxPooling - Dence - ReLU - Dence
# 2018/05/25 by marsee
# Keras / Tensorflowで始めるディープラーニング入門 https://qiita.com/yampy/items/706d44417c433e68db0d
# のPythonコードを再利用させて頂いている
import sys, os
sys.path.append(os.pardir) # 親ディレクトリのファイルをインポートするための設定
from dataset_curve.curve_dataset import load_mnist
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D, Activation
from keras import backend as K
import numpy as np
batch_size = 128
num_classes = 3
epochs = 8
img_rows, img_cols = 10, 56
(x_train, y_train), (x_test, y_test) = load_mnist(normalize=False, flatten=False)
print('x_train shape:', x_train.shape)
print('y_train shape:', y_train.shape)
# トレーニングデータとテストデータをシャッフル
trainzip = list(zip(y_train, x_train))
np.random.shuffle(trainzip)
y_train, x_train = zip(*trainzip)
x_train = np.asarray(x_train)
y_train = np.asarray(y_train)
testzip = list(zip(y_test, x_test))
np.random.shuffle(testzip)
y_test, x_test = zip(*testzip)
x_test = np.asarray(x_test)
y_test = np.asarray(y_test)
#Kerasのバックエンドで動くTensorFlowとTheanoでは入力チャンネルの順番が違うので場合分けして書いています
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
y_train = y_train.astype('int32')
y_test = y_test.astype('int32')
y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(2, kernel_size=(5, 5),
input_shape=input_shape))
model.add(Activation(activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(100))
model.add(Activation(activation='relu'))
model.add(Dense(num_classes))
model.add(Activation(activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,
verbose=1, validation_data=(x_test, y_test))
load_mnist() の引数で、normalize=False は下のPython コードで 0 ~ 1.0 に正規化しているので、0 ~ 255 の値とした。flatten=False は、(28, 28) の手書き数字画像が必要なので指定している。(x_train, y_train), (x_test, y_test) = load_mnist(normalize=False, flatten=False)
print('x_train shape:', x_train.shape)
print('x_train shape:', y_train.shape)
だったのだが、下の if 文の中の reshape で配列の形を変えている。最後が 1 次元なので、形を変えても問題無いようだ。x_train shape: (60000, 1, 28, 28)
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | 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 |