728x90
반응형
In [120]:
import keras
print(keras.__version__)
from keras.datasets import mnist
In [121]:
(train_images, train_labels), (test_images,test_labels) = mnist.load_data()
In [10]:
import matplotlib.pyplot as plt
image = train_images[0].reshape(28,28)
plt.imshow(image,cmap=plt.cm.binary)
plt.show()
In [20]:
from keras import models
from keras import layers
network = models.Sequential()
network.add(layers.Dense(512,activation='relu',input_shape=(28*28,)))
network.add(layers.Dense(10,activation='softmax'))
In [21]:
network.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
In [84]:
train_images = train_images.reshape((60000,28*28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000,28*28))
test_images = test_images.astype('float32') / 255
In [123]:
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
In [28]:
network.fit(train_images,train_labels,epochs=10,batch_size=128)
Out[28]:
In [29]:
test_loss, test_acc = network.evaluate(test_images,test_labels)
In [33]:
print("Test Loss : {0} / Test Acc : {1}".format(test_loss,test_acc))
In [124]:
networkCNN = models.Sequential()
# 필터도 학습하면서 최적의 값을 찾아간다. 필터를 weight라고 보면 됨.
# strides default 값이 (1,1)이다.
networkCNN.add(layers.Conv2D(32,(3,3),activation='relu', input_shape=(28,28,1))) # (28,28,1)에서 1은 흑백 채널을 말함 / 필터가 32개 있는 것
networkCNN.add(layers.MaxPool2D((2,2)))
networkCNN.add(layers.Conv2D(64,(3,3),activation='relu'))
networkCNN.add(layers.MaxPool2D((2,2)))
networkCNN.add(layers.Conv2D(64,(3,3),activation='relu'))
In [125]:
# 지금까지 Conv net의 구조 출력
# Conv layer를 거치면 (input_size - Filter_size) / stride) + 1로 사이즈가 줄어듬
# 여기서는 (2,2)로 설정했기에 MaxPooling을 거치면 size가 반으로 줄어든다. / 소수점은 버림
networkCNN.summary()
# param = 3x3x28 = 288 + 32 (필터의 갯수)
In [126]:
# 평평하게 한 줄짜리로 만듬
networkCNN.add(layers.Flatten())
networkCNN.add(layers.Dense(64, activation='relu'))
networkCNN.add(layers.Dense(10, activation='softmax'))
In [127]:
networkCNN.summary()
# 마지막 softmax를 통해 나온 확률을 통해 레이블을 예측한다.
In [128]:
networkCNN.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
In [129]:
train_images = train_images.reshape((60000, 28,28,1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000,28,28,1))
test_images = test_images.astype('float32') / 255
In [ ]:
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
In [130]:
networkCNN.fit(train_images,train_labels,epochs=5,batch_size=64)
Out[130]:
In [131]:
test_loss, test_acc = networkCNN.evaluate(test_images,test_labels)
In [132]:
print("Test Loss : {0} / Test Acc : {1}".format(test_loss,test_acc))
728x90
반응형