Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Fri Jan 12 22:27:44 2024
- @author: yiwen
- """
- import numpy as np
- import matplotlib.pyplot as plt
- def tanh(x):
- return (1.0-np.exp(-2*x))/(1.0+np.exp(-2*x))
- def tanh_derivative(x):
- return (1+tanh(x)) * (1-tanh(x))
- class Neural_Network(object):
- def __init__(self, net_arch):
- self.activity = tanh;
- self.activity_derivative = tanh_derivative;
- self.layers = len(net_arch)
- self.steps_per_epoch = 1000
- self.arch = net_arch
- self.weights = []
- for layer in range(self.layers-1):
- w = 2*np.random.rand(net_arch[layer]+1,
- net_arch[layer+1])-1
- self.weights.append(w)
- def predict(self, x):
- val = np.concatenate((np.ones(1), np.array(x)))
- for i in range(0, len(self.weights)):
- val = self.activity(np.dot(val, self.weights[i]))
- val = np.concatenate((np.ones(1), np.array(val)))
- return val[1]
- def fit(self, data, labels, learning_rate=0.1, epochs=100):
- ones = np.ones((1, data.shape[0]))
- z = np.concatenate((ones.T, data), axis=1)
- '''
- a = [[1,2],
- [3,4]]
- b = [[5,6]]
- c = np.concatenate((a,b), axis = 1)
- c = [[1,2,5],
- [3,4,6]]
- '''
- training = epochs*self.steps_per_epoch
- for k in range(training):
- if k%self.steps_per_epoch==0:
- print(f'epochs: {k/self.steps_per_epoch}')
- for s in data:
- print(s, self.predict(s))
- sample = np.random.randint(data.shape[0])
- y =[z[sample]]
- for i in range(len(self.weights)-1):
- activation = np.dot(y[i], self.weights[i])
- activity = self.activity(activation)
- activity = np.concatenate((np.ones(1),np.array(activity)))
- y.append(activity)
- activation = np.dot(y[-1], self.weights[-1])
- activity = self.activity(activation)
- y.append(activity)
- error = labels[sample]- y[-1]
- delta_vec = [error*self.activity_derivative(
- y[-1])]
- for i in range(self.layers-2, 0 ,-1):
- error = delta_vec[-1].dot(self.weights[i][1:].T)
- error = error*self.activity_derivative(y[i][1:])
- delta_vec.append(error)
- delta_vec.reverse()
- for i in range(len(self.weights)):
- layer = y[i].reshape(1, self.arch[i]+1)
- delta = delta_vec[i].reshape(1,self.arch[i+1])
- self.weights[i]+= learning_rate*layer.T.dot(delta)
- if __name__=='__main__':
- np.random.seed(0)
- model = Neural_Network([2,2,1])
- x = np.array([[0,0],
- [0,1],
- [1,0],
- [1,1]])
- y = np.array([0,1,1,0])
- model.fit(x,y,epochs=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement