我是theano新手,我正在实施一个简单的基于感知器的学习规则,我收到了以下错误,我无法理解为什么会出现此错误?
这是我的代码:
import numpy as np
import theano
from theano import function
from theano import tensor as T
from theano import shared
from theano import Param
from theano.tensor.shared_randomstreams import RandomStreams
import matplotlib
import matplotlib.pyplot as plt
import numpy
import theano
import theano.tensor as T
randGen = numpy.random
#perceptron
N = 400
feats = 2
randGen = np.random
data_class1 = randGen.normal(-3.0,1.0,N/2*feats).reshape(N/2,feats)
data_class0 = randGen.normal(3.0,1.0,N/2*feats).reshape(N/2,feats)
data_class1 = np.concatenate((np.ones((N/2,1)),data_class1) , axis=1)
data_class0 = np.concatenate((np.ones((N/2,1)),data_class0) , axis=1)
class1_label = np.ones(N/2)
class0_label = -1*np.ones(N/2)
D = (np.concatenate((data_class1,data_class0)), np.concatenate((class1_label,class0_label)))
training_steps = 10000
# Declare Theano symbolic variables
x = T.row("x")
y = T.row("y")
w = shared(randGen.normal(0.0,1.0,feats+1).reshape(feats+1,1), name="w")
x_data = T.matrix('x_data')
s_data = T.sgn(2*T.sgn(T.dot(x_data,w))-1)
predictedOut = function([x_data],s_data)
s = T.sgn(2*T.sgn(T.dot(x,w)-1))
prod = function([x],s)
z1 = T.row('z1')
w_up = function(inputs=[x,y,z1],outputs=[T.transpose(x)*(z1-y)])
z2 = T.row('z2')
train = function([z2],
updates=[(w,w-z2)]
)
count = 0
while np.abs(np.sum(predictedOut(D[0])-D[1])) > 0:
print 'on example ',count
a1 = D[0][count,:].reshape(1,feats+1)
b1 = D[1][count].reshape(1,1)
a2 = prod(a1).reshape(1,1)
a3 = w_up(a1,b1,a2)[0].reshape(feats+1,1)
train(a3)
count += 1
ERROR:
on example 0
Traceback (most recent call last):
File "/Users/theanoPractice/src/Perceptron.py", line 68, in <module>
train(a3)
File "/Library/Python/2.7/site-packages/theano/compile/function_module.py", line 497, in __call__
allow_downcast=s.allow_downcast)
File "/Library/Python/2.7/site-packages/theano/tensor/type.py", line 174, in filter
" dimension.", data.shape, self.broadcastable)
TypeError: ('Bad input argument to theano function at index 0(0-based)', 'Non-unit value on shape on a broadcastable dimension.', (3, 1), (True, False))
答案 0 :(得分:0)
所以我在代码中发现了问题,
“train”函数需要一行,但传递的值是(3,1)列向量。
所以这是更正后的版本:
train = function([z2],
updates=[(w,w-T.transpose(z2))]
)
count = 0
while np.abs(np.sum(predictedOut(D[0])-D[1])) > 0 and count < 1000 :
print 'on example ',count
a1 = D[0][count%N,:].reshape(1,feats+1)
b1 = D[1][count%N].reshape(1,1)
a2 = prod(a1).reshape(1,1)
a3 = w_up(a1,b1,a2)[0].reshape(1,feats+1)
train(a3)
count += 1