changed 6 years ago
Linked with GitHub

ML week 3

Get Your Hands Dirty

pip install keras tensorflow matplotlib
pip install jupyter
import keras
import numpy as np
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], -1)

n_values = np.max(y_train) + 1
y_train = np.eye(n_values)[y_train]

from keras.models import Sequential
from keras.layers import Dense, Dropout

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=(28*28)))
model.add(Dense(16, activation='relu'))
model.add(Dense(10, activation='softmax'))

from keras.optimizers import RMSprop
model.compile(optimizer=RMSprop(lr=0.001),
             loss='categorical_crossentropy',
             metrics=['accuracy'])
             
history=model.fit(x_train, y_train, validation_split=0.05,
                 epochs=25, batch_size=64)
                 
y = model.predict(x_test.reshape(x_test.shape[0], -1))

import matplotlib.pyplot as plt
plt.imshow(x_test[0])
fig = plt.gcf()
fig.savefig('out.png')
plt.show()

$ python
or $ ipython
>>> exec(open('xxx.py').read())
>>> x_train.shape
>>> y_train.shape
1 -1    X1    b1   Z1
-2 1 乘  X2 + b2 = Z2

http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17_2.html

References

Hung-yi Lee
MNIST tutorial with Keras
Train your first neural network: basic classification
Keras Documentation
devdocs

Select a repo