machine-learning - 如何在 Keras 中拟合模型期间检查预测输出?

标签 machine-learning keras neural-network

我是 Keras 新手,我学会了拟合和评估模型。 评估模型后,我们可以看到模型做出的实际预测。

我想知道在 Keras 中拟合期间是否也可以看到预测?到目前为止,我找不到任何执行此操作的代码。

最佳答案

由于这个问题没有指定“纪元”,并且由于使用回调可能代表额外的计算,因此我认为这并不完全是重复。

通过 tensorflow ,您可以使用自定义训练循环并打开急切执行。创建自定义训练循环的简单教程:https://www.tensorflow.org/tutorials/eager/custom_training_walkthrough

基本上你会:

#transform your data in to a Dataset:
dataset = tf.data.Dataset.from_tensor_slices(
    (x_train, y_train)).shuffle(some_buffer).batch(batchSize) 
     #the above is buggy in some versions regarding shuffling, you may need to shuffle
     #again between each epoch    


#create an optimizer    
optimizer = tf.keras.optimizers.Adam()

#create an epoch loop:
for e in range(epochs):

    #create a batch loop
    for i, (x, y_true) in enumerate(dataset):

        #create a tape to record actions
        with  tf.GradientTape() as tape:

            #take the model's predictions
            y_pred = model(x)

            #calculate loss
            loss = tf.keras.losses.binary_crossentropy(y_true, y_pred)

        #calculate gradients
        gradients = tape.gradient(loss, model.trainable_weights)

        #apply gradients
        optimizer.apply_gradients(zip(gradients, model.trainable_weights)

您可以使用 y_pred var 执行任何操作,包括获取其 numpy_pred = y_pred.numpy() 值。

本教程提供了有关指标和验证循环的更多详细信息。

关于machine-learning - 如何在 Keras 中拟合模型期间检查预测输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58114217/

相关文章:

python - keras 启动时间 (_make_train_function()) 在 Tesla V100-SXM2-16GB GPU 上非常慢,与功能较弱的 GPU 相比

tensorflow - 为 CNN 调试 'TypeError: Can not convert a ndarray into a Tensor or Operation'

python - 我应该如何解释 sparse_categorical_crossentropy 函数的输出?

python - 没有名为 "fastai"的模块

neural-network - 训练一个 doc2Vec 模型实际需要多少数据?

machine-learning - 如何使用神经网络进行人脸检测?

python - 如何获取列表中随机选择的行的索引(Python)

Python 和 HyperOpt : How to make multi-process grid searching?

algorithm - k-means 输入应该包含唯一值还是所有值(也重复)?

python - 加载具有自定义层的模型时 Keras 中的形状不兼容