python - Keras - Error when checking target -
given following code:
import matplotlib.pyplot plt import numpy keras import callbacks keras import optimizers keras.layers import dense, dropout keras.models import sequential keras.callbacks import modelcheckpoint sklearn.preprocessing import standardscaler sklearn.ensemble import extratreesclassifier sklearn.utils import shuffle # stopping - stop training before overfitting early_stop = callbacks.earlystopping(monitor='val_loss', min_delta=0, patience=3, verbose=1, mode='auto') # fix random seed reproducibility seed = 42 numpy.random.seed(seed) # load pima indians dataset dataset = numpy.loadtxt("./data/poc.csv",skiprows=1, delimiter=",") # split input (x) , output (y) variables x = dataset[:, 0:14] y = dataset[:, 14:18] # # standardize features removing mean , scaling unit variance scaler = standardscaler() x = scaler.fit_transform(x) #adam optimizer learning rate decay opt = optimizers.adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0001) ## create our model model = sequential() model.add(dense(200, input_dim=14, kernel_initializer='uniform', activation='relu')) model.add(dropout(0.2)) model.add(dense(100, activation='relu')) model.add(dropout(0.2)) model.add(dense(60, activation='relu')) model.add(dropout(0.2)) model.add(dense(30, activation='relu')) model.add(dropout(0.2)) model.add(dense(5, activation='sigmoid')) model.summary() # compile model using binary crossentropy since predicting 0/1 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) # checkpoint filepath="./checkpoints/weights.best.hdf5" checkpoint = modelcheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=true, mode='max') # fit model history = model.fit(x, y, validation_split=0.33, epochs=10000, batch_size=10, verbose=0, callbacks=[early_stop,checkpoint])
and data so:
17.6,1,1,0,1,0,0,0,0,0,0,3.9,9.2,20.29,0,1,0,0,0 12.9,1,0,1,0,0,0,0,0,0,0,4.1,13.5,0.08,0,0,0,1,0 3.2,1,0,1,0,0,0,0,0,0,0,4.122031746,13.8,0.01,0,0,0,0,0 ...
i following output / error:
_________________________________________________________________ layer (type) output shape param # ================================================================= dense_1 (dense) (none, 200) 3000 _________________________________________________________________ dropout_1 (dropout) (none, 200) 0 _________________________________________________________________ dense_2 (dense) (none, 100) 20100 _________________________________________________________________ dropout_2 (dropout) (none, 100) 0 _________________________________________________________________ dense_3 (dense) (none, 60) 6060 _________________________________________________________________ dropout_3 (dropout) (none, 60) 0 _________________________________________________________________ dense_4 (dense) (none, 30) 1830 _________________________________________________________________ dropout_4 (dropout) (none, 30) 0 _________________________________________________________________ dense_5 (dense) (none, 5) 155 ================================================================= total params: 31,145 trainable params: 31,145 non-trainable params: 0 _________________________________________________________________
error when checking target: expected dense_5 have shape (none, 1) got array shape (716, 4)
what missing?
your last layer, dense_5
, has size 5, while target has size 4.
in order work, size of each targert must number of classes want predict. remember have represented in 1 hot enconding. can use to_categorical keras.
Comments
Post a Comment