Keras使用全神经网络增加MNIST手写数字测试准确性

Keras使用全神经网络(dense)将testaccrucy测试准确性多次调试后到达98.5%。
1.原代码
import tensorflow as tf
import tensorflow.contrib.keras as keras
from PIL import Image

import numpy as np
import matplotlib.pyplot as plt

# MNIST handwritten, 60k 28*28 grayscale images of the 10 digits,
# along with a test set of 10k images
# http://yann.lecun.com/exdb/mnist/

# load data
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
print('Training set: {} and Training Targets: {}'.format(x_train.shape, y_train.shape))
print('Test set: {} and test targets: {}'.format(x_test.shape, y_test.shape))
print('First training data: {}. \n Its size is: {}'.format(x_train[0], x_train[0].shape))

#show first 16 images
for i in range(16):
    plt.subplot(4, 4, i+1)
    plt.imshow(x_train[i], cmap = 'Greys_r')
plt.show()

# set seeds
np.random.seed(9987)
tf.set_random_seed(9987)

#generate one-hot labels
y_train_onehot = keras.utils.to_categorical(y_train)
print('First 10 labels: ', y_train[:10])
print('First 10 one-hot labels: ', y_train_onehot[:10])

# preprocessing data
#1 reshape images to be row vectors
x_train_1 = np.reshape(x_train, [x_train.shape[0], x_train.shape[1] * x_train.shape[2]])
x_test_1 = np.reshape(x_test, [x_test.shape[0], x_test.shape[1] * x_test.shape[2]])
plt.imshow(np.reshape(x_train_1[0], [28, 28]), cmap = 'Greys_r')
plt.show()

#implement a feedforward NN: 2 hidden layers each have 50 hidden unites with tanh activation,
# and one output layer with 10 units for the 10 classes
model = keras.models.Sequential()
model.add(keras.layers.Dense(
    units=50,
    input_dim=x_train_1.shape[1],
    kernel_initializer='glorot_uniform',
    bias_initializer='zeros',
    activation='tanh'))
model.add(keras.layers.Dense(
    units=50,
    input_dim=50,
    kernel_initializer='glorot_uniform',
    bias_initializer='zeros',
    activation='tanh'))

model.add(keras.layers.Dense(
    units=y_train_onehot.shape[1],
    input_dim=50,
    kernel_initializer='glorot_uniform',
    bias_initializer='zeros',
    activation='softmax'))
sgd_optimizer = keras.optimizers.SGD(lr=0.001, decay=1e-7, momentum
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值