|
在你的基础上改了一下, 仅供参考- import theano
- import theano.tensor as T
- from theano import shared
- import numpy as np
- from random import random
- import matplotlib.pyplot as plt
- class Layer():
- def __init__(self,inputs,in_size,out_size,activation_function):
- self.W = theano.shared(np.random.randn(in_size,out_size))
- self.b = theano.shared(np.zeros(out_size))
- self.lay_out = T.dot(inputs,self.W) +self.b
- self.activation_function = activation_function
- if activation_function is None:
- self.outputs = self.lay_out
- else:
- self.outputs = self.activation_function(self.lay_out)
- #定义输入量
- x = T.matrix('x')
- a_hat = T.vector('a_hat')
- #添加层
- l1 = Layer(x,2,8,T.nnet.relu)
- l2 = Layer(l1.outputs,8,1,T.nnet.sigmoid)
- output = l2.outputs.T
- #计算costs
- costs = T.nnet.binary_crossentropy(output, a_hat).mean()
- #计算梯度
- gW1,gb1,gW2,gb2 = T.grad(costs,[l1.W,l1.b,l2.W,l2.b])
- learning_rate = 0.01
- train = theano.function(
- inputs = [x,a_hat],
- outputs = [output,costs],
- updates = [
- [l1.W,l1.W-learning_rate*gW1],
- [l1.b,l1.b-learning_rate*gb1],
- [l2.W,l2.W-learning_rate*gW2],
- [l2.b,l2.b-learning_rate*gb2],
- ]
- )
- #定义输入和权重
- train_inputs = [
- [0,0],
- [0,1],
- [1,0],
- [1,1]
- ]
- train_outputs = [1,0,0,1]
- #遍历所有输入并计算输出
- cost = []
- for iteration in range(10000):
- prediction,cost_iter = train(train_inputs,train_outputs)
- cost.append(cost_iter)
复制代码 prediction # array([[ 0.98008668, 0.00795113, 0.00725245, 0.98606478]])
cost[-1] # array(0.012352318532146248)
|
|