添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
文雅的炒饭  ·  JPA Query Methods :: ...·  5 月前    · 
绅士的围巾  ·  JSON.NET: Why Use ...·  1 年前    · 

NameError: name 'X_train' is not defined.这个错误也出现在剩余的代码中。

3 人不认可
from sklearn.model_selection import train_test_split  
data,Label = shuffle(M, label, random_state = 2)
labelled_data = [data, Label]
X,Y = [labelled_data[0],labelled_data[1]]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.4, random_state=4)
x_test, x_validation, y_test, y_validation=train_test_split(X_test, Y_test, test_size=0.5,
random_state=4)
X_train = X_train.reshape((X_train.shape[0],256,256,3))
x_validation = x_validation.reshape((x_validation.shape[0],256,256,3))
x_test =x_test.reshape((x_test.shape[0],256,256,3))
X_train = X_train.astype('float32')
x_validation = x_validation.astype('float32')
x_test = x_test.astype('float32')
X_train = X_train/255
x_validation = x_validation/255
x_test =x_test/255
from keras.utils import np_utils
Y_validation = np_utils.to_categorical(Y_train,8)
y_validation =np_utils.to_categorical(y_validation,8)
y_test =np_utils.to_categorical(y_test,8)
from keras.models import Sequential
from keras.layers import Activation, Dense
model = Sequential()
model.add(Dense(units=256,input_shape=(1000,),activation='relu'))
model.add(Dense(units=64,activation='tanh'))
model.add(Dense(units=8,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='sgd', 
metrics=['categorical_accuracy']) 
model.fit(X_train,Y_train,epochs=5,batch_size=32)
model.predict(x_test,batch_size=32)
return model
from keras.layers.convolutional import Convolution2D
model = Sequential()
model.add(convolution2D(filters=(6,3,3),input_shape=(256,256,1),activation='relu'))
from keras.layers.convolutional import MaxPooling2D
model = Sequential()
model.add(MaxPooling2D(pool_size=(2,2),strides=2))
from keras.layers import Dropout
model = Sequential()
model.add(Convolution2D(filters=6, nb_row=3, nb_col=3,subsample=(2,2),
    input_shape=(256, 256, 1,), activation='relu', border_mode='same'))
model.add(Dropout(0.1))
model.add(Flatten())
model.add(Dense(8, activation='softmax'))
model= fish_model()
print(model.summary())
history = model.fit(X_train,Y_train,validation_data=(x_validation, y_validation),epochs=5,batch_size=32)
Model: "sequential_42"
NameError                                 Traceback (most recent call last)
 <ipython-input-131-ab439073340b> in <module>
       1 model= fish_model()
       2 print(model.summary())
 ----> 3 history = model.fit(X_train,Y_train,validation_data=(x_validation, y_validation),epochs=5,batch_size=32)
 NameError: name 'X_train' is not defined

我已经定义了X_train,但它显示了一个错误,如ir没有定义。 当我试图测试准确性时,我也得到了同样的错误,如X_test没有定义。

2 个评论
看起来 X_train 是在不同的会话中定义的?所有的行都在一个会话中运行吗?
如果你在Jupyter笔记本中运行这个,并且你已经重启了内核,请确保你先运行了之前的单元。另外,为什么要在你的第一个 model.predict 之后设置 return model
python
machine-learning
keras
SREE LAKSHMI
SREE LAKSHMI
发布于 2019-09-29
1 个回答
Joe McKenna
Joe McKenna
发布于 2019-09-29
已采纳
0 人赞同

the line number shown in the error