@BlueBowser/

Artificial intelegence

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import numpy as np
import random


class neuralnetwork(object):
    """Defines the values for the equasion"""
    def __init__(self):
        self.inputLayerSize=2
        self.outputLayerSize=1
        self.hiddenLayerSize=3
        self.w1=np.random.randn(self.inputLayerSize,self.hiddenLayerSize)
        self.w2=np.random.randn(self.hiddenLayerSize,self.outputLayerSize)
    """this downwards describes the steps that the computer must follow to get the correct awsner"""
    def forward(self, x):
        self.z2=np.dot(x, self.w1)
        self.a2=self.sigmoid(self.z2)
        self.z3=np.dot(self.a2,self.w2)
        self.yhat=self.sigmoid(self.z3)
        return self.yhat
        
    def sigmoid(self, z):
        return 1/(1+np.exp(-z))

    def sigmoidprime(self,z):
        return np.exp(-z)/((1+np.exp(-z))**2)

    def costfunction(self,x,y):
        self.yhat=self.forward(x)
        j=0.5*sum((y-self.yhat)**2)
        return j

    def costfunctionprime(self,x,y):
        self.yhat=self.forward(x)
        delta3=np.multiply(-(y-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(x.T,delta2)
        return dJdW1,dJdW2
    
    """the function that train the code to accuratly predict the awsner"""
    def train(self,iterations,X,Y):
        dJdW1,dJdW2=self.costfunctionprime(X, Y)
        for i in range(iterations):
            self.w1-=dJdW1
            self.w2-=dJdW2
            
"""to find avrage"""            
def avgcalc(x,y):
    return(x+y)/2

trainingsetinput=[]
trainingsetoutput=[]
testingsetinput=[]
testingsetoutput=[]
"""generates random numbers"""
for i in range(10):
    newset=[]
    x=random.random()
    y=random.random()
    newset=[x,y]
    trainingsetinput.append(newset)
    avg=[avgcalc(x,y)]
    trainingsetoutput.append(avg)

for i in range(10):
    newset=[]
    x=random.random()
    y=random.random()
    newset=[x,y]
    testingsetinput.append(newset)
    avg=[avgcalc(x,y)]
    testingsetoutput.append(avg)
    
trainingsetinput=np.array(trainingsetinput)
trainingsetoutput=np.array(trainingsetoutput)
testingsetinput=np.array(testingsetinput)
testingsetoutput=np.array(testingsetoutput)
"""the code that allows the users to see that awsners that the program makes"""
nn = neuralnetwork()
cost1=#nn.costfunction(trainingsetinput, trainingsetoutput)
print('Cost Function 1: ', cost1)
dJdW1,dJdW2=nn.costfunctionprime(trainingsetinput, trainingsetoutput)
print(dJdW1,dJdW2)

nn.train(1000,trainingsetinput,trainingsetoutput)
print(nn.forward(trainingsetinput))