U E D R , A S I H C RSS

머신러닝스터디/2016/2016_06_18 (rev. 1.2)

머신러닝스터디/2016/2016_06_18

내용

  • 아래 코드에서 마지막 레이어의 아웃풋은 0~1 사이의 값이 나와야 하므로 relu가 아니라 sigmoid를 쓴다.

코드

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.datasets import mnist
from keras.layers.core import Reshape
from keras.utils.np_utils import to_categorical
import numpy as np

(X_train, y_train), (X_test, y_test) = mnist.load_data()

model = Sequential()
model.add(Reshape((28*28,), input_shape=(28,28)))
model.add(Dense(60000, input_dim=28*28, activation='relu'))

model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))

model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adagrad',
              metrics=['accuracy'])


model.fit(X_train, to_categorical(y_train, 10),
          nb_epoch=3,
          batch_size=200)

score = model.evaluate(X_test, to_categorical(y_test, 10), batch_size=10000)

print(score)

# output
print(model.predict(np.array([X_test[0]])))
print(y_test[0])
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:27
Processing time 0.0324 sec