Assignment Week6 AI4ICPS
Assignment Week6 AI4ICPS
Answer-1
I have placed all my code in the below public Google Colab link (Which is public):
https://colab.research.google.com/drive/1iotHTXYPicOJwaHMxQuxyLFAyOhb5yLq?usp=sharing
import numpy as np
import pylab as pl
import networkx as nx
Defining and visualising the graph - But with the new points as given in the assignment
edges = [(0, 5), (0, 1), (2, 3), (5, 4), (1, 2),
(1, 3), (9, 10), (2, 4), (2, 6), (6, 7),
(5, 9), (2, 9), (3, 8), (3, 10)]
G = nx.Graph()
G.add_edges_from(edges)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos)
pl.show()
Output:
FIGURE A.
Next piece of code will be defining the reward system for the bot - But again with the new "Goal" (Which is 7).
1. This basically means that whichever point's column part (point[1]) is 7, there we are assigning that
point (Lets say x,7 in point's column) with max reward (So that all points in my edge set, in the 7th
column are 100 (max reward)
2. And vice versa if a point's row part (point[0]) matches, then reverse of point (y,7) is marked with award
using M[point[::-1]]- So for those points also 7th column is marked with max reward
PS - For other points (Which are not matching goal in x or y, there we are making the value in M corresponding
to that point as 0) - Which is basically higher reward than -1 (Because we want traversal to happen on our
points)
goal = 7
MATRIX_SIZE = 11
M = np.matrix(np.ones(shape =(MATRIX_SIZE, MATRIX_SIZE)))
M *= -1
if point[0] == goal:
M[point[::-1]] = 100
else:
M[point[::-1]]= 0
# reverse of point
available_action = available_actions(initial_state)
print(available_action)
action = sample_next_action(available_action)
print(action)
# Next fn Checks the max index possible for the passed action
# Also assigns the Q[current state, action] which is movement from current state to possible
action where reward>0 to M (original matrix) value but with gamma factor
# Ultimately we are updating the Q-Matrix according to the path chosen
def update(current_state, action, gamma):
max_index = np.where(Q[action, ] == np.max(Q[action, ]))[1]
if max_index.shape[0] > 1:
max_index = int(np.random.choice(max_index, size = 1))
else:
max_index = int(max_index)
Now we will have the final section which is training and evaluating the bot using the Q-Matrix. Training is
simply finding out available actions, one random action, and update step for 1000 random initial states.
Also we will test, how to bot traverses to "7" which was our reward state.
scores = []
for i in range(1000):
current_state = np.random.randint(0, int(Q.shape[0]))
available_action = available_actions(current_state)
action = sample_next_action(available_action)
score = update(current_state, action, gamma)
scores.append(score)
print("Trained Q matrix:")
print(Q / np.max(Q)*100)
# Testing
current_state = 0
steps = [current_state]
while current_state != 7:
pl.plot(scores)
pl.xlabel('No of iterations')
pl.ylabel('Reward gained')
pl.show()
Output
Question-2. For the code given in this github repository
https://github.com/Sabyasachi123276/Deep-Learning-Tutorials/blob/main/Convolutional_Neural_Network_with
_Implementation_in_Python.ipynb
(A) Change the Epoch values from 10 to 50
(B) What are your observations? Is there an improvement in Accuracy
Answer-2
I have placed all my code in the below public Google Colab link (Which is public):
https://colab.research.google.com/drive/1aYzCuFiU4LC5xR4UqiKYZ0WAZTyKREQp?usp=sharing
Code for Answer 2, pasted from my Colab Notebook: Along with detailed explanation of code (Which I
feel is necessary since we were given the full code and we just had to understand)
Now we will load the MNIST dataset from keras library. MNIST DB (Modified National Institute of Standards
and Technology Database) is a large database of pixel values of handwritten digits mapped to their actual
values.
● Keras MNIST when loaded contains:
● Training data as "x_train": NumPy array of image data with 60K pixel values as Matrix of (60000, 28, 28).
RGB values range from 0 to 255 in a 28x28 pixel array for each of 60K images
● Actual Digit Values of x_train pixels in "y_train": NumPy array of digit labels (integers in range 0-9) with
shape (60000,)
● x_test, y_test (Just like x_train and y_train but with 10K values)
We will also reshape data (Pixel data) to add one more dimension because it is needed further for Keras
Library
(X_train,y_train) , (X_test,y_test)=mnist.load_data()
print(X_train[59999,0])
print(X_train.shape)
#reshaping data - Adding one more dimension because it is needed further in Keras library
X_test = X_test.reshape((X_test.shape[0],X_test.shape[1],X_test.shape[2],1))
print(X_train.shape)
print(X_test.shape)
print(y_train[59999])
Output
Now we will normalize the pixel values - When using the image as it is and passing through a Neural Network,
the computation of high numeric values may become more complex. To reduce this we can normalize the
values to range from 0 to 1. After normalizing, the numbers will be small and the computation becomes
easier and faster. As the pixel range is 0 to 255, so dividing all the values by 255 will convert it to range from 0
to 1.
X_train=X_train/255
X_test=X_test/255
model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)))
model.add(MaxPool2D(2,2))
model.add(Flatten())
model.add(Dense(100,activation='relu'))
● Finally we will add the output layer, which is the final layer in the neural network where desired
predictions are obtained, and then we compile the model.
● The compilation is the final step which actually creates a model.
● After compilation, we train the model using the X_train,y_train sets.
○ During training we have to specify "Epoch"
○ An epoch means training the neural network with all the training data for one cycle. In an
epoch, we use all of the data exactly once in a pass. A forward pass and a backward pass
together are counted as one pass.
○ We will take Epoch as 50 as specified in the assignment
model.add(Dense(10,activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(X_train,y_train,epochs=50)
Output
Finally we will evaluate our multi-layered sequential NN model on the test data
model.evaluate(X_test,y_test)
Output