In [1]:
import theano
import theano.tensor as T
import numpy as np
import numpy
In [2]:
x = T.vector()
w1 = theano.shared(np.random.randn(2))
b1 = theano.shared(np.random.randn(1))
w2 = theano.shared(np.random.randn(2))
b2 = theano.shared(np.random.randn(1))
w = theano.shared(np.random.randn(2))
b = theano.shared(np.random.randn(1))
z1 = T.dot(w1,x) + b1
a1 = 1/(1+T.exp(-1*z1))
z2 = T.dot(w2,x) + b2
a2 = 1/(1+T.exp(-1*z2))
z = T.dot(w,[a1,a2]) + b
a = 1/(1+T.exp(-1*z))
In [3]:
# define xor's output function
fa = theano.function(inputs=[x] ,outputs=a)
In [4]:
fa([1,1])
Out[4]:
In [5]:
# 期望的 output 值
y_hat = T.scalar()
In [6]:
cost = -1*(y_hat*T.log(a)+(1-y_hat)*T.log(1-a)).sum()
In [7]:
fcost = theano.function(inputs=[x,y_hat],outputs=cost )
In [8]:
fcost([1,1],0)
Out[8]:
In [9]:
dw1,db1,dw2,db2,dw,db = T.grad(cost,[w1,b1,w2,b2,w,b] )
In [10]:
from itertools import izip
def Myupdates(ps,gs):
r = 0.1
pu = [ (p,p-r*g) for p,g in izip(ps,gs) ]
return pu
print Myupdates([w,],[dw,] )
In [11]:
g = theano.function(inputs=[x,y_hat],outputs=[cost,a] , updates=Myupdates([w,b,w1,b1,w2,b2],[dw,db,dw1,db1,dw2,db2] ) )
In [12]:
for i in range(100000):
g([0,1],1)
g([1,0],1)
g([0,0],0)
g([1,1],0)
In [13]:
print g([0,1],1)
print g([1,0],1)
print g([0,0],0)
print g([1,1],0)
In [ ]: