Question: Fix the errors in the following python code so that it runs import numpy as np def sigmoid(x): return 1.0/(1.0 + np.exp(-x)) def sigmoid_prime(x): return

Fix the errors in the following python code so that it runs

import numpy as np

def sigmoid(x): return 1.0/(1.0 + np.exp(-x))

def sigmoid_prime(x): return sigmoid(x)*(1.0-sigmoid(x))

class XOR:

def __init__(self, layers, activation='sigmoid'): if activation == 'sigmoid': self.activation = sigmoid self.activation_prime = sigmoid_prime

# Set weights self.weights = [] for i in range(1, len(layers) - 1): r = 2*np.random.random((layers[i-1] + 1, layers[i] + 1)) -1 self.weights.append(r) # output layer - random((2+1, 1)) : 3 x 1 r = 2*np.random.random( (layers[i] + 1, layers[i+1])) - 1 self.weights.append(r)

def fit(self, X, y, learning_rate=0.01, epochs=100): # This is to add the bias unit to the input layer ones = np.atleast_2d(np.ones(X.shape[0])) X = np.concatenate((ones.T, X), axis=1) for u in range(epochs): i = np.random.randint(X.shape[0]) a = [X[i]]

for m in range(len(self.weights)): dot_value = np.dot(a[m], self.weights[m]) activation = self.activation(dot_value) a.append(activation) # output layer error = y[i] - a[-1] deltas = [error * self.activation_prime(a[-1])]

# we need to begin at the second to last layer # (a layer before the output layer) for m in range(len(a) - 2, 0, -1): deltas.append(deltas[-1].dot(self.weights[m].T)*self.activation_prime(a[m]))

deltas.reverse()

# backpropagation for i in range(len(self.weights)): layer = np.atleast_2d(a[i]) delta = np.atleast_2d(deltas[i]) self.weights[i] += learning_rate * layer.T.dot(delta)

if u % 10000 == 0: print 'epochs:', u

def pred_output(self, x): b = np.concatenate((np.ones(1).T, np.array(x)), axis=1) for l in range(0, len(self.weights)): b = self.activation(np.dot(b, self.weights[l])) return b

if __name__ == '__main__':

net = NeuralNetwork([2,2,1]) X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([0, 0.99, 0.99, 0]) net.fit(X, y) for e in X: print(e,net.pred_output(e))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!