有没有一种方法可以在on_epoch_end上跟踪预测?

时间:2019-11-30 00:42:49

标签: python numpy tensorflow keras callback

我创建了一个具有3个单独输出的keras模型:

MODEL = Model(inputs = [image, speed],outputs = [steering, gas, brake])                                 

我想跟踪每个时期对恒定输入的预测,只是看到了这一点: Create keras callback to save model predictions and targets for each batch during training

我的输出形状为(如果batch为256):[arr,arr,arr]其中arr是形状为(256,)的numpy数组,因为有256个示例和一个值。

在尝试使用Create keras callback to save model predictions and targets for each batch during training中的代码时,我在形状上总是出错,所以请您告诉我在此处声明self.var_y_true时应该放入什么形状?

class CollectOutputAndTarget(Callback):
    def __init__(self):
        super(CollectOutputAndTarget, self).__init__()
        self.targets = []  # collect y_true batches
        self.outputs = []  # collect y_pred batches

        # the shape of these 2 variables will change according to batch shape
        # to handle the "last batch", specify `validate_shape=False`
        self.var_y_true = tf.Variable(0., validate_shape=False)
        self.var_y_pred = tf.Variable(0., validate_shape=False)

    def on_batch_end(self, batch, logs=None):
        # evaluate the variables and save them into lists
        self.targets.append(K.eval(self.var_y_true))
        self.outputs.append(K.eval(self.var_y_pred))

我认为这可能有效(由于我们没有在张量声明中指定批处理大小,因此似乎没有任何意义):

self.var_y_true = tf.Variable([np.zeros(256,)]*3, validate_shape=False)

ValueError: Argument must be a dense tensor:
got shape [13, 256], but wanted [13]

另一条线索是:

self.var_y_true = tf.Variable([np.zeros(1,)]*3, validate_shape=False)

ValueError: Argument must be a dense tensor: [array([0]), array([0]), array([0])] - got shape [3, 1], but wanted [3].

在训练过程中如何跟踪预测?

0 个答案:

没有答案