Deep Learning: Keras + MNIST
Data representations for neural networks
# -*- coding: utf-8 -*-
"""
@Date: 2018/9/28
@Author: dreamhomes
@Summary:
"""
from keras.utils import to_categorical
from keras import models
from keras import layers
import numpy as np
# (train_images, train_labels), (test_images, test_labels) = mnist.load_data()
path = "./mnist.npz"
f = np.load(path)
# N=60000
train_images, train_labels = f['x_train'], f['y_train']
print(len(train_labels))
#import matplotlib.pyplot as plt
# digit = train_images[0]
# plt.imshow(digit, cmap=plt.cm.binary)
# plt.show()
test_images, test_labels = f['x_test'], f['y_test']
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(512, activation='relu'))
network.add(layers.Dense(10, activation='softmax'))
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
train_images = train_images.reshape((60000, 28 * 28))
# print(train_images[1])
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
history = network.fit(train_images, train_labels, epochs=5, batch_size=128)
# print(history.history.keys())
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Deep Learning: Keras + Boston_housing텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.