简体   繁体   中英

Why my acc always higher but my val_acc is very small?

I tried to train 14000 training datasets and 3500 validation datasets, but why every time I train I always get high accuracy results while the validation section is very small

so what should I do if I want the results of the validation to be close to the accuracy of the training and provide significant additions to each epoch

does there have to be something to add or subtract? [sorry for bad english]

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

classifier = Sequential()


classifier.add(Conv2D(16, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
`classifier.add(MaxPooling2D(pool_size = (2, 2)))


classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))`


classifier.add(Conv2D(64, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

classifier.add(Flatten())

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

from keras.callbacks import TensorBoard
# Use TensorBoard
callbacks = TensorBoard(log_dir='./Graph')

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (64, 64),
                                                 batch_size = 32,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (64, 64),
                                            batch_size = 32,
                                            class_mode = 'binary')

classifier.fit_generator(training_set,
                         steps_per_epoch = 100,
                         epochs = 200,
                         validation_data = test_set,
                         validation_steps = 200)

classifier.save('model.h5')

im got this result (sorry im don't know how to put image in here)

Epoch 198/200 100/100 [==============================] - 114s 1s/step - loss: 0.1032 - acc: 0.9619 - val_loss: 1.1953 - val_acc: 0.7160

Epoch 199/200 100/100 [==============================] - 115s 1s/step - loss: 0.1107 - acc: 0.9591 - val_loss: 1.4148 - val_acc: 0.6702

Epoch 200/200 100/100 [==============================] - 112s 1s/step - loss: 0.1229 - acc: 0.9528 - val_loss: 1.2995 - val_acc: 0.6928

When your training accuracy is very high, but your validation accuracy is low, you have overfitted your model. Simply, your model has learned the structure of your training data, but is unable to generalize it. In order to reduce overfitting, you can try to

  • simplify your model,
  • introduce dropout to some layers,
  • use bigger training batches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM