Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from sklearn.utils import shuffle
- x=64
- Y=10
- z=1
- i=0
- q=0
- X = np.array([[0]*x])
- y = np.array([0])
- nb=20
- while q!=nb:
- def lirecompter():
- inputfile=("test.txt")
- with open (inputfile,'r') as inputfile:
- filedata=inputfile.read()
- destinationpath=("test.txt")
- filedata=filedata.replace(texttofind,texttoreplace)
- with open (destinationpath,'w') as file:
- file.write(filedata)
- #pour modifier les [0, 0, 0, 255] en 0
- texttofind="[0, 0, 0, 255]"
- texttoreplace="0"
- lirecompter()
- #pour modifier les [255, 255, 255, 255] en 1
- texttofind="[255, 255, 255, 255]"
- texttoreplace="1"
- lirecompter()
- texttofind="[255, 255, 255]"
- texttoreplace="1"
- lirecompter()
- texttofind="[0, 0, 0]"
- texttoreplace="0"
- lirecompter()
- texttofind="[["
- texttoreplace=" "
- lirecompter()
- texttofind="]]"
- texttoreplace=" "
- lirecompter()
- texttofind="["
- texttoreplace=""
- lirecompter()
- texttofind="]"
- texttoreplace=""
- lirecompter()
- texttofind=","
- texttoreplace=""
- lirecompter()
- L=open("test.txt","r")
- l=(L.read()).split(" ")
- a = int(l[0])
- b=[a]
- Sortie=[0]*10
- Sortie[a]=1
- listes=[0]*65
- for loop in range(65):
- listes[loop]=l[loop]
- texttofind=' '.join(map(str, listes))+' '
- exttoreplace=""
- del listes[0]
- lirecompter()
- y=np.insert(y,q,b)
- # X = (hours studying, hours sleeping), y = score on test, xPredicted = 4 hours studying & 8 hours sleeping (input data for prediction)
- E=listes
- X=np.insert(X,i,E)
- i=i+1
- q=q+1
- X=X.reshape(-1, 64)
- y=y.reshape(-1,1)
- y=np.delete(y,nb,0)
- X=np.delete(X,nb,0)
- y=y/10
- X, y = shuffle(X, y)
- xPredicted = np.array([0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0])
- # scale units
- class Neural_Network(object):
- def __init__(self):
- #parameters
- self.inputSize = x
- self.outputSize = z
- self.hiddenSize = Y
- #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, X):
- #forward propagation through our network
- self.z = np.dot(X, 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 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, X, y, o):
- # backward propagate through the network
- self.o_error = y - 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 += X.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, X, y):
- o = self.forward(X)
- self.backward(X, y, o)
- def saveWeights(self):
- np.savetxt("w1.txt", self.W1, fmt="%s")
- np.savetxt("w2.txt", self.W2, fmt="%s")
- def predict(self):
- print ("Predicted data based on trained weights Damn: ");
- print ("Input (scaled): \n" + str(xPredicted));
- print ("Output: \n" + str(self.forward(xPredicted)));
- NN = Neural_Network()
- for i in range(1000): # trains the NN 1,000 times
- print ("# " + str(i) + "\n")
- print ("Input (scaled): \n" + str(X))
- print ("Actual Output: \n" + str(y))
- print ("Predicted Output: \n" + str(NN.forward(X)))
- print ("Loss: \n" + str(np.mean(np.square(y - NN.forward(X))))) # mean sum squared loss
- print ("\n")
- NN.train(X, y)
- NN.saveWeights()
- NN.predict()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement