Numpy 可广播维度上形状的非单位值

Numpy 可广播维度上形状的非单位值,numpy,theano,Numpy,Theano,我是一个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

我是一个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
错误:

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))

所以我在代码中发现了问题

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

我复制并粘贴了你的代码,它运行良好。但这并不意味着它可以在我的机器上工作:循环永远不会运行,因为条件的计算结果为False。如果我有时间,我会检查如果我强制调用函数会发生什么。我不确定这里到底发生了什么,因为我不完全了解您的架构,尤其是您的函数序列看起来很奇怪,只有在调用w_up的输出时才起作用。但我觉得这可能是因为您经常使用T.row,它的特点是ndim=2,broadcastable=[True,False]。然后您继续转置它们,这可能会导致问题,尽管它不应该-。broadcastable会相应地被转置,但可能它仍然会变得混乱。你为什么不直接和t.fvector一起工作呢?谢谢!!所以我发现了问题,答案如下。