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

Layer (type) Output Shape Param #

 dense_67 (Dense)             (None, 256)               256256    

 dense_68 (Dense)             (None, 64)                16448     

dense_69 (Dense) (None, 8) 520

 Total params: 273,224
 Trainable params: 273,224
  Non-trainable params: 0

None

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

I have already defined X_train but it shows an error like ir is not defined. When i tried to test the accuracy also i got the same error like x_test is not defined.

2
  • 1
    It looks like X_train is defined in different session? Are all the lines run in a single session?
    – Chris
    Commented Sep 29, 2019 at 9:56
  • If you are running this in a Jupyter notebook and you have restarted the kernel, be sure you have first run the previous cells. Also, why return model after your first model.predict?
    – desertnaut
    Commented Sep 29, 2019 at 11:21

1 Answer 1

0

the line number shown in the error


'3'


does not match the line number in the code you posted.


Start running your code in chunks to get the 'real' line number of the error. For example, run the first 10 lines of code, then the next 10 lines of code, or run the first module then run the second module. When you get to the error by chunking, the line number should be correct or you may find the error when running the chunks before you get to the end of your code.

2
  • when i split this and run i am getting error in the beginning itself.That is data,Label = shuffle(M, label, random_state = 2) labelled_data = [data, Label] X,Y = [labelled_data[0],labelled_data[1]] here i am getting like X is not defined Commented Sep 29, 2019 at 12:47
  • I do not see 'shuffle' in the built in functions of python, and I don't see your definition of 'shuffle' posted in the functions. Make sure your scripts (including 'shuffle' def) are in the directories listed in the python<vernum>._pth file in the python.exe directory in order to import without error and use variables in the imports. Commented Sep 30, 2019 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.