gpt4 book ai didi

python - 将每个批处理或纪元的验证准确性打印到控制台(Keras)

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:44 28 4
gpt4 key购买 nike

我正在使用 ImageDataGeneratorflow_from_directory 生成我的数据,并且使用 model.fit_generator 来拟合数据。

这默认仅输出训练数据集的准确性。似乎没有将验证准确性输出到终端的选项。

这是我的代码的相关部分:

#train data generator


print('Starting Preprocessing')

train_datagen = ImageDataGenerator(preprocessing_function = preprocess)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size,
class_mode = 'categorical') #class_mode = 'categorical'


#same for validation
val_datagen = ImageDataGenerator(preprocessing_function = preprocess)

validation_generator = val_datagen.flow_from_directory(
validation_data_dir,
target_size = (img_height, img_width),
batch_size=batch_size,
class_mode='categorical')





########################Model Creation###################################

#create the base pre-trained model
print('Finished Preprocessing, starting model creating \n')
base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(12, activation='softmax')(x)
model = Model(input=base_model.input, output=predictions)




for layer in model.layers[:-34]:
layer.trainable = False
for layer in model.layers[-34:]:
layer.trainable = True


from keras.optimizers import SGD
model.compile(optimizer=SGD(lr=0.001, momentum=0.92),
loss='categorical_crossentropy',
metrics = ['accuracy'])



#############SAVE Model #######################################


file_name = str(datetime.datetime.now()).split(' ')[0] + '_{epoch:02d}.hdf5'
filepath = os.path.join(save_dir, file_name)



checkpoints =ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
save_best_only=False, save_weights_only=False,
mode='auto', period=2)

###############Fit Model #############################

model.fit_generator(
train_generator,
steps_per_epoch =total_samples//batch_size,
epochs = epochs,
validation_data=validation_generator,
validation_steps=total_validation//batch_size,
callbacks = [checkpoints],
shuffle= True)

更新输出:

在整个训练过程中,我只得到训练准确率的输出,但在训练结束时,我得到了训练和验证的准确性。

Epoch 1/10

1/363 [..............................] - ETA: 1:05:58 - loss: 2.4976 - acc: 0.0640
2/363 [..............................] - ETA: 51:33 - loss: 2.4927 - acc: 0.0760
3/363 [..............................] - ETA: 48:55 - loss: 2.5067 - acc: 0.0787
4/363 [..............................] - ETA: 47:26 - loss: 2.5110 - acc: 0.0770
5/363 [..............................] - ETA: 46:30 - loss: 2.5021 - acc: 0.0824
6/363 [..............................] - ETA: 45:56 - loss: 2.5063 - acc: 0.0820

最佳答案

这个想法是你在每个时期之后检查你的验证集,而不是在每个批处理之后。如果在每批处理之后,您都必须在整个验证集上评估模型的性能,那么您会浪费很多时间。

在每个 epoch 之后,您都会有相应的损失和准确度用于训练和验证。但是在一个时期内,您将只能访问训练损失和准确性。

关于python - 将每个批处理或纪元的验证准确性打印到控制台(Keras),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47930176/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com