Below I attached 4 pictures with error as a picture.
Generally, I'm training my neural network ( having a 2, 3, 1
architecture ) that consists of two input neurons in the input layer, 3 neurons in my hidden layer and 1 output neuron in my output layer.
So, I trained my network using back propagation and I am having small error ( which is specified in the picture ).
Can someone help me with that please.
Error: shapes (200,200) and (1,3) not aligned: 200 (dim 1) != 1 (dim 0)
import numpy as np
import random
# Generating training data set according to the function y=x^2+y^2
input1_train = np.random.uniform(low=-1, high=1, size=(200,))
input2_train = np.random.uniform(low=-1, high=1, size=(200,))
input1_sq_train= input1_train **2
input2_sq_train= input2_train **2
input_merge= np.column_stack((input1_train,input2_train))
# normalized input data
input_merge= input_merge / np.amax(input_merge, axis=0)
# output of the training data
y_output_train= input1_sq_train + input2_sq_train
# normalized output data
y_output_train= y_output_train / 100
# Generating test data set according to the function y=x^2+y^2
input1_test = np.random.uniform(low=-1, high=1, size=(100,))
input2_test = np.random.uniform(low=-1, high=1, size=(100,))
input1_sq_test= input1_test **2
input2_sq_test= input2_test **2
y_output_test= input1_sq_test + input2_sq_test
# Merging two inputs of testing data into an one matrix
input_merge1= np.column_stack((input1_test,input2_test))
# normalized input test data
input_merge1=input_merge1 / np.amax(input_merge1, axis=0)
# normalized output test data
y_output_test= y_output_test / 100
# Generating validation data set according to the function y=x^2+y^2
input1_validation = np.random.uniform(low=-1, high=1, size=(50,))
input2_validation = np.random.uniform(low=-1, high=1, size=(50,))
input1_sq_validation= input1_validation **2
input2_sq_validation= input2_validation **2
input_merge2= np.column_stack((input1_validation,input2_validation))
# normalized input validation data
input_merge2= input_merge2 / np.amax(input_merge2, axis=0)
y_output_validation= input1_sq_validation + input2_sq_validation
# normalized output validation data
y_output_validation= y_output_validation / 100
class Neural_Network(object):
def __init__(self):
# parameters
self.inputSize = 2
self.outputSize = 1
self.hiddenSize = 3
# weights
self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # (3x2)
# weight matrix from input to hidden layer
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # (3x1)
# weight matrix from hidden to output layer
def forward(self, input_merge):
# forward propagation through our network
self.z = np.dot(input_merge, self.W1) # dot product of X (input) and first set of 3x2 weights
self.z2 = self.sigmoid(self.z) # activation function
self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2)
# and second set of 3x1 weights
o = self.sigmoid(self.z3) # final activation function
return o
def costFunction(self, input_merge, y_output_train):
# Compute cost for given X,y, use weights already stored in class.
self.o = self.forward(input_merge)
J = 0.5*sum((y_output_train-self.yHat)**2)
return J
def costFunctionPrime(self, input_merge, y_output_train):
# Compute derivative with respect to W and W2 for a given X and y:
self.o = self.forward(input_merge)
delta3 = np.multiply(-(y_output_train-self.yHat),
self.sigmoidPrime(self.z3))
dJdW2 = np.dot(self.a2.T, delta3)
delta2 = np.dot(delta3, self.W2.T)*self.sigmoidPrime(self.z2)
dJdW1 = np.dot(input_merge.T, delta2)
return dJdW1, dJdW2
def sigmoid(self, s):
# activation function
return 1/(1+np.exp(-s))
def sigmoidPrime(self, s):
# derivative of sigmoid
return s * (1 - s)
def backward(self, input_merge, y_output_train, o):
# backward propgate through the network
self.o_error = y_output_train - o # error in output
self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to error
self.z2_error = self.o_delta.dot(self.W2.T) # z2 error: how much our hidden layer weights contributed to output error
self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
self.W1 += input_merge.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
self.W2 += self.z2.T.dot(self.o_delta) # adjusting second set (hidden --> output) weights
def train (self, input_merge, y_output_train):
o = self.forward(input_merge)
self.backward(input_merge, y_output_train, o)
NN = Neural_Network()
for i in range(1000): # trains the NN 1,000 times
# print ( "Actual Output for training data: \n" + str(y_output_train))
# print ("Predicted Output for training data: \n" + str(NN.forward(input_merge)))
print ( "Loss for training: \n"
+ str( np.mean( np.square( y_output_train
- NN.forward( input_merge )
)
)
)
) # mean sum squared loss
NN.train(input_merge, y_output_train)
# NN.test(input_merge1,y_output_test)
# NN.validation(input_merge2,y_output_validation)