假设在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
)?