Keras:跟踪Tensorboard的自定义变量/张量

时间:2019-03-04 13:36:54

标签: tensorflow keras tensorboard

假设在Keras中使用基本工作流程(没有有意义的任务):

import keras
import keras.backend as K
import numpy as np
import tensorflow as tf

numSamples = 1024
dimInput = 4
dimOutput = 2

inputs = keras.layers.Input(shape=(dimInput,))
output = keras.layers.Dense(200, activation='relu')(inputs)
tf.summary.scalar('custom_var', tf.reduce_mean(output))
predictions = keras.layers.Dense(dimOutput, activation='softmax')(output)
model = keras.models.Model(inputs=inputs, outputs=predictions)

def custom_metric(a, b):
    return K.mean(output)

model.compile(optimizer=keras.optimizers.Adam(lr=0.1), loss='sparse_categorical_crossentropy', metrics=['accu
tensorboard = keras.callbacks.TensorBoard(log_dir='./delme', histogram_freq=1)

x = np.random.random((numSamples, dimInput))
x = (x - np.mean(x)) / np.std(x)
y = np.random.randint(dimOutput, size=numSamples)
model.fit(x=x, y=y, batch_size=16, epochs=10, validation_split=0.25, callbacks=[tensorboard])

对于上面的示例,custom_var仅在histogram_freq>0的情况下针对每个单个小批进行评估和记录。

  • 如何在不记录直方图的情况下记录custom_var (通过设置histogram_freq>0)?
  • 另外,而不是多个 每个小批量的数值,如何记录验证集的平均值?

0 个答案:

没有答案