找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2001|回复: 4

[求助] 神经网络求解逻辑运算,变量维度没弄明白,请大神指点。

4

主题

5

帖子

5

积分

贫民

积分
5
choumu3416 发表于 2018-1-10 16:10:55 | 显示全部楼层 |阅读模式
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.random((in_size,out_size)))
        self.b = theano.shared(np.ones((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,3,T.nnet.sigmoid)
l2 = Layer(l1.outputs,3,1,None)

#计算cost
cost = T.sum(-(a_hat*T.log(l2.outputs) + (1-a_hat)*T.log(1-l2.outputs)))

#计算梯度
gW1,gb1,gW2,gb2 = T.grad(cost,[l1.W,l1.b,l2.W,l2.b])
learning_rate = 0.1
train = theano.function(
    inputs = [x,a_hat],
    outputs = [l2.outputs,cost],
    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)

print ('The outputs of the NN are:')
for i in range(len(train_inputs)):
    print ('The output for x1=%d | x2=%d is %.2f' % (train_inputs[0],train_inputs[1],prediction))

plt.plot(cost)
plt.show()

回复

使用道具 举报

2

主题

219

帖子

219

积分

版主

Rank: 7Rank: 7Rank: 7

积分
219

热心会员默默耕耘优秀版主

剑心无痕 发表于 2018-1-11 09:12:25 | 显示全部楼层
你输入的参数维度没有问题,是你计算损失值得时候维度出的问题
把l2.outputs转置一下就行了
cost = T.sum(-(a_hat*T.log(l2.outputs.T) + (1-a_hat)*T.log(1-l2.outputs.T)))
回复 支持 反对

使用道具 举报

4

主题

5

帖子

5

积分

贫民

积分
5
choumu3416  楼主| 发表于 2018-1-11 18:57:31 | 显示全部楼层
剑心无痕 发表于 2018-1-11 09:12
你输入的参数维度没有问题,是你计算损失值得时候维度出的问题
把l2.outputs转置一下就行了
cost = T.sum(- ...

谢谢大神。转置后语法没有问题,但是没有正确结果输出。还请大神抽时间指点下。
回复 支持 反对

使用道具 举报

2

主题

219

帖子

219

积分

版主

Rank: 7Rank: 7Rank: 7

积分
219

热心会员默默耕耘优秀版主

剑心无痕 发表于 2018-1-12 10:26:49 | 显示全部楼层
choumu3416 发表于 2018-1-11 18:57
谢谢大神。转置后语法没有问题,但是没有正确结果输出。还请大神抽时间指点下。 ...

sigmoid要放在最后一层把输出控制在0~1,你的最后一层没有激活函数,很难收敛
l1 = Layer(x,2,3,T.nnet.relu)
l2 = Layer(l1.outputs,3,1,T.nnet.sigmoid)
回复 支持 反对

使用道具 举报

2

主题

219

帖子

219

积分

版主

Rank: 7Rank: 7Rank: 7

积分
219

热心会员默默耕耘优秀版主

剑心无痕 发表于 2018-1-12 11:04:31 | 显示全部楼层
choumu3416 发表于 2018-1-11 18:57
谢谢大神。转置后语法没有问题,但是没有正确结果输出。还请大神抽时间指点下。 ...

在你的基础上改了一下, 仅供参考
  1. import theano
  2. import theano.tensor as T
  3. from theano import shared
  4. import numpy as np
  5. from random import random
  6. import matplotlib.pyplot as plt

  7. class Layer():
  8.     def __init__(self,inputs,in_size,out_size,activation_function):
  9.         self.W = theano.shared(np.random.randn(in_size,out_size))
  10.         self.b = theano.shared(np.zeros(out_size))
  11.         self.lay_out = T.dot(inputs,self.W) +self.b
  12.         self.activation_function = activation_function
  13.         if activation_function is None:
  14.             self.outputs = self.lay_out
  15.         else:
  16.             self.outputs = self.activation_function(self.lay_out)

  17. #定义输入量
  18. x = T.matrix('x')
  19. a_hat = T.vector('a_hat')

  20. #添加层
  21. l1 = Layer(x,2,8,T.nnet.relu)
  22. l2 = Layer(l1.outputs,8,1,T.nnet.sigmoid)
  23. output = l2.outputs.T

  24. #计算costs
  25. costs = T.nnet.binary_crossentropy(output, a_hat).mean()

  26. #计算梯度
  27. gW1,gb1,gW2,gb2 = T.grad(costs,[l1.W,l1.b,l2.W,l2.b])
  28. learning_rate = 0.01
  29. train = theano.function(
  30.     inputs = [x,a_hat],
  31.     outputs = [output,costs],
  32.     updates = [
  33.         [l1.W,l1.W-learning_rate*gW1],
  34.         [l1.b,l1.b-learning_rate*gb1],
  35.         [l2.W,l2.W-learning_rate*gW2],
  36.         [l2.b,l2.b-learning_rate*gb2],
  37.     ]
  38. )

  39. #定义输入和权重
  40. train_inputs = [
  41.     [0,0],
  42.     [0,1],
  43.     [1,0],
  44.     [1,1]
  45. ]
  46. train_outputs = [1,0,0,1]

  47. #遍历所有输入并计算输出
  48. cost = []
  49. for iteration in range(10000):
  50.     prediction,cost_iter = train(train_inputs,train_outputs)
  51.     cost.append(cost_iter)

复制代码
prediction  # array([[ 0.98008668,  0.00795113,  0.00725245,  0.98606478]])
cost[-1] # array(0.012352318532146248)



回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表