莫煩的RNN教學分類例子:手寫數據集mnist

"""
"""
To know more or get code samples, please visit my website:
https://morvanzhou.github.io/tutorials/
Or search: 莫煩Python
Thank you for supporting!
"""

# please note, all tutorial code are running under python3.5.
# If you use the version like python2.7, please modify the code accordingly

# 8 - RNN Classifier example

# to try tensorflow, un-comment following two lines
# import os
# os.environ['KERAS_BACKEND']='tensorflow'
import torch
import numpy as np
np.random.seed(1337)  # for reproducibility

from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import SimpleRNN, Activation, Dense
from keras.optimizers import Adam
import matplotlib.pyplot as plt

TIME_STEPS = 28     # same as the height of the image
INPUT_SIZE = 28     # same as the width of the image
BATCH_SIZE = 50
BATCH_INDEX = 0
OUTPUT_SIZE = 10
CELL_SIZE = 50
LR = 0.001
generation=8001
accu=[]
los=[]
x=[]

# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# X shape (60,000 28x28), y shape (10,000, )
# (X_train, y_train), (X_test, y_test) = mnist.load_data()


path='./mnist.npz'
buff = np.load(path)
X_train, y_train = buff['x_train'], buff['y_train']
X_test, y_test = buff['x_test'], buff['y_test']
buff.close()

# SimpleRNN

# data pre-processing
X_train = X_train.reshape(-1, 28, 28) / 255.      # normalize
X_test = X_test.reshape(-1, 28, 28) / 255.        # normalize
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)

# build RNN model
model = Sequential()

# RNN cell
model.add(SimpleRNN(
    # for batch_input_shape, if using tensorflow as the backend, we have to put None for the batch_size.
    # Otherwise, model.evaluate() will get error.
    unroll=True,
    batch_input_shape=(None, TIME_STEPS, INPUT_SIZE),       # Or: input_dim=INPUT_SIZE, input_length=TIME_STEPS,
    output_dim=CELL_SIZE,
    
))

# output layer
model.add(Dense(OUTPUT_SIZE))
model.add(Activation('softmax'))

# optimizer
adam = Adam(LR)
model.compile(optimizer=adam,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# training
for step in range(generation):
    # data shape = (batch_num, steps, inputs/outputs)
    X_batch = X_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :, :]
    Y_batch = y_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :]
    cost = model.train_on_batch(X_batch, Y_batch)
    BATCH_INDEX += BATCH_SIZE
    BATCH_INDEX = 0 if BATCH_INDEX >= X_train.shape[0] else BATCH_INDEX

    if step % 500 == 0:
        cost, accuracy = model.evaluate(X_test, y_test, batch_size=y_test.shape[0], verbose=False)
        print('{}th test cost:{}'.format(int(step/500), cost), 'test accuracy: ', accuracy)

        los=np.append(los,cost)
        accu=np.append(accu,accuracy)

print(los)
print(accu)
x=range(int(generation/500)+1)
x=np.array(x)
x=x+1
print(x)
# %matplotlib inline
plt.figure(1, figsize=(12, 6))
plt.subplot(121)
plt.plot(x, los, 'r',label='mnist predict loss')
plt.xlabel("generation")
plt.ylabel("loss value")
plt.legend(loc='best')

plt.subplot(122)
plt.plot(x,accu, 'black',label='mnist predict accuracy')
plt.xlabel("generation")
plt.ylabel("accuracy value")
plt.legend(loc='best')
plt.show()

結果:
在這裏插入圖片描述
由於此數據即對應是對應的.npz格式的,需要配套下載
網站被牆了:https://s3.amazonaws.com/img-datasets/mnist.npz
數據集如下:https://download.csdn.net/download/John_ashley/12515830
學習使用。侵權速刪

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章