我想在训练模型时监控坡度和权重。这是重现该问题的简单示例。看来keras deos并没有记录分布和直方图。 我在Ubuntu 18.04上使用Tensorflow 1.14和keras 2.4
import numpy as np
import pandas as pd
data = np.random.rand(200, 2)
expected = np.random.randint(2, size=200).reshape(-1, 1)
dataFrame = pd.DataFrame(data, columns=['a', 'b'])
expectedFrame = pd.DataFrame(expected, columns=['expected'])
dataFrameTrain, dataFrameTest = dataFrame[:100], dataFrame[-100:]
expectedFrameTrain, expectedFrameTest = expectedFrame[:100], expectedFrame[-100:]
def generator(X_data, y_data, batch_size):
samples_per_epoch = X_data.shape[0]
number_of_batches = samples_per_epoch / batch_size
counter = 0
while 1:
X_batch = np.array(X_data[batch_size * counter:batch_size * (counter + 1)]).astype('float32')
y_batch = np.array(y_data[batch_size * counter:batch_size * (counter + 1)]).astype('float32')
counter += 1
yield (X_batch, y_batch)
# restart counter to yeild data in the next epoch as well
if counter >= number_of_batches:
counter = 0
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input
img_input = Input(shape=(2))
l1 = Dense(12, activation='relu')(img_input)
l2 = Dense(1, activation='sigmoid')(l1)
model = Model(inputs=[img_input], outputs=[l2])
model.compile(loss='binary_crossentropy', optimizer='adadelta', metrics=['accuracy'])
# Train the model using generator vs using the full batch
batch_size = 8
# Tensorboard callbacks
from tensorflow.keras.callbacks import TensorBoard
tb = TensorBoard(log_dir='./logs',
histogram_freq=1,
write_graph=True,
write_images=True,
update_freq='epoch',
write_grads=True,
profile_batch=1)
# train with fit_generator
model.fit_generator(generator(dataFrameTrain, expectedFrameTrain, batch_size), epochs=3,
steps_per_epoch=dataFrame.shape[0] / batch_size, callbacks=[tb])
validation_data=generator(dataFrameTest, expectedFrameTest, batch_size * 2),
validation_steps=dataFrame.shape[0] / batch_size * 2
那么,为什么以这种方式实现?并且是否根据验证数据或训练数据来收集所有统计信息?!