Python theano:类中未定义的变量

Python theano:类中未定义的变量,python,class,undefined,theano,Python,Class,Undefined,Theano,最低限度准则: import theano import theano.tensor as T import numpy as np from numpy import dtype class logistic_regression(object): # constructor def __init__(self,input,n_in,n_out): # initialise parameters self.W=theano.shared(value=np.zer

最低限度准则:

import theano
import theano.tensor as T
import numpy as np
from numpy import dtype

class logistic_regression(object):
# constructor
    def __init__(self,input,n_in,n_out):
    # initialise parameters
       self.W=theano.shared(value=np.zeros((n_in,n_out),dtype=theano.config.floatX),name='W',borrow=True)
    self.b=theano.shared(value=np.zeros((n_out,),dtype=theano.config.floatX),name='b',borrow=True)
    self.p_y_given_x=self.calculate_probability()
    self.neg_log_likelihood=self.calculate_neg_log_likelihood(y)
def calculate_probability(self):
    p_y_given_x=T.nnet.softmax(T.dot(input,self.W)+self.b)
    return p_y_given_x
def calculate_neg_log_likelihood(self,y):
    neg_log_likelihood=T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y])
    return neg_log_likelihood
行中:

self.neg\u log\u似然=self.calculate\u neg\u log\u似然(y)
我在eclipse中遇到一个错误:

“未定义变量y”


嗯,您正在传递一个变量
y
,它没有在任何地方定义。
Eclipse正在构造函数范围内,然后在全局范围内寻找变量y的定义,例如,
y=10

因为它找不到它,它会警告你


您可能想传递
p\u y\u给定的\u x
变量吗?

类的构造函数需要y作为输入。一旦我把y作为n输入,问题就解决了。现在def_uuinit_uu(self,input,n_in,n_out)读作def_uinit_u(self,input,y,n_in,n_out)