Created
May 30, 2018 01:48
CIFAR-10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.optimizers import Adam | |
from keras.datasets import cifar10 | |
from keras.utils.np_utils import to_categorical | |
import numpy as np | |
import time | |
import matplotlib.pyplot as plt | |
start_time = time.time() | |
# データの読み込み | |
(x_train, y_train), (x_test, y_test) = cifar10.load_data() | |
# 小数化 | |
x_train = x_train / 255.0 | |
x_test = x_test / 255.0 | |
# データ数 | |
m_train, m_test = x_train.shape[0], x_test.shape[0] | |
# ベクトル化 | |
x_train, x_test = x_train.reshape(m_train, -1), x_test.reshape(m_test, -1) | |
# ノルムで標準化 | |
x_train = x_train / np.linalg.norm(x_train, ord=2, axis=1, keepdims=True) | |
x_test = x_test / np.linalg.norm(x_test, ord=2, axis=1, keepdims=True) | |
# yをOneHotVector化 | |
y_train, y_test = to_categorical(y_train), to_categorical(y_test) | |
# モデル | |
print(x_train.shape) #(50000, 3072) | |
model = Sequential() | |
model.add(Dense(768, activation="relu", input_shape=x_train.shape[1:])) | |
model.add(Dense(192, activation="relu")) | |
model.add(Dense(10, activation="softmax")) | |
# コンパイル | |
model.compile(optimizer=Adam(), loss="categorical_crossentropy", metrics=["accuracy"]) | |
# フィット | |
history = model.fit(x_train, y_train, batch_size=64, epochs=15).history | |
# 経過時間 | |
print("Elapsed[s] : ", time.time() - start_time) | |
# テスト精度 | |
test_eval = model.evaluate(x_test, y_test) | |
print("train accuracy :", history["acc"][-1]) | |
print("test accuracy :", test_eval[1]) | |
# 訓練誤差のプロット | |
plt.plot(range(len(history["loss"])), history["loss"], marker=".") | |
plt.show() | |
#1.66GB | |
# epoch=30の場合 | |
#Elapsed[s] : 1366.77405834198 | |
#10000/10000 [==============================] - 2s 167us/step | |
#train accuracy : 0.68696 | |
#test accuracy : 0.5358 | |
# epoch=15の場合 | |
#Elapsed[s] : 697.2960453033447 | |
#10000/10000 [==============================] - 2s 153us/step | |
#train accuracy : 0.57604 | |
#test accuracy : 0.5231 | |
#_________________________________________________________________ | |
#Layer (type) Output Shape Param # | |
#================================================================= | |
#dense_1 (Dense) (None, 768) 2360064 | |
#_________________________________________________________________ | |
#dense_2 (Dense) (None, 192) 147648 | |
#_________________________________________________________________ | |
#dense_3 (Dense) (None, 10) 1930 | |
#================================================================= | |
#Total params: 2,509,642 | |
#Trainable params: 2,509,642 | |
#Non-trainable params: 0 | |
#_________________________________________________________________ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment