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

  • 本文关键字:单位 广播 numpy 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

相关内容

  • 没有找到相关文章