Home > other >  NameError: name 'Dropout' is not defined how to solve?
NameError: name 'Dropout' is not defined how to solve?

Time:02-13

This is my source code
 # use dropout to improve neural network 
The from __future__ import print_function
The import numpy as np
The from keras. Datasets import mnist
The from keras. Models import Sequential
The from keras. The layers. The core import Dense, Activation
The from keras. Optimizers import SGD
The from keras. Utils import np_utils
Np, the random seed (1671) # repetitive setting

# network and training
NB_EPOCH=250
BATCH_SIZE=128
VERBOSE=1
NB_CLASSES=10
The OPTIMIZER=SGD ()
N_HIDDEN=128
VALIDATION_SPLIT=0.2
DROPOUT=0.3
# data: the mixed and divided into training set and testing set
(X_train y_train), (X_test, y_test)=mnist. Load_data ()
28 * 28 # X_train is 60000 lines of data, can be transformed into 60000 * 784
RESHAPED=784
#
X_train=X_train. Reshape (60000, RESHAPED)
X_test=X_test. Reshape (10000, RESHAPED)
X_train=X_train. Astype (' float32 ')
X_test=X_test. Astype (' float32 ')
# normalized
X_train/=255
X_test/=255
# transform class vector into binary classification matrix
Y_train=np_utils. To_categorical (Y_train NB_CLASSES)
Y_test=np_utils. To_categorical (Y_test NB_CLASSES)
# N_HIDDEN a hidden layer, 10 a output
The model=Sequential ()
Model. The add (Dense (N_HIDDEN, input_shape=(RESHAPED,)))
Model. The add (Activation (' relu))
Model. The add (Dropout (Dropout))
Model. The add (Dense (N_HIDDEN))
Model. The add (Activation (' relu))
Model. The add (Dropout (Dropout))
Model. The add (Dense (NB_CLASSES))
Model. The add (Activation (' softmax))
Model. The summary ()
Model.com from running (loss='categorical_crossentropy',
Optimizer=optimizer,
The metrics=[' accuracy '])
History=the model fit (X_train Y_train,
Batch_size=batch_size, epochs=NB_EPOCH,
Verbose=verbose, validation_split=validation_split)
Score=model. The evaluate (X_test Y_test, verbose=verbose)
Print (", "Test score, score [0])
Print (' Test accuracy: 'score [1])

  • Related