Dear all, Here is the instruction for Project 5 assignment: Consider in CANVAS: Module 13: Keras DNN with Keras: 1. Detailed Version: With detailed explanation [Jupyter Notebook] or [Accessibility...

1 answer below »
I just need the code. Python, Jupyter Notebook


Dear all, Here is the instruction for Project 5 assignment: Consider in CANVAS: Module 13: Keras DNN with Keras: 1. Detailed Version: With detailed explanation [Jupyter Notebook] or [Accessibility score: High Click to improve HTML]   2. Simplified Version: With codes only [Jupyter Notebook] or [Accessibility score: High Click to improve HTML] Consider one or more TIPS from below to construct five different DNN models for MNIST: 1) Use different # of hidden layers; 2) Use different activation functions; 3) Use different # of neurons; 4) Use different batch_size; 5) use different # of epochs. In your project report, please consider some of the following tables/figures to summarize your models’ performances. Some tips to build an efficient models for MNIST dataset: #### The precautions when actually calling toolkit-Keras to train the model are as follows: ##### Build a neural network architecture 1. batch_size=100, epochs=20 is appropriate. If batch_size is too large, the loss curve will be too smooth and stuck at the local minima, saddle point or plateau. If the batch_size is too small, it will cause too many updates, too much computation, and slow speed. But can bring a certain degree of accuracy improvement. 2. Do not have too many hidden layers, otherwise vanishing gradient may occur. In general, two to three layers are appropriate. 3. If there are too many layers, do not use activation functions that reduce input effects such as sigmoid, and should choose approximately linear activation functions such as ReLU and Maxout (the two should also be selected if the number of layers is small) 4. The number of neurons contained in each hidden layer is five or six hundred. 5. For classification problems, the loss function must use cross entropy (categorical_crossentropy) instead of mean square error (mse) 6. The optimizer generally chooses Adam, which combines RMSProp and Momentum, and also considers the past gradient, the current gradient, and the last momentum. 7. If the accuracy rate on the testing data is very low, and the accuracy rate on the training data is relatively high, you can consider using dropout. The way to use Keras is to add a model.add(Dropout(0.5)) after each hidden layer. The 0.5 parameter is set by yourself; note that after adding dropout, the accuracy on the training set will decrease, but the accuracy on the testing set will increase, which is normal. 8. If the input is the pixel of the picture, pay attention to normalizing the gray value, that is, divide by 255, so that it is between 0 and 1. 9. The final output is best to output the accuracy rates on the training set and the testing set at the same time, so that the right decision can be made. ##### For performance on training data The first important point of training a neural network is that **we must first improve the performance on training data** 1. Activation function uses ReLU or Maxout 2. Use Adam for Adaptive learning rate 3. If the above two fail to improve the accuracy of the training set, it means that your network has no ability to fit training data. Try to change the network structure, including the number of layers and the number of neurons in each layer. ##### For performance on test data: 1. If the performance on the training set is relatively poor, no matter how the test set performs, go back and improve the performance on the training data. 2. If the performance on the training set is better, but the performance on the test set is relatively poor, it means that overfitting has occurred and the dropout method may be adopted for improvement. For example, we can consider the following model structure: # define network structure model = Sequential() model.add(Dense(input_dim=28 * 28, units=500, activation='relu')) # model.add(Dropout(0.5)) model.add(Dense(units=500, activation='relu')) # model.add(Dropout(0.5)) model.add(Dense(units=10, activation='softmax')) # set configurations model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
Answered Same DayMay 05, 2021

Answer To: Dear all, Here is the instruction for Project 5 assignment: Consider in CANVAS: Module 13: Keras DNN...

Sandeep Kumar answered on May 06 2021
146 Votes
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import keras\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Dropout, Flatten, Activation \n",
"from keras.layers import Conv2D, MaxPooling2D\n",
"from keras.optimizers import SGD, Adam\n",
"from keras.utils import np_utils\n",
"from keras.datasets import mnist"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"x_train shape: (60000, 784)\n60000 train samples\n10000 test samples\n"
]
}
],
"source": [
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
"x_train = x_train.reshape(x_train.shape[0], 28*28)\n",
"x_test = x_test.reshape(x_test.shape[0], 28*28)\n",
"\n",
"x_train = x_train.astype('float32')\n",
"x_test = x_test.astype('float32')\n",
"x_train /= 255 # normalize our data values to the range [0, 1]\n",
"x_test /= 255 #normalize our data values to the range [0, 1]\n",
"print('x_train shape:', x_train.shape)\n",
"print(x_train.shape[0], 'train samples')\n",
"print(x_test.shape[0], 'test samples')\n",
"\n",
"y_train = keras.utils.to_categorical(y_train, 10)\n",
"y_test = keras.utils.to_categorical(y_test, 10)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Model: \"sequential\"\n_________________________________________________________________\nLayer (type) Output Shape Param # \n=================================================================\ndense (Dense) (None, 500) 392500 \n_________________________________________________________________\ndense_1 (Dense) (None, 600) 300600 \n_________________________________________________________________\ndense_2 (Dense) (None, 600) 360600 \n_________________________________________________________________\ndense_3 (Dense) (None, 10) 6010 \n=================================================================\nTotal params: 1,059,710\nTrainable params: 1,059,710\nNon-trainable params: 0\n_________________________________________________________________\n"
]
}
],
"source": [
"model = Sequential()\n",
"model.add(Dense(input_dim=28 * 28, units=500, activation='sigmoid'))\n",
"model.add(Dense(units=600, activation='sigmoid'))\n",
"model.add(Dense(units=600, activation='sigmoid'))\n",
"model.add(Dense(units=10, activation='relu'))\n",
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/20\n",
"270/270 [==============================] - 6s 21ms/step - loss: 7.7299 - accuracy: 0.1188 - val_loss: 7.3150 - val_accuracy: 0.3998\n",
"Epoch 2/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.2602 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 3/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.1006 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 4/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0989 - val_loss: nan - v
al_accuracy: 0.0978\n",
"Epoch 5/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0981 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 6/20\n",
"270/270 [==============================] - 4s 14ms/step - loss: nan - accuracy: 0.0981 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 7/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0970 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 8/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0989 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 9/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0987 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 10/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0987 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 11/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0997 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 12/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0994 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 13/20\n",
"270/270 [==============================] - 5s 17ms/step - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 14/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.1004 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 15/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.1004 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 16/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0971 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 17/20\n",
"270/270 [==============================] - 4s 15ms/step - loss: nan - accuracy: 0.0998 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 18/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0992 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 19/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0981 - val_loss: nan - val_accuracy: 0.0978\n",
"Epoch 20/20\n",
"270/270 [==============================] - 4s 16ms/step - loss: nan - accuracy: 0.0984 - val_loss: nan - val_accuracy: 0.0978\n"
]
}
],
"source": [
"model.compile(loss='categorical_crossentropy',\n",
" optimizer='adam',\n",
" metrics=['accuracy'])\n",
"\n",
"history = model.fit(x_train, y_train, validation_split=0.1, batch_size = 200, epochs = 20, verbose = 1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Total loss on Testing Set: nan\nAccuracy of Testing Set: 0.09799999743700027\n"
]
}
],
"source": [
"score = model.evaluate(x_test,y_test, verbose = 0)\n",
"print('Total loss on Testing Set:',score[0])\n",
"print('Accuracy of Testing Set:',score[1])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "
",
"image/svg+xml": "\n\n\n\n \n\n\n\n2021-05-06T20:01:26.376127\nimage/svg+xml\n\n\nMatplotlib v3.3.4, https://matplotlib.org/\n\n\n\n\n \n \n\n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n\n\n\n \n\n",
"image/png":...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here