本文整理汇总了Python中tensorflow.keras.callbacks.CSVLogger方法的典型用法代码示例。如果您正苦于以下问题:Python callbacks.CSVLogger方法的具体用法?Python callbacks.CSVLogger怎么用?Python callbacks.CSVLogger使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.callbacks
的用法示例。
在下文中一共展示了callbacks.CSVLogger方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_callbacks
# 需要导入模块: from tensorflow.keras import callbacks [as 别名]
# 或者: from tensorflow.keras.callbacks import CSVLogger [as 别名]
def get_callbacks(model_file, logging_file=None, early_stopping_patience=None,
initial_learning_rate=0.01, lr_change_mode=None, verbosity=1):
callbacks = list()
# save the model
callbacks.append(ModelCheckpoint(model_file, monitor='val_loss', save_best_only=True, mode='auto'))
# records the basic metrics
callbacks.append(CSVLogger(logging_file, append=True))
return callbacks
示例2: pretrain
# 需要导入模块: from tensorflow.keras import callbacks [as 别名]
# 或者: from tensorflow.keras.callbacks import CSVLogger [as 别名]
def pretrain(self, x, y=None, optimizer='adam', epochs=200, batch_size=256,
save_dir='results/temp', verbose=1, aug_pretrain=False):
print('Begin pretraining: ', '-' * 60)
self.autoencoder.compile(optimizer=optimizer, loss='mse')
csv_logger = callbacks.CSVLogger(save_dir + '/pretrain_log.csv')
cb = [csv_logger]
if y is not None and verbose > 0:
class PrintACC(callbacks.Callback):
def __init__(self, x, y):
self.x = x
self.y = y
super(PrintACC, self).__init__()
def on_epoch_end(self, epoch, logs=None):
if int(epochs / 10) != 0 and epoch % int(epochs/10) != 0:
return
feature_model = Model(self.model.input,
self.model.get_layer(index=int(len(self.model.layers) / 2)).output)
features = feature_model.predict(self.x)
km = KMeans(n_clusters=len(np.unique(self.y)), n_init=20, n_jobs=4)
y_pred = km.fit_predict(features)
print(' '*8 + '|==> acc: %.4f, nmi: %.4f <==|'
% (metrics.acc(self.y, y_pred), metrics.nmi(self.y, y_pred)))
cb.append(PrintACC(x, y))
# begin pretraining
t0 = time()
if not aug_pretrain:
self.autoencoder.fit(x, x, batch_size=batch_size, epochs=epochs, callbacks=cb, verbose=verbose)
else:
print('-=*'*20)
print('Using augmentation for ae')
print('-=*'*20)
def gen(x, batch_size):
if len(x.shape) > 2: # image
gen0 = self.datagen.flow(x, shuffle=True, batch_size=batch_size)
while True:
batch_x = gen0.next()
yield [batch_x, batch_x]
else:
width = int(np.sqrt(x.shape[-1]))
if width * width == x.shape[-1]: # gray
im_shape = [-1, width, width, 1]
else: # RGB
width = int(np.sqrt(x.shape[-1] / 3.0))
im_shape = [-1, width, width, 3]
gen0 = self.datagen.flow(np.reshape(x, im_shape), shuffle=True, batch_size=batch_size)
while True:
batch_x = gen0.next()
batch_x = np.reshape(batch_x, [batch_x.shape[0], x.shape[-1]])
yield [batch_x, batch_x]
self.autoencoder.fit_generator(gen(x, batch_size), steps_per_epoch=int(x.shape[0]/batch_size),
epochs=epochs, callbacks=cb, verbose=verbose,
workers=8, use_multiprocessing=True if platform.system() != "Windows" else False)
print('Pretraining time: ', time() - t0)
self.autoencoder.save_weights(save_dir + '/ae_weights.h5')
print('Pretrained weights are saved to %s/ae_weights.h5' % save_dir)
self.pretrained = True
print('End pretraining: ', '-' * 60)