【问题标题】:How to plot epoch vs. val_acc and epoch vs. val_loss graph in CNN?如何在 CNN 中绘制 epoch vs. val_acc 和 epoch vs. val_loss 图?
【发布时间】:2019-07-11 23:26:58
【问题描述】:

我使用卷积神经网络 (CNN) 来训练数据集。在这里,我将 epoch、val_loss、val_acc、total loss、训练时间等作为历史记录。如果我想计算准确率的平均值,那么如何访问 val_acc,以及如何绘制 epoch vs. val_acc 和 epoch vs. val_loss 图?

convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3], name='input')
convnet = conv_2d(convnet, 32, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)

convnet = conv_2d(convnet, 64, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)

convnet = conv_2d(convnet, 128, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)

convnet = conv_2d(convnet, 32, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)

convnet = conv_2d(convnet, 64, 3, activation='relu')
convnet = max_pool_2d(convnet, 3)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 4, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='log')

if os.path.exists('{}.meta'.format(MODEL_NAME)):
   model.load(MODEL_NAME)
   print('model loaded!')

train = train_data[:-150]
test = train_data[-50:]

X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,3)
test_y = [i[1] for i in test]

hist=model.fit({'input': X}, {'targets': Y}, n_epoch=8, validation_set=({'input': test_x}, {'targets': test_y}),
snapshot_step=40, show_metric=True, run_id=MODEL_NAME)
model.save(MODEL_NAME)

【问题讨论】:

    标签: python python-3.x tensorflow image-processing conv-neural-network


    【解决方案1】:

    您可以使用callbacks in Tensorflow。特别是,您可以使用Keras callbacks 之类的CSVLogger,它将您的纪元结果流式传输到CSV。从那里您可以进行各种分析。

    基于您的代码的示例:

    csv_logger = CSVLogger('training.log')
    model.fit({'input': X}, {'targets': Y}, ..., callbacks=[csv_logger]
    

    【讨论】:

      【解决方案2】:

      尝试以下方法:

      history = model.fit(X_train, Y_train, validation_data=(X_test, Y_test), batch_size=32, epochs=10, verbose=1)
      
      # Get training and test loss histories
      training_loss = history.history['loss']
      test_loss = history.history['val_loss']
      
      # Create count of the number of epochs
      epoch_count = range(1, len(training_loss) + 1)
      
      # Visualize loss history
      plt.plot(epoch_count, training_loss, 'r--')
      plt.plot(epoch_count, test_loss, 'b-')
      plt.legend(['Training Loss', 'Test Loss'])
      plt.xlabel('Epoch')
      plt.ylabel('Loss')
      plt.show();
      

      感谢https://chrisalbon.com/deep_learning/keras/visualize_loss_history/

      【讨论】:

        猜你喜欢
        • 2019-07-21
        • 1970-01-01
        • 2018-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-28
        • 2023-04-11
        • 1970-01-01
        相关资源
        最近更新 更多