名称错误:尽管未定义X-Train

时间:2019-06-10 09:49:19

标签: python

我正在为基于Logistic回归的神经网络编码。定义所有子功能后,我创建了一个模型,在其中调用了这些功能。但其名称错误。

   def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000,     learning_rate = 0.5, print_cost = False):
      Arguments:
      X_train -- training set represented by a numpy array of shape (num_px * num_px * 3, m_train)

      return d

#As the model is called after it is defined,
  d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.5, print_cost = True)

 #It gives error
 Name Error:  name 'train_set_x' is not defined. Traceback suggests that the error in  statement which calls function.

2 个答案:

答案 0 :(得分:0)

对我来说范围似乎不对?您将整个内容发布到了一个完整的代码段中,但是看起来就像您定义了一个函数:

def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000,     
          learning_rate = 0.5, print_cost = False):
    X_train, Y_train, X_test, Y_test, classes = load_dataset() 

然后要使用它?

但是请注意:您希望model函数从某个地方获取值-参数列表是您从外界获得的,而不是想要返回的!

X_model和其余的仅在该函数中分配,因此调用d = model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = True)没有X_train,没有Y_train,X_test,Y_test ...

参数是从左到右解析的,所以这就是为什么您只会得到第一个错误的原因。但是,如果您在X_train = None之前执行d=...,则会收到关于Y_train的相同错误消息。

答案 1 :(得分:0)

这对我有用。尝试从头开始运行单元以上传数据文件