def call(self, inputs): x = inputs x = self.conv1(x) x = self.maxpool1(x) x = self.conv2(x) x = self.maxpool2(x) x = self.flatten(x) x = self.dense1(x) x = self.dense2(x) x = self.gaussianconnection(x) # x = tf.reshape(x, [batchsz,-1]) # reshape成平面层 # x = self.dense5(x) # 输出层 return x myLeNet = LeNet() myLeNet.compile(optimizer=optimizers.Adam(lr=1e-3), loss=tf.losses.CategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # tensorboard 可视化过程 tb = TensorBoard( log_dir='logs', histogram_freq=0, write_graph=True, write_images=False, update_freq='batch', profile_batch=2, embeddings_freq=0, embeddings_metadata=None ) tb.on_epoch_begin(0) myLeNet.fit(db, epochs=10, validation_data = db_test, validation_freq = 1, callbacks=[tb]) myLeNet.evaluate(db_test)
embeddings_freq=1) loss_history = [] cos_loss_history = [] T = 0.0 t0 = time.time() tbcb.set_model(model) tbcb.on_train_begin() f = open('log.txt', 'w') try: for epoch in range(200): tbcb.on_epoch_begin(epoch) cos_loss = CosineSimilarity() for step in (range(num_batch)): tbcb.on_train_batch_begin(step) print('========== step: {:03d} / {:03d} ============\r'.format( step, num_batch), end='') u, seq, pos, neg = sampler.next_batch() seq = tf.convert_to_tensor(seq) pos = tf.convert_to_tensor(pos) neg = tf.convert_to_tensor(neg) with tf.GradientTape() as tape:
tensorboard = TensorBoard(log_dir="logs/") tensorboard.set_model(model) tensorboard.on_train_begin() epochs = 3 train_logs_dict = {} test_logs_dict = {} for epoch in range(epochs): training_acc, testing_acc, training_loss, testing_loss = [], [], [], [] print("\nStart of epoch %d" % (epoch + 1, )) # Iterate over the batches of the dataset. modelcheckpoint.on_epoch_begin(epoch) earlystop.on_epoch_begin(epoch) reduce_lr.on_epoch_begin(epoch) tensorboard.on_epoch_begin(epoch) for x_batch_train, y_batch_train in get_batch(batch_size, x_train, y_train): train_loss, train_accuracy = model.train_on_batch( x_batch_train, y_batch_train) training_acc.append(train_accuracy) training_loss.append(train_loss) for x_batch_test, y_batch_test in get_batch(batch_size, x_test, y_test): test_loss, test_accuracy = model.test_on_batch(x_batch_test, y_batch_test) testing_acc.append(test_accuracy) testing_loss.append(test_loss) train_logs_dict = get_logs(train_logs_dict, epoch, model, x_train, y_train)