Предполагая базовый рабочий процесс в 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
)? - Кроме того, вместо нескольких значений для каждой мини-партии, как записать среднее значение занабор проверки?