pixel view of hand written digit,
training data shape (60000, 28, 28)
60000 images 28 x 28 pixels each
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
import os
import numpy as np
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
"""
np.set_printoptions(linewidth=160)
for x in x_train[2]:
print(x)
print(y_train[2])
print(x_train.shape)
"""
x_train = x_train/255.0
x_test = x_test/255.0
#print(x_train[0].shape)
# run on cpu
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
#train on GPU
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
#print("Num GPUs Available: ", len(pysical_devices))
tf.config.experimental.set_memory_growth(pysical_devices[0], True)
model = Sequential()
model.add(LSTM(128, input_shape=(x_train.shape[1:]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128))
model.add(Dropout(0.1))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
model.summary()
opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)
model.compile(
loss='sparse_categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'],
)
model.fit(x_train,
y_train,
epochs=3,
validation_data=(x_test, y_test))
------------------------------------
#logs
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None, 28, 128) 80384
_________________________________________________________________
dropout (Dropout) (None, 28, 128) 0
_________________________________________________________________
lstm_1 (LSTM) (None, 128) 131584
_________________________________________________________________
dropout_1 (Dropout) (None, 128) 0
_________________________________________________________________
dense (Dense) (None, 32) 4128
_________________________________________________________________
dropout_2 (Dropout) (None, 32) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 330
=================================================================
Total params: 216,426
Trainable params: 216,426
Non-trainable params: 0
1875/1875 [==============================] - 87s 46ms/step - loss: 1.0635 - accuracy: 0.6280 - val_loss: 0.1618 - val_accuracy: 0.9562
Epoch 2/3
1875/1875 [==============================] - 85s 46ms/step - loss: 0.1688 - accuracy: 0.9536 - val_loss: 0.0958 - val_accuracy: 0.9709
Epoch 3/3
1875/1875 [==============================] - 87s 46ms/step - loss: 0.1132 - accuracy: 0.9702 - val_loss: 0.0888 - val_accuracy: 0.9743
reference:
cannot import name 'CuDNNLSTM'
No comments:
Post a Comment