Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

List the major stock market indexes and explain what they tell us.

Answered: 1 week ago

Question

4. What is Title VII? What does it state?

Answered: 1 week ago

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago