{"id":715,"date":"2022-11-06T22:18:40","date_gmt":"2022-11-06T22:18:40","guid":{"rendered":"https:\/\/gatesboltonanalytics.com\/?page_id=715"},"modified":"2022-11-06T22:20:50","modified_gmt":"2022-11-06T22:20:50","slug":"rnn-lstm-tensorflow-keras-image-classification","status":"publish","type":"page","link":"https:\/\/gatesboltonanalytics.com\/?page_id=715","title":{"rendered":"RNN-LSTM &#8211; Tensorflow\/Keras &#8211; MNIST"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport tensorflow as tf\nimport tensorflow.keras\nfrom tensorflow.keras.datasets import mnist\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import LSTM, Dense, Dropout, LSTM\nfrom tensorflow.keras.optimizers import Adam\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import layers\n\n\n\n\n#Importing the data\n(X_train, y_train),(X_test, y_test) = mnist.load_data()  # unpacks images to x_train\/x_test and labels to y_train\/y_test\n#Normalizing the data\nX_train = X_train.astype('float32') \/ 255.0\nX_test = X_test.astype('float32') \/ 255.0\n\nprint(X_train&#91;4])\nprint(y_train&#91;4])\nplt.imshow(X_train&#91;3]) \n\nprint(X_train.shape)\nprint(X_train.shape&#91;1:])\nprint(y_train.shape)\nprint(X_test.shape)\nprint(y_test.shape)\n\nmodel = tensorflow.keras.models.Sequential()\n\n#Adding the model layers\n# The number of units is the number of neurons connected to the \n#layer holding the concatenated vector of hidden state and input\nmodel.add(tensorflow.keras.layers.LSTM(128, input_shape=(X_train.shape&#91;1:]), return_sequences=True))\nmodel.add(tensorflow.keras.layers.Dropout(0.2))\nmodel.add(tensorflow.keras.layers.LSTM(128))\nmodel.add(tensorflow.keras.layers.Dense(64, activation='relu'))\nmodel.add(tensorflow.keras.layers.Dropout(0.2))\nmodel.add(tensorflow.keras.layers.Dense(10, activation='softmax'))\n\n#Compiling the model\nmodel.compile( loss='sparse_categorical_crossentropy', \n              optimizer = tensorflow.keras.optimizers.Adam(lr=0.001, decay=1e-6), metrics=&#91;'accuracy'] )\n\n#Fitting data to the model\nhistory = model.fit(X_train, y_train, epochs=3, validation_data=(X_test, y_test))\n\n# Evaluating the test loss and accuracy\ntest_loss, test_acc = model.evaluate(X_test, y_test)\nprint('Test Loss: {}'.format(test_loss))\nprint('Test Accuracy: {}'.format(test_acc))\n\ntrain_loss = history.history&#91;'loss']\nval_loss = history.history&#91;'val_loss']\ntrain_acc = history.history&#91;'accuracy']\nval_acc = history.history&#91;'val_accuracy']\n\n#visualizing the change in the validation loss\nplt.plot(val_loss) \nplt.title(\"Test Loss\",size=18)\nplt.xlabel(\"Number of iterations\") \nplt.ylabel(\"Test Loss\") \nplt.show() \n\n#visualizing the change in the validation loss\nplt.plot(val_acc) \nplt.title(\"Test Accuracy\",size=18)\nplt.xlabel(\"Number of iterations\") \nplt.ylabel(\"Test Accuracy\") \nplt.show() <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-715","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/gatesboltonanalytics.com\/index.php?rest_route=\/wp\/v2\/pages\/715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gatesboltonanalytics.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/gatesboltonanalytics.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/gatesboltonanalytics.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gatesboltonanalytics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=715"}],"version-history":[{"count":2,"href":"https:\/\/gatesboltonanalytics.com\/index.php?rest_route=\/wp\/v2\/pages\/715\/revisions"}],"predecessor-version":[{"id":718,"href":"https:\/\/gatesboltonanalytics.com\/index.php?rest_route=\/wp\/v2\/pages\/715\/revisions\/718"}],"wp:attachment":[{"href":"https:\/\/gatesboltonanalytics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}