本文整理匯總了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)