def train(): with tf.Graph().as_default(): with tf.device('/gpu:' + str(GPU_INDEX)): #pointclouds_pl, labels_pl = placeholder_inputs(BATCH_SIZE, NUM_POINT) pointclouds_pl, labels_pl = MODEL.placeholder_inputs( BATCH_SIZE, NUM_POINT, POINT_DIM) is_training_pl = tf.placeholder(tf.bool, shape=()) # Note the global_step=batch parameter to minimize. # That tells the optimizer to helpfully increment the 'batch' parameter # for you every time it trains. batch = tf.get_variable('batch', [], initializer=tf.constant_initializer(0), trainable=False) bn_decay = get_bn_decay(batch) tf.summary.scalar('bn_decay', bn_decay) # Get model and loss pred, end_points = MODEL.get_model(pointclouds_pl, is_training_pl, bn_decay=bn_decay) MODEL.get_loss(pred, labels_pl, end_points) losses = tf.get_collection('losses') total_loss = tf.add_n(losses, name='total_loss') tf.summary.scalar('total_loss', total_loss) for l in losses + [total_loss]: tf.summary.scalar(l.op.name, l) correct = tf.equal(tf.argmax(pred, 1), tf.to_int64(labels_pl)) accuracy = tf.reduce_sum(tf.cast(correct, tf.float32)) / float(BATCH_SIZE) tf.summary.scalar('accuracy', accuracy) print("--- Get training operator") # Get training operator learning_rate = get_learning_rate(batch) tf.summary.scalar('learning_rate', learning_rate) if OPTIMIZER == 'momentum': optimizer = tf.train.MomentumOptimizer(learning_rate, momentum=MOMENTUM) elif OPTIMIZER == 'adam': optimizer = tf.train.AdamOptimizer(learning_rate) train_op = optimizer.minimize(total_loss, global_step=batch) # Add ops to save and restore all the variables. saver = tf.train.Saver() # Create a session config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True config.log_device_placement = False sess = tf.Session(config=config) # Add summary writers merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter( os.path.join(log.LOG_DIR, 'train'), sess.graph) test_writer = tf.summary.FileWriter(os.path.join(log.LOG_DIR, 'test'), sess.graph) # Init variables init = tf.global_variables_initializer() sess.run(init) ops = { 'pointclouds_pl': pointclouds_pl, 'labels_pl': labels_pl, 'is_training_pl': is_training_pl, 'pred': pred, 'loss': total_loss, 'train_op': train_op, 'merged': merged, 'step': batch, 'end_points': end_points } best_acc = -1 for epoch in range(MAX_EPOCH): log.out('**** EPOCH %03d ****' % (epoch)) sys.stdout.flush() train_one_epoch(sess, ops, train_writer) eval_one_epoch(sess, ops, test_writer) # Save the variables to disk. if epoch % 10 == 0: save_path = saver.save(sess, os.path.join(log.LOG_DIR, "model.ckpt")) log.out("Model saved in file: %s" % save_path)
def train_source_domain(deep_model, n_res_blocks, batch_size, learning_rate, path_save_network_model, model_dir_, feature_space_dimension, margin_in_loss, loss_type): #================================ settings: save_plot_embedding_space = True save_points_in_embedding_space = True load_saved_network_model = False num_epoch = 300 save_network_model_every_how_many_epochs = 10 save_embedding_every_how_many_epochs = 10 STEPS_PER_EPOCH_TRAIN = 704 n_samples_plot = 2000 #--> if None, plot all # path_tfrecords_train = 'D:\\triplet_work_Main\\_Important_files\\triplets.tfrecords' # path_tfrecords_train = '.\\..\\..\\triplet_work_Main\\_Important_files\\triplets.tfrecords' path_tfrecords_train = 'D:\\TCGA_triplets\\tfrecord\\triplets.tfrecords' path_save_embedding_space = ".\\results\\" + deep_model + "\\embedding_train_set\\" path_save_loss = ".\\loss_saved\\" #================================ train_dataset = tf.data.TFRecordDataset([path_tfrecords_train]) train_dataset = train_dataset.map(Utils.parse_function) train_dataset = train_dataset.map(Utils.normalize_triplets) num_repeat = None train_dataset = train_dataset.repeat(num_repeat) train_dataset = train_dataset.shuffle(buffer_size=1024) train_dataset = train_dataset.batch(batch_size) handle = tf.placeholder(tf.string, shape=[]) iterator = tf.data.Iterator.from_string_handle(handle, train_dataset.output_types, train_dataset.output_shapes) next_element = iterator.get_next() # training_iterator = train_dataset.make_initializable_iterator() training_iterator = tf.data.make_initializable_iterator(train_dataset) # Siamese: if deep_model == "CNN": siamese = CNN_Siamese.CNN_Siamese( loss_type=loss_type, feature_space_dimension=feature_space_dimension, margin_in_loss=margin_in_loss) elif deep_model == "ResNet": siamese = ResNet_Siamese.ResNet_Siamese( loss_type=loss_type, feature_space_dimension=feature_space_dimension, n_res_blocks=n_res_blocks, margin_in_loss=margin_in_loss, is_train=True) # train_step = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(siamese.loss) train_step = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize( siamese.loss) # tf.initialize_all_variables().run() saver_ = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) training_handle = sess.run(training_iterator.string_handle()) sess.run(training_iterator.initializer) if load_saved_network_model: _, latest_epoch = load_network_model( saver_=saver_, session_=sess, checkpoint_dir=path_save_network_model, model_dir_=model_dir_, model_name=deep_model) else: latest_epoch = -1 loss_average_of_epochs = [] for epoch in range(latest_epoch + 1, num_epoch): losses_in_epoch = [] print("============= epoch: " + str(epoch) + "/" + str(num_epoch - 1)) embeddings_in_epoch = np.zeros( (STEPS_PER_EPOCH_TRAIN * batch_size * 3, feature_space_dimension)) types_in_epoch = np.zeros( (STEPS_PER_EPOCH_TRAIN * batch_size * 3, )) subtypes_in_epoch = np.zeros( (STEPS_PER_EPOCH_TRAIN * batch_size * 3, )) for i in range(STEPS_PER_EPOCH_TRAIN): image_anchor, image_neighbor, image_distant, type_anchor, type_neighbor, type_distant, subtype_anchor, subtype_neighbor, subtype_distant = sess.run( next_element, feed_dict={handle: training_handle}) image_anchor_batch_3_channels = image_anchor.reshape( (batch_size, 128, 128, 4))[:, :, :, 0:3] image_neighbor_batch_3_channels = image_neighbor.reshape( (batch_size, 128, 128, 4))[:, :, :, 0:3] image_distant_batch_3_channels = image_distant.reshape( (batch_size, 128, 128, 4))[:, :, :, 0:3] _, loss_v, embedding1, embedding2, embedding3 = sess.run( [ train_step, siamese.loss, siamese.o1, siamese.o2, siamese.o3 ], feed_dict={ siamese.x1: image_anchor_batch_3_channels, siamese.x2: image_neighbor_batch_3_channels, siamese.x3: image_distant_batch_3_channels }) embeddings_in_epoch[((i * 3 * batch_size) + (0 * batch_size)):( (i * 3 * batch_size) + (1 * batch_size)), :] = embedding1 embeddings_in_epoch[((i * 3 * batch_size) + (1 * batch_size)):( (i * 3 * batch_size) + (2 * batch_size)), :] = embedding2 embeddings_in_epoch[((i * 3 * batch_size) + (2 * batch_size)):( (i * 3 * batch_size) + (3 * batch_size)), :] = embedding3 types_in_epoch[((i * 3 * batch_size) + (0 * batch_size)):( (i * 3 * batch_size) + (1 * batch_size))] = type_anchor types_in_epoch[((i * 3 * batch_size) + (1 * batch_size)):( (i * 3 * batch_size) + (2 * batch_size))] = type_neighbor types_in_epoch[((i * 3 * batch_size) + (2 * batch_size)):( (i * 3 * batch_size) + (3 * batch_size))] = type_distant subtypes_in_epoch[((i * 3 * batch_size) + (0 * batch_size)):( (i * 3 * batch_size) + (1 * batch_size))] = subtype_anchor subtypes_in_epoch[((i * 3 * batch_size) + (1 * batch_size)):( (i * 3 * batch_size) + (2 * batch_size))] = subtype_neighbor subtypes_in_epoch[((i * 3 * batch_size) + (2 * batch_size)):( (i * 3 * batch_size) + (3 * batch_size))] = subtype_distant losses_in_epoch.extend([loss_v]) # report average loss of epoch: loss_average_of_epochs.append( np.average(np.asarray(losses_in_epoch))) print("Average loss of epoch " + str(epoch) + ": " + str(loss_average_of_epochs[-1])) if not os.path.exists(path_save_loss): os.makedirs(path_save_loss) np.save(path_save_loss + "loss.npy", np.asarray(loss_average_of_epochs)) # plot the embedding space: if (epoch % save_embedding_every_how_many_epochs == 0): if save_points_in_embedding_space: if not os.path.exists(path_save_embedding_space + "numpy\\"): os.makedirs(path_save_embedding_space + "numpy\\") np.save( path_save_embedding_space + "numpy\\embeddings_in_epoch_" + str(epoch) + ".npy", embeddings_in_epoch) np.save( path_save_embedding_space + "numpy\\types_in_epoch_" + str(epoch) + ".npy", types_in_epoch) np.save( path_save_embedding_space + "numpy\\subtypes_in_epoch_" + str(epoch) + ".npy", subtypes_in_epoch) if save_plot_embedding_space: print("saving the plot of embedding space....") plt.figure(200) # fig.clf() TCGA_get_color_and_shape_of_points(embeddings_in_epoch, types_in_epoch, subtypes_in_epoch, n_samples_plot) if not os.path.exists(path_save_embedding_space + "plots\\"): os.makedirs(path_save_embedding_space + "plots\\") plt.savefig(path_save_embedding_space + "plots\\" + 'epoch' + str(epoch) + '_step' + str(i) + '.png') plt.clf() plt.close() # save the network model: if (epoch % save_network_model_every_how_many_epochs == 0): save_network_model(saver_=saver_, session_=sess, checkpoint_dir=path_save_network_model, step=epoch, model_name=deep_model, model_dir_=model_dir_) print("Model saved in path: %s" % path_save_network_model)
#[x1, x2, x3] * [ w2 ] # w3 # 1은 w1 원소(열)가 하나이고, 행이 3이다. W = tf.Variable(tf.random_normal([3, 1]), name='weight') b = tf.Variable(tf.random_normal([1]), name='bias') # Hypothesis hypothesis = tf.matmul(X, W) + b # Simplified cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) train = optimizer.minimize(cost) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) for step in range(2001): cost_val, hy_val, _ = sess.run([cost, hypothesis, train], feed_dict={ X: x_data, Y: y_data }) if step % 10 == 0: print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)
def test_imhogeneous_poisson_process_example(self): # Toy 1D data. index_points = np.array([-10., -7.2, -4., -1., 0.8, 4., 6.2, 9.]).reshape([-1, 1]).astype(np.float32) observed_counts = np.array([100, 90, 60, 1, 4, 37, 55, 42]).astype(np.float32) # Trainable GP hyperparameters. kernel_log_amplitude = tf.Variable(0., name='kernel_log_amplitude') kernel_log_lengthscale = tf.Variable(0., name='kernel_log_lengthscale') observation_noise_log_scale = tf.Variable( 0., name='observation_noise_log_scale') # Generative model. def model_fn(): kernel = tfp.math.psd_kernels.ExponentiatedQuadratic( amplitude=tf.exp(kernel_log_amplitude), length_scale=tf.exp(kernel_log_lengthscale)) latent_log_rates = yield tfd.JointDistributionCoroutine.Root( tfd.GaussianProcess(kernel, index_points=index_points, observation_noise_variance=tf.exp( observation_noise_log_scale), name='latent_log_rates')) yield tfd.Independent(tfd.Poisson(log_rate=latent_log_rates), reinterpreted_batch_ndims=1, name='y') model = tfd.JointDistributionCoroutine(model_fn, name='model') # Variational model. logit_locs = tf.Variable(tf.zeros(observed_counts.shape)) logit_softplus_scales = tf.Variable( tf.ones(observed_counts.shape) * -1) def variational_model_fn(): _ = yield tfd.JointDistributionCoroutine.Root( tfd.Independent(tfd.Normal( loc=logit_locs, scale=tf.nn.softplus(logit_softplus_scales)), reinterpreted_batch_ndims=1)) _ = yield tfd.VectorDeterministic(observed_counts) q = tfd.JointDistributionCoroutine(variational_model_fn, name='variational_model') losses, sample_path = tfp.vi.fit_surrogate_posterior( target_log_prob_fn=lambda *args: model.log_prob(args), surrogate_posterior=q, optimizer=tf.optimizers.Adam(learning_rate=0.1), num_steps=100, seed=test_util.test_seed(), sample_size=1, trace_fn=lambda loss, grads, variables: (loss, q.sample(seed=42)[0])) self.evaluate(tf1.global_variables_initializer()) losses_, sample_path_ = self.evaluate((losses, sample_path)) self.assertLess(losses_[-1], 80.) # Optimal loss is roughly 40. # Optimal latent logits are approximately the log observed counts. self.assertAllClose(sample_path_[-1], np.log(observed_counts), atol=1.0)
def testMultipleConvMaskAdded(self, pruning_method): tf.reset_default_graph() g = tf.Graph() with g.as_default(): number_of_layers = 5 kernel_size = [3, 3] base_depth = 4 depth_step = 7 input_tensor = tf.ones((8, self.height, self.width, base_depth)) top_layer = input_tensor for ix in range(number_of_layers): units = base_depth + (ix + 1) * depth_step top_layer = pruning_layers.sparse_conv2d( x=top_layer, units=units, kernel_size=kernel_size, is_training=False, sparsity_technique=pruning_method) if pruning_method == 'variational_dropout': theta_logsigma2 = tf.get_collection( vd.layers.THETA_LOGSIGMA2_COLLECTION) self.assertLen(theta_logsigma2, number_of_layers) utils.add_vd_pruning_summaries(theta_logsigma2, threshold=3.0) dkl_loss_1 = utils.variational_dropout_dkl_loss( reg_scalar=1, start_reg_ramp_up=0, end_reg_ramp_up=1000, warm_up=False, use_tpu=False) dkl_loss_1 = tf.reshape(dkl_loss_1, [1]) dkl_loss_2 = utils.variational_dropout_dkl_loss( reg_scalar=5, start_reg_ramp_up=0, end_reg_ramp_up=1000, warm_up=False, use_tpu=False) dkl_loss_2 = tf.reshape(dkl_loss_2, [1]) for ix in range(number_of_layers): self.assertListEqual(theta_logsigma2[ix][0].get_shape().as_list(), [ kernel_size[0], kernel_size[1], base_depth + ix * depth_step, base_depth + (ix + 1) * depth_step ]) init_op = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init_op) if pruning_method == 'variational_dropout': loss_1, loss_2 = sess.run([dkl_loss_1, dkl_loss_2]) self.assertGreater(loss_2, loss_1) elif pruning_method == 'l0_regularization': theta_logalpha = tf.get_collection( l0.layers.THETA_LOGALPHA_COLLECTION) self.assertLen(theta_logalpha, number_of_layers) utils.add_l0_summaries(theta_logalpha) l0_norm_loss_1 = utils.l0_regularization_loss( reg_scalar=1, start_reg_ramp_up=0, end_reg_ramp_up=1000, warm_up=False, use_tpu=False) l0_norm_loss_1 = tf.reshape(l0_norm_loss_1, [1]) l0_norm_loss_2 = utils.l0_regularization_loss( reg_scalar=5, start_reg_ramp_up=0, end_reg_ramp_up=1000, warm_up=False, use_tpu=False) l0_norm_loss_2 = tf.reshape(l0_norm_loss_2, [1]) for ix in range(number_of_layers): self.assertListEqual(theta_logalpha[ix][0].get_shape().as_list(), [ kernel_size[0], kernel_size[1], base_depth + ix * depth_step, base_depth + (ix + 1) * depth_step ]) init_op = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init_op) loss_1, loss_2 = sess.run([l0_norm_loss_1, l0_norm_loss_2]) self.assertGreater(loss_2, loss_1) else: mask = tf.get_collection(core.MASK_COLLECTION) for ix in range(number_of_layers): self.assertListEqual(mask[ix].get_shape().as_list(), [ kernel_size[0], kernel_size[1], base_depth + ix * depth_step, base_depth + (ix + 1) * depth_step ])
def pretrain(): model = Model(args) sampler = DataGenerator(args) all_val_loss = [] with tf.Session() as sess: tf.global_variables_initializer().run() start = time.time() for epoch in range(args.num_epochs): for batch_idx in range( int(sampler.total_traj_num / args.batch_size)): batch_data = sampler.next_batch(args.batch_size) feed = dict(zip(model.input_form, batch_data)) sess.run(model.pretrain_op, feed) val_loss = compute_loss(sess, model, sampler, "val", args) # if len(all_val_loss) > 0 and val_loss >= all_val_loss[-1]: # print("Early termination with val loss: {}:".format(val_loss)) # break all_val_loss.append(val_loss) end = time.time() print("pretrain epoch: {}\tval loss: {}\telapsed time: {}".format( epoch, val_loss, end - start)) start = time.time() model_dir = f"./models/{args.model_type}_{args.x_latent_size}_{args.rnn_size}" if not os.path.exists(model_dir): os.makedirs(model_dir) save_model_name = model_dir + f"/{args.model_type}_pretrain" model.save(sess, save_model_name) # model_name = "./models/{}_{}_{}/{}_{}".format( # args.model_type, args.x_latent_size, args.rnn_size, args.model_type, 'pretrain') # model.restore(sess, model_name) # K-means init sample_num = 10000 x_embedded = [] for batch_idx in range(int(sample_num / args.batch_size)): batch_data = sampler.next_batch(args.batch_size) feed = dict(zip(model.input_form, batch_data)) x_embedded.append(sess.run(model.batch_post_embedded, feed)) x_embedded = np.concatenate(x_embedded, axis=0) kmeans = KMeans(n_clusters=args.mem_num) kmeans.fit(x_embedded) init_mu_c = kmeans.cluster_centers_ init_sigma_c = np.zeros_like(init_mu_c) init_pi = np.zeros(shape=args.mem_num) init_feed = dict( zip(model.cluster_init, [init_mu_c, init_sigma_c, init_pi])) sess.run([model.init_mu_c_op, model.init_sigma_c_op, model.init_pi_op], init_feed) save_model_name = "./models/{}_{}_{}/{}_{}".format( args.model_type, args.x_latent_size, args.rnn_size, args.model_type, "init") model.save(sess, save_model_name) print("Init model saved.")
activation=tf.nn.tanh) # unpacking the results outputs = tf.reshape(stacked_logits, [-1, n_outputs, n_neurons]) # TODO: try different error estimation function xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=outputs, name='error_est') loss = tf.reduce_mean(xentropy) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) training_op = optimizer.minimize(loss) correct = tf.nn.in_top_k(outputs, y, 1) accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) init = tf.global_variables_initializer() saver = tf.train.Saver() n_epoch = 1500 batch_size = 20 # training session with tf.Session() as sess: init.run() try: saver.restore(sess, './trained_models/music_synth_model') except FileNotFoundError: print('No previous model found') for epoch in range(n_epoch): x_batch, y_batch = 0, 0
def test_build_subnetwork(self, builder_params, want_name): with tf.Graph().as_default() as g, self.test_session(graph=g) as sess: data = np.concatenate([ np.ones((1, _IMAGE_DIM, _IMAGE_DIM, 1)), 2. * np.ones( (1, _IMAGE_DIM, _IMAGE_DIM, 1)) ]) features = {"x": tf.constant(data)} labels = tf.constant([0, 1]) training = True mode = tf.estimator.ModeKeys.TRAIN head = tf.contrib.estimator.binary_classification_head( loss_reduction=tf.losses.Reduction.SUM) ensemble = None name = None subnetwork = None builders = [] for builder_param in builder_params: builders.append( _builder(checkpoint_dir=self.test_subdirectory, **builder_param)) for idx, builder in enumerate(builders): name = builder.name # Pass the subnetworks of previous builders to the next builder. with tf.variable_scope("subnetwork_{}".format(idx)): subnetwork = builder.build_subnetwork( features=features, logits_dimension=head.logits_dimension, training=training, iteration_step=tf.train.get_or_create_global_step(), summary=_FakeSummary(), previous_ensemble=ensemble) logits = subnetwork.logits weighted_subnetworks = [] if ensemble: logits += ensemble.logits weighted_subnetworks = ensemble.weighted_subnetworks ensemble = adanet.Ensemble( weighted_subnetworks=weighted_subnetworks + [ adanet.WeightedSubnetwork( name=None, logits=logits, weight=None, subnetwork=subnetwork) ], logits=logits, bias=0.) estimator_spec = head.create_estimator_spec( features=features, labels=labels, mode=mode, train_op_fn=lambda loss: tf.no_op(), logits=ensemble.logits) sess.run(tf.global_variables_initializer()) train_op = builders[-1].build_subnetwork_train_op( subnetwork, estimator_spec.loss, var_list=None, labels=labels, iteration_step=tf.train.get_or_create_global_step(), summary=_FakeSummary(), previous_ensemble=ensemble) for _ in range(10): sess.run(train_op) self.assertEqual(want_name, name) self.assertGreater(sess.run(estimator_spec.loss), 0.0)
def test(self): content = tf.placeholder('float',[1,304,304,3]) style = tf.placeholder('float',[1,304,304,3]) ##### 添加第5个AE # content_encode_5=self.encoder.encoder(content,'relu5') # style_encode_5=self.encoder.encoder(style,'relu5') # blended_5=wct_tf(content_encode_5,style_encode_5,self.alpha) # stylized_5,var_list5=self.decoder.decoder(blended_5,'relu5') # # content_encode_4=self.encoder.encoder(stylized_5,'relu4') # style_encode_4=self.encoder.encoder(style,'relu4') # blended_4=wct_tf(content_encode_4,style_encode_4,self.alpha) # stylized_4,var_list4=self.decoder.decoder(blended_4,'relu4') ##### #内容图片经过编码器结果 content_encode_4 = self.encoder.encoder(content,'relu4') #风格图片经过编码器结果 style_encode_4 = self.encoder.encoder(style,'relu4') #wct后的结果 blended_4 = wct_tf(content_encode_4,style_encode_4,self.alpha) stylized_4 ,var_list4= self.decoder.decoder(blended_4,'relu4') content_encode_3 = self.encoder.encoder(stylized_4,'relu3') style_encode_3 = self.encoder.encoder(style,'relu3') blended_3 = wct_tf(content_encode_3,style_encode_3,self.alpha) stylized_3 ,var_list3= self.decoder.decoder(blended_3,'relu3') content_encode_2 = self.encoder.encoder(stylized_3,'relu2') style_encode_2 = self.encoder.encoder(style,'relu2') blended_2 = wct_tf(content_encode_2,style_encode_2,self.alpha) stylized_2 ,var_list2= self.decoder.decoder(blended_2,'relu2') content_encode_1 = self.encoder.encoder(stylized_2,'relu1') style_encode_1 = self.encoder.encoder(style,'relu1') blended_1 = wct_tf(content_encode_1,style_encode_1,self.alpha) stylized_1,var_list1 = self.decoder.decoder(blended_1,'relu1') #获取解码器模型的保存点 saver1 = tf.train.Saver(var_list1) saver2 = tf.train.Saver(var_list2) saver3 = tf.train.Saver(var_list3) saver4 = tf.train.Saver(var_list4) # saver5 = tf.train.Saver(var_list5) #add with tf.Session()as sess: tf.global_variables_initializer().run() tf.local_variables_initializer().run() #恢复保存点 saver1.restore(sess,self.decoder_weights[0]) saver2.restore(sess,self.decoder_weights[1]) saver3.restore(sess,self.decoder_weights[2]) saver4.restore(sess,self.decoder_weights[3]) # saver5.restore(sess,self.decoder_weights[4]) #add img_c = image.load_img(self.content_path,target_size=(304,304,3)) img_c = image.img_to_array(img_c) img_c = np.expand_dims(img_c,axis=0) img_s = image.load_img(self.style_path,target_size = (304,304,3)) img_s = image.img_to_array(img_s) img_s = np.expand_dims(img_s,axis=0) feed_dict = {content : img_c , style : img_s} #生成最终的结果 result = sess.run(stylized_1,feed_dict= feed_dict) result = result[0] result = np.clip(result,0,255)/255. imsave(self.output_path,result)
def train(sess, env, actor, critic, actor_noise, buffer_size, min_batch, ep): sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(max_to_keep=5) # Initialize target network weights actor.update_target_network() critic.update_target_network() # Initialize replay memory replay_buffer = ReplayBuffer(buffer_size, 0) max_episodes = ep max_steps = 1000 score_list = [] plt.ion() for i in range(max_episodes): state = env.reset() score = 0 for j in range(max_steps): action = actor.predict(np.reshape(state, (1, actor.s_dim))) + actor_noise() next_state, reward, done, info = env.step(action[0]) replay_buffer.add( np.reshape(state, (actor.s_dim,)), np.reshape(action, (actor.a_dim,)), reward, done, np.reshape(next_state, (actor.s_dim,)), ) # updating the network in batch if replay_buffer.size() < min_batch: continue states, actions, rewards, dones, next_states = replay_buffer.sample_batch( min_batch ) target_q = critic.predict_target( next_states, actor.predict_target(next_states) ) y = [] for k in range(min_batch): y.append(rewards[k] + critic.gamma * target_q[k] * (1 - dones[k])) # Update the critic given the targets predicted_q_value, _ = critic.train( states, actions, np.reshape(y, (min_batch, 1)) ) # Update the actor policy using the sampled gradient a_outs = actor.predict(states) grads = critic.action_gradients(states, a_outs) actor.train(states, grads[0]) # Update target networks actor.update_target_network() critic.update_target_network() state = next_state score += reward env.render() if done: print("Reward: {} | Episode: {}/{}".format(int(score), i, max_episodes)) break score_list.append(score) plt.cla() plt.plot(score_list) plt.pause(0.0001) avg = np.mean(score_list[-200:]) print("Average of last 200 episodes: {0:.2f} \n".format(avg)) if avg > 200: print("Task Completed") print("The last episode ran for {} time steps!".format((j + 1))) save_model( saver, sess, fname="model_checkpoints/lunarLander", steps=i, write_meta_graph=True, ) break if i % 10 == 0: save_model(saver, sess, fname="model_checkpoints/lunarLander", steps=i) plt.ioff() plt.show() plt.savefig("lunarLander-ddpg.png") return score_list
def decode_fn(hparams): """The main function.""" decode_dict, decode_mask, label_dict = _decode_common(hparams) if FLAGS.problem != "android_howto": decode_dict["input_refs"] = decode_utils.unify_input_ref( decode_dict["verbs"], decode_dict["input_refs"]) print_ops = [] for key in [ "raw_task", "verbs", "objects", "verb_refs", "obj_refs", "input_refs" ]: print_ops.append( tf.print(key, tf.shape(decode_dict[key]), decode_dict[key], label_dict[key], "decode_mask", decode_mask, summarize=100)) acc_metrics = decode_utils.compute_seq_metrics(label_dict, decode_dict, mask=None) saver = tf.train.Saver() with tf.Session() as session: session.run(tf.global_variables_initializer()) latest_checkpoint = tf.train.latest_checkpoint(FLAGS.checkpoint_path) tf.logging.info("Restoring from the latest checkpoint: %s" % (latest_checkpoint)) saver.restore(session, latest_checkpoint) task_seqs = [] ref_seqs = [] act_seqs = [] mask_seqs = [] try: i = 0 while True: tf.logging.info("Example %d" % i) task, acc, mask, label, decode = session.run([ decode_dict["raw_task"], acc_metrics, decode_mask, label_dict, decode_dict ]) ref_seq = {} ref_seq["gt_seq"] = np.concatenate([ label["verb_refs"], label["obj_refs"], label["input_refs"] ], axis=-1) ref_seq["pred_seq"] = np.concatenate([ decode["verb_refs"], decode["obj_refs"], decode["input_refs"] ], axis=-1) ref_seq["complete_seq_acc"] = acc["complete_refs_acc"] ref_seq["partial_seq_acc"] = acc["partial_refs_acc"] act_seq = {} act_seq["gt_seq"] = np.concatenate([ np.expand_dims(label["verbs"], 2), np.expand_dims(label["objects"], 2), label["input_refs"] ], axis=-1) act_seq["pred_seq"] = np.concatenate([ np.expand_dims(decode["verbs"], 2), np.expand_dims(decode["objects"], 2), decode["input_refs"] ], axis=-1) act_seq["complete_seq_acc"] = acc["complete_acts_acc"] act_seq["partial_seq_acc"] = acc["partial_acts_acc"] print("task", task) print("ref_seq", ref_seq) print("act_seq", act_seq) print("mask", mask) task_seqs.append(task) ref_seqs.append(ref_seq) act_seqs.append(act_seq) mask_seqs.append(mask) i += 1 except tf.errors.OutOfRangeError: pass save(task_seqs, ref_seqs, mask_seqs, "joint_refs") save(task_seqs, act_seqs, mask_seqs, "joint_act")
def run(n_epochs, L, C, lr, batch_size, use_l1_penalty=True, use_proximal_soft_thresholding=True): # Intialize variables and placeholders initialW = (((np.random.rand(28 * 28, 1) * 2) - 1) * 1e-6).astype(dtype='float32') initialB = 0.0 w = tf.Variable(initialW, name="w") b = tf.Variable(initialB, name="b") x = tf.placeholder(dtype=tf.float32, name='x') y = tf.placeholder(dtype=tf.float32, name='y') n_train = x_train.shape[0] predictions = tf.matmul(x, w) + b loss = tf.math.log(1 + tf.math.exp(tf.multiply(-y, predictions))) l1_penalty = (1 / C) * tf.reduce_sum(tf.abs(w)) if use_l1_penalty: risk = tf.reduce_mean(loss) + l1_penalty else: risk = tf.reduce_mean(loss) optimizer = tf.train.GradientDescentOptimizer(lr) train = optimizer.minimize(risk) errors = [] # create a tensorflow session and initialize the variables with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(0, n_epochs): for j in range(0, n_train, batch_size): jS = j jE = min(n_train, j + batch_size) x_batch = x_train[jS:jE, :] y_batch = y_train[jS:jE, :] _, curr_batch_risk, predBatchY, w_value = sess.run( [train, risk, predictions, w], feed_dict={ x: x_batch, y: y_batch }) if use_proximal_soft_thresholding: new_w_value = np.zeros(w_value.shape) if len(new_w_value[w_value >= 1 / L]) > 0: new_w_value[w_value >= 1 / L] = w_value[w_value >= 1 / L] - (1 / L) if len(new_w_value[w_value <= -1 / L]) > 0: new_w_value[w_value <= -1 / L] = w_value[w_value <= -1 / L] + (1 / L) new_w_assign = tf.assign(w, new_w_value) sess.run(new_w_assign) y_pred, curr_w, curr_b = sess.run([predictions, w, b], feed_dict={ x: x_test, y: y_test }) err = np.sum(np.abs(np.sign(y_pred) - y_test)) errors.append(err) print(i, "Total Error count:", err) return errors, w_value
def main(): print("print start load the params...") print(json.dumps(config, ensure_ascii=False, indent=2)) tf.logging.set_verbosity(tf.logging.INFO) tf.io.gfile.makedirs(config["out"]) tf.io.gfile.makedirs(config["train_logs_path"]) tf.io.gfile.makedirs(config["dev_logs_path"]) train_examples_len = config["train_examples_len"] dev_examples_len = config["dev_examples_len"] learning_rate_init = config["learning_rate"] eval_per_step = config["eval_per_step"] num_labels = config["num_labels"] num_train_steps = math.ceil(train_examples_len / config["train_batch_size"]) num_dev_steps = math.ceil(dev_examples_len / config["dev_batch_size"]) num_warmup_steps = math.ceil(num_train_steps * config["num_train_epochs"] * config["warmup_proportion"]) print("num_train_steps:{}, num_dev_steps:{}, num_warmup_steps:{}".format( num_train_steps, num_dev_steps, num_warmup_steps)) use_one_hot_embeddings = False is_training = True use_tpu = False seq_len = config["max_seq_len"] init_checkpoint = config["init_checkpoint"] print("print start compile the bert model...") # 定义输入输出 input_ids = tf.placeholder(tf.int64, shape=[None, seq_len], name='input_ids') input_mask = tf.placeholder(tf.int64, shape=[None, seq_len], name='input_mask') segment_ids = tf.placeholder(tf.int64, shape=[None, seq_len], name='segment_ids') labels = tf.placeholder(tf.int64, shape=[None, seq_len], name='labels') keep_prob = tf.placeholder(tf.float32, name='keep_prob') # , name='is_training' bert_config_ = load_bert_config(config["bert_config"]) (total_loss, acc, logits, probabilities) = create_model(bert_config_, is_training, input_ids, input_mask, segment_ids, labels, keep_prob, num_labels, use_one_hot_embeddings) train_op, learning_rate = optimization.create_optimizer( total_loss, learning_rate_init, num_train_steps * config["num_train_epochs"], num_warmup_steps, False) print("print start train the bert model...") batch_size = config["train_batch_size"] dev_batch_size = config["dev_batch_size"] init_global = tf.global_variables_initializer() saver = tf.train.Saver([ v for v in tf.global_variables() if 'adam_v' not in v.name and 'adam_m' not in v.name ], max_to_keep=2) # 保存最后top3模型 with tf.Session() as sess: sess.run(init_global) train_summary_writer = tf.summary.FileWriter(config["train_logs_path"]) dev_summary_writer = tf.summary.FileWriter(config["dev_logs_path"]) print("start load the pre train model") if init_checkpoint: # tvars = tf.global_variables() tvars = tf.trainable_variables() print("trainable_variables", len(tvars)) (assignment_map, initialized_variable_names ) = modeling.get_assignment_map_from_checkpoint( tvars, init_checkpoint) print("initialized_variable_names:", len(initialized_variable_names)) saver_ = tf.train.Saver( [v for v in tvars if v.name in initialized_variable_names]) saver_.restore(sess, init_checkpoint) tvars = tf.global_variables() initialized_vars = [ v for v in tvars if v.name in initialized_variable_names ] not_initialized_vars = [ v for v in tvars if v.name not in initialized_variable_names ] tf.logging.info('--all size %s; not initialized size %s' % (len(tvars), len(not_initialized_vars))) if len(not_initialized_vars): sess.run(tf.variables_initializer(not_initialized_vars)) for v in initialized_vars: print('--initialized: %s, shape = %s' % (v.name, v.shape)) for v in not_initialized_vars: print('--not initialized: %s, shape = %s' % (v.name, v.shape)) else: sess.run(tf.global_variables_initializer()) # if init_checkpoint: # saver.restore(sess, init_checkpoint) # print("checkpoint restored from %s" % init_checkpoint) print("********* train start *********") # tf.summary.FileWriter("output/",sess.graph) # albert remove dropout def train_step(ids, mask, segment, y, step): feed = { input_ids: ids, input_mask: mask, segment_ids: segment, labels: y, keep_prob: 0.9 } _, lr, out_loss, acc_, p_ = sess.run( [train_op, learning_rate, total_loss, acc, probabilities], feed_dict=feed) with train_summary_writer.as_default(): tf.summary.scalar('learning_rate', lr, step) tf.summary.scalar('loss', out_loss, step) tf.summary.scalar('accuracy', acc_, step) print("step :{}, lr:{}, loss :{}, acc :{}".format( step, lr, out_loss, acc_)) return out_loss, p_, y def dev_step(ids, mask, segment, y, step): feed = { input_ids: ids, input_mask: mask, segment_ids: segment, labels: y, keep_prob: 1.0 } out_loss, acc_, p_ = sess.run([total_loss, acc, probabilities], feed_dict=feed) with dev_summary_writer.as_default(): tf.summary.scalar('dev_loss', out_loss, step) tf.summary.scalar('dev_accuracy', acc_, step) print("loss :{}, acc :{}".format(out_loss, acc_)) return out_loss, p_, y # min_total_loss_dev = 999999 min_total_loss_dev = np.Inf step = 0 for epoch in range(config["num_train_epochs"]): _ = "{:*^100s}".format(("epoch-" + str(epoch)).center(20)) print(_) # 读取训练数据 total_loss_train = 0 # total_pre_train = [] # total_true_train = [] input_ids2, input_mask2, segment_ids2, labels2 = get_input_data( config["in_1"], seq_len, batch_size) for i in range(num_train_steps): step += 1 ids_train, mask_train, segment_train, y_train = sess.run( [input_ids2, input_mask2, segment_ids2, labels2]) out_loss, pre, y = train_step(ids_train, mask_train, segment_train, y_train, step) total_loss_train += out_loss # total_pre_train.extend(pre) # total_true_train.extend(y) if step % eval_per_step == 0 and step >= config[ "eval_start_step"]: total_loss_dev = 0 dev_input_ids2, dev_input_mask2, dev_segment_ids2, dev_labels2 = get_input_data( config["in_2"], seq_len, dev_batch_size, False) # total_pre_dev = [] # total_true_dev = [] for j in range(num_dev_steps): # 一个 epoch 的 轮数 ids_dev, mask_dev, segment_dev, y_dev = sess.run([ dev_input_ids2, dev_input_mask2, dev_segment_ids2, dev_labels2 ]) out_loss, pre, y = dev_step(ids_dev, mask_dev, segment_dev, y_dev, step) total_loss_dev += out_loss # total_pre_dev.extend(pre) # total_true_dev.extend(y_dev) print("total_loss_dev:{}".format(total_loss_dev)) # print(classification_report(total_true_dev, total_pre_dev, digits=4)) if total_loss_dev < min_total_loss_dev: print("save model:\t%f\t>%f" % (min_total_loss_dev, total_loss_dev)) min_total_loss_dev = total_loss_dev saver.save(sess, config["out"] + 'bert.ckpt', global_step=step) elif step < config[ "eval_start_step"] and step % config["auto_save"] == 0: saver.save(sess, config["out"] + 'bert.ckpt', global_step=step) _ = "{:*^100s}".format( ("epoch-" + str(epoch) + " report:").center(20)) print("total_loss_train:{}".format(total_loss_train)) # print(classification_report(total_true_train, total_pre_train, digits=4)) train_summary_writer.close() dev_summary_writer.close() sess.close() # remove dropout print("remove dropout in predict") tf.reset_default_graph() is_training = False input_ids = tf.placeholder(tf.int64, shape=[None, seq_len], name='input_ids') input_mask = tf.placeholder(tf.int64, shape=[None, seq_len], name='input_mask') segment_ids = tf.placeholder(tf.int64, shape=[None, seq_len], name='segment_ids') labels = tf.placeholder(tf.int64, shape=[None, seq_len], name='labels') keep_prob = tf.placeholder(tf.float32, name='keep_prob') # , name='is_training' bert_config_ = load_bert_config(config["bert_config"]) (total_loss, _, logits, probabilities) = create_model(bert_config_, is_training, input_ids, input_mask, segment_ids, labels, keep_prob, num_labels, use_one_hot_embeddings) init_global = tf.global_variables_initializer() saver = tf.train.Saver(tf.global_variables(), max_to_keep=1) # 保存最后top3模型 try: checkpoint = tf.train.get_checkpoint_state(config["out"]) input_checkpoint = checkpoint.model_checkpoint_path print("[INFO] input_checkpoint:", input_checkpoint) except Exception as e: input_checkpoint = config["out"] print("[INFO] Model folder", config["out"], repr(e)) with tf.Session() as sess: sess.run(init_global) saver.restore(sess, input_checkpoint) saver.save(sess, config["out_1"] + 'bert.ckpt') sess.close()
def main(_): assert FLAGS.model_dirs_or_checkpoints if not tf.gfile.Exists(FLAGS.output_dir): tf.gfile.MakeDirs(FLAGS.output_dir) if (FLAGS.operation == "average_last_n" and len(FLAGS.model_dirs_or_checkpoints) > 1): raise ValueError("Need only 1 directory for %s operation" % FLAGS.operation) checkpoints = [] for path in FLAGS.model_dirs_or_checkpoints: if tf.gfile.IsDirectory(path): # Grab the latest checkpoint for all the provided model dirs checkpoint_state = tf.train.get_checkpoint_state(path) if FLAGS.operation == "average_last_n": ckpt_paths = tf.io.gfile.glob(os.path.join(path, "model.ckpt*index")) def sort_fn(ckpt): return int(re.sub(".*ckpt-", "", ckpt)) ckpts = sorted([c.replace(".index", "") for c in ckpt_paths], key=sort_fn) checkpoints.extend(ckpts[-FLAGS.number_of_checkpoints:]) else: checkpoints.append(checkpoint_state.all_model_checkpoint_paths[-1]) else: if FLAGS.operation == "average_last_n": raise ValueError("need a directory while running %s operation" % FLAGS.operation) checkpoints.append(path) logging.info("Using checkpoints %s", checkpoints) if FLAGS.operation in ["ensemble", "average", "average_last_n"]: if len(checkpoints) == 1: raise ValueError("no point in ensebling/averaging one checkpoint") else: if len(checkpoints) != 1: raise ValueError( "operation %s requires exactly one checkpoint" % FLAGS.operation) var_values = {} var_dtypes = {} for i in range(0, len(checkpoints)): checkpoint = checkpoints[i] logging.info("loading checkpoint %s", checkpoint) reader = tf.train.load_checkpoint(checkpoint) var_list = tf.train.list_variables(checkpoint) for (name, _) in var_list: if i: assert name in var_values tensor = reader.get_tensor(name) assert tensor.dtype == var_dtypes[name] var_values[name].append(tensor) else: tensor = reader.get_tensor(name) var_dtypes[name] = tensor.dtype var_values[name] = [tensor] if not FLAGS.global_step: if name == "global_step": FLAGS.global_step = tensor logging.info("Read from checkpoint %s", checkpoint) # stack the list of tensors along the 0th dimension. for name, tensors in var_values.items(): tensor = tensors[0] if name == "global_step": new_val = np.int32(FLAGS.global_step) elif FLAGS.operation == "ensemble": new_val = np.stack(tensors) elif FLAGS.operation == "autoensemble": new_val = np.stack([tensor] * FLAGS.autoensemble_size) elif FLAGS.operation == "average" or FLAGS.operation == "average_last_n": new_val = average_tensors(tensors) elif FLAGS.operation == "extract_first": new_val = tensor[0] else: raise ValueError("unknown FLAGS.operation=%s" % FLAGS.operation) var_values[name] = new_val with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE): tf_vars = [ tf.get_variable(v, shape=var_values[v].shape, dtype=var_dtypes[v]) for v in var_values ] placeholders = [tf.placeholder(v.dtype, shape=v.shape) for v in tf_vars] assign_ops = [tf.assign(v, p) for (v, p) in zip(tf_vars, placeholders)] saver = tf.train.Saver(tf.all_variables()) output_file = "model.ckpt-" + str(FLAGS.global_step) output_path = os.path.join(FLAGS.output_dir, output_file) # Build a model consisting only of variables, set them to the average values. with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for p, assign_op, (name, value) in zip(placeholders, assign_ops, six.iteritems(var_values)): sess.run(assign_op, {p: value}) # Use the built saver to save the averaged checkpoint. saver.save(sess, output_path) logging.info("Transformed checkpoints saved in %s", output_path)
def main(unused_argv): logging.info("Loading %s", FLAGS.game_name) game = FLAGS.game_name num_players = FLAGS.num_players env_configs = {"players": num_players} env = rl_environment.Environment(game, **env_configs) info_state_size = env.observation_spec()["info_state"][0] num_actions = env.action_spec()["num_actions"] hidden_layers_sizes = [int(l) for l in FLAGS.hidden_layers_sizes] kwargs = { "replay_buffer_capacity": FLAGS.replay_buffer_capacity, "reservoir_buffer_capacity": FLAGS.reservoir_buffer_capacity, "min_buffer_size_to_learn": FLAGS.min_buffer_size_to_learn, "anticipatory_param": FLAGS.anticipatory_param, "batch_size": FLAGS.batch_size, "learn_every": FLAGS.learn_every, "rl_learning_rate": FLAGS.rl_learning_rate, "sl_learning_rate": FLAGS.sl_learning_rate, "optimizer_str": FLAGS.optimizer_str, "loss_str": FLAGS.loss_str, "update_target_network_every": FLAGS.update_target_network_every, "discount_factor": FLAGS.discount_factor, "epsilon_decay_duration": FLAGS.epsilon_decay_duration, "epsilon_start": FLAGS.epsilon_start, "epsilon_end": FLAGS.epsilon_end, } with tf.Session() as sess: # pylint: disable=g-complex-comprehension agents = [ nfsp.NFSP(sess, idx, info_state_size, num_actions, hidden_layers_sizes, **kwargs) for idx in range(num_players) ] joint_avg_policy = NFSPPolicies(env, agents, nfsp.MODE.average_policy) sess.run(tf.global_variables_initializer()) if FLAGS.use_checkpoints: for agent in agents: if agent.has_checkpoint(FLAGS.checkpoint_dir): agent.restore(FLAGS.checkpoint_dir) for ep in range(FLAGS.num_train_episodes): if (ep + 1) % FLAGS.eval_every == 0: losses = [agent.loss for agent in agents] logging.info("Losses: %s", losses) if FLAGS.evaluation_metric == "exploitability": # Avg exploitability is implemented only for 2 players constant-sum # games, use nash_conv otherwise. expl = exploitability.exploitability( env.game, joint_avg_policy) logging.info("[%s] Exploitability AVG %s", ep + 1, expl) elif FLAGS.evaluation_metric == "nash_conv": nash_conv = exploitability.nash_conv( env.game, joint_avg_policy) logging.info("[%s] NashConv %s", ep + 1, nash_conv) else: raise ValueError(" ".join( ("Invalid evaluation metric, choose from", "'exploitability', 'nash_conv'."))) if FLAGS.use_checkpoints: for agent in agents: agent.save(FLAGS.checkpoint_dir) logging.info("_____________________________________________") time_step = env.reset() while not time_step.last(): player_id = time_step.observations["current_player"] agent_output = agents[player_id].step(time_step) action_list = [agent_output.action] time_step = env.step(action_list) # Episode is over, step all agents with final info state. for agent in agents: agent.step(time_step)
def __init__( self, sess, n_actions, n_features, actor_lr=0.0001, critic_lr=0.0002, proposed_learning_rate=0.01, learn_scale=False, ): self.sess = sess self.n_actions = n_actions self.n_features = n_features self.actor_lr = actor_lr self.critic_lr = critic_lr self.proposed_learning_rate = proposed_learning_rate self.max_nn_output = 0 self.min_nn_output = 10 self.best_validation_loss = 10 self.obs = tf.placeholder(tf.float32, [None, n_features], 'observation') self.action = tf.placeholder(tf.float32, None, 'action') self.actor_adv = tf.placeholder(tf.float32, [None, 1], 'actor_advantage') self.learn_scale = learn_scale with tf.variable_scope('critic_scope'): layer1 = tf.layers.dense(self.obs, 32, tf.nn.relu) self.value = tf.layers.dense(layer1, 1) self.discounted_r = tf.placeholder(tf.float32, [None, 1], 'discounted_r') self.critic_adv = self.discounted_r - self.value self.critic_loss = tf.reduce_mean(tf.square(self.critic_adv)) self.critic_train_op = tf.train.AdamOptimizer(critic_lr).minimize( self.critic_loss) dist, dist_params = self.build_action_model('dist', trainable=True) dist_old, dist_old_params = self.build_action_model( 'dist_old', trainable=False) self.sample_action = tf.squeeze(dist.sample(1), axis=0) with tf.variable_scope('update_distribution'): self.update_dist_op = [ old_prob.assign(new_prob) for new_prob, old_prob in zip(dist_params, dist_old_params) ] with tf.variable_scope('actor_loss'): new_action_prob = dist.prob(self.action) + LOG_BASE old_action_prob = dist_old.prob(self.action) + LOG_BASE prob_ratio = tf.exp(tf.log(new_action_prob) - tf.log(old_action_prob)) surrogate_loss = prob_ratio * self.actor_adv self.actor_loss = -tf.reduce_mean( tf.minimum( surrogate_loss, tf.clip_by_value(prob_ratio, 1.0 - EPSILON, 1.0 + EPSILON) * self.actor_adv)) with tf.variable_scope('actor_train'): actor_optimizer = tf.train.AdamOptimizer(learning_rate=actor_lr) actor_gvs = actor_optimizer.compute_gradients(self.actor_loss) actor_capped_gvs = [(tf.clip_by_norm(grad, FLAGS.gradient_clipping_l2) if grad is not None else grad, var) for grad, var in actor_gvs] self.actor_train_op = actor_optimizer.apply_gradients(actor_capped_gvs) self.sess.run(tf.global_variables_initializer())
def demofunc(train_person_id, test_image_path): train_path = 'DataSet\\Features\\Training/training_' + train_person_id + '.csv' #testing(paths) testing(test_image_path) test_path = 'DataSet\\TestFeatures/testcsv.csv' #True def readCSV(train_path, test_path, type2=False): # Reading train data df = pd.read_csv(train_path, usecols=range(n_input)) train_input = np.array(df.values) train_input = train_input.astype( np.float32, copy=False) # Converting input to float_32 #Reading train data df = pd.read_csv(train_path, usecols=(n_input, )) temp = [elem[0] for elem in df.values] correct = np.array(temp) corr_train = keras.utils.to_categorical(correct, 2) # Converting to one # Reading test data df = pd.read_csv(test_path, usecols=range(n_input)) test_input = np.array(df.values) test_input = test_input.astype(np.float32, copy=False) if not (type2): df = pd.read_csv(test_path, usecols=(n_input, )) temp = [elem[0] for elem in df.values] correct = np.array(temp) corr_test = keras.utils.to_categorical(correct, 2) # Converting to one if not (type2): return train_input, corr_train, test_input, corr_test else: return train_input, corr_train, test_input ops.reset_default_graph() # Parameters learning_rate = 0.001 training_epochs = 1000 display_step = 1 # Network Parameters n_hidden_1 = 7 # 1st layer number of neurons n_hidden_2 = 10 # 2nd layer number of neurons n_hidden_3 = 30 # 3rd layer n_classes = 2 # no. of classes (genuine or forged) # tf Graph input X = tf.placeholder("float", [None, n_input]) Y = tf.placeholder("float", [None, n_classes]) # Store layers weight & bias weights = { 'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], seed=1)), 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3])), 'out': tf.Variable(tf.random_normal([n_hidden_1, n_classes], seed=2)) } biases = { 'b1': tf.Variable(tf.random_normal([n_hidden_1], seed=3)), 'b2': tf.Variable(tf.random_normal([n_hidden_2])), 'b3': tf.Variable(tf.random_normal([n_hidden_3])), 'out': tf.Variable(tf.random_normal([n_classes], seed=4)) } # Create model def multilayer_perceptron(x): layer_1 = tf.tanh((tf.matmul(x, weights['h1']) + biases['b1'])) layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3']) out_layer = tf.tanh(tf.matmul(layer_1, weights['out']) + biases['out']) return out_layer # Construct model logits = multilayer_perceptron(X) loss_op = tf.reduce_mean(tf.squared_difference(logits, Y)) #minimize the error optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op) # For accuracies pred = tf.nn.softmax(logits) correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # Initializing the variables init = tf.global_variables_initializer() def evaluate(train_path, test_path, type2=TRUE): if not (type2): train_input, corr_train, test_input, corr_test = readCSV( train_path, test_path) else: train_input, corr_train, test_input = readCSV( train_path, test_path, type2) ans = 'Random' with tf.Session() as sess: for epoch in range(training_epochs): _, cost = sess.run([train_op, loss_op], feed_dict={ X: train_input, Y: corr_train }) if cost < 0.0001: break if epoch % 999 == 0: print("Epoch:", '%04d' % (epoch + 1), "cost={:.9f}".format(cost)) #print("Optimization Finished!") # Finding accuracies accuracy1 = accuracy.eval({X: train_input, Y: corr_train}) print("Accuracy for train:", accuracy1) #print("Accuracy for test:", accuracy2) if type2 is False: accuracy2 = accuracy.eval({X: test_input, Y: corr_test}) print("Accuracy for test:", accuracy2) return accuracy1, accuracy2 else: prediction = pred.eval({X: test_input}) print(prediction[0][1]) print(prediction[0][0]) if prediction[0][1] > prediction[0][0]: #if prediction[0][1]>0.78: print('Genuine Image') strdemo = 'Genuine Image' return strdemo #return True else: print('Forged Image') strdemo = 'ForgedImage' return strdemo #return False #trainAndTest() result = evaluate(train_path, test_path, type2=True) lblResult = Label(topFrame, text=result) lblResult.place(x=120, y=460, anchor=CENTER)
def main(unused_argv): train_steps = FLAGS.train_steps num_episode = FLAGS.num_episode trainee_model = FLAGS.trainee_model best_validation_loss = 10 batch_size = 1000 train_size = 50000 valid_size = 10000 test_batch_num = 10 if trainee_model == 'resnet': test_size = 1000 else: test_size = 10000 fashion_mnist = keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() train_images = train_images / 255.0 test_images = test_images / 255.0 # Different input tensor shape for different trainee models. if trainee_model == 'mlp': num_pixels = train_images.shape[1] * train_images.shape[2] x_train = train_images.reshape(train_images.shape[0], num_pixels).astype('float32') train_set = x_train[0:train_size, :] train_label = train_labels[0:train_size].astype('int64') valid_set = x_train[train_size:, :] valid_label = train_labels[train_size:].astype('int64') test_set = test_images.reshape(test_images.shape[0], num_pixels).astype('float32') test_label = test_labels.astype('int64') elif trainee_model == 'cnn' or trainee_model == 'resnet': x_train = train_images.reshape(train_images.shape[0], train_images.shape[1], train_images.shape[2], 1).astype('float32') train_set = x_train[0:train_size, :, :, :] train_label = train_labels[0:train_size].astype('int64') valid_set = x_train[train_size:, :, :, :] valid_label = train_labels[train_size:].astype('int64') test_set = test_images.reshape(test_images.shape[0], test_images.shape[1], test_images.shape[2], 1).astype('float32') test_label = test_labels.astype('int64') init_observation = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]) observation_dim = init_observation.shape[0] adaptive_tuner_sess = tf.Session() adaptive_tuner = AdaptiveTuner( sess=adaptive_tuner_sess, n_features=observation_dim, n_actions=1, actor_lr=FLAGS.actor_learning_rate, critic_lr=FLAGS.critic_learning_rate, proposed_learning_rate=FLAGS.default_learning_rate) # Log the training process. running_reward = 0 valid_loss_matrix = np.zeros((num_episode, train_steps)) train_loss_matrix = np.zeros((num_episode, train_steps)) valid_accuracy_matrix = np.zeros((num_episode, train_steps)) test_loss_matrix = np.zeros((num_episode, train_steps)) test_accuracy_matrix = np.zeros((num_episode, train_steps)) reward_matrix = np.zeros((num_episode, train_steps)) observation_matrix = np.zeros((num_episode, train_steps, observation_dim)) learning_rate_matrix = np.zeros((num_episode, train_steps)) running_reward_array = np.zeros(num_episode) gnn = tf.Graph() with gnn.as_default(): # Prepare train/valid/test split for Fashion Mnist dataset = tf.data.Dataset.from_tensor_slices( (train_set, train_label)).repeat().batch(batch_size) train_iter = tf.data.make_one_shot_iterator(dataset) feature, label = train_iter.get_next() valid_dataset = tf.data.Dataset.from_tensor_slices( (valid_set, valid_label)).repeat().batch(valid_size) valid_iter = tf.data.make_one_shot_iterator(valid_dataset) valid_feature, valid_label = valid_iter.get_next() test_dataset = tf.data.Dataset.from_tensor_slices( (test_set, test_label)).repeat().batch(test_size) test_iter = tf.data.make_one_shot_iterator(test_dataset) test_feature, test_label = test_iter.get_next() learned_learning_rate = tf.placeholder(tf.float32, shape=[]) # Build trainee model. if trainee_model == 'mlp': train_logits = build_mlp_model(feature, reuse=False) valid_logits = build_mlp_model(valid_feature, reuse=True) test_logits = build_mlp_model(test_feature, reuse=True) elif trainee_model == 'cnn': train_logits = build_cnn_model(feature, reuse=False) valid_logits = build_cnn_model(valid_feature, reuse=True) test_logits = build_cnn_model(test_feature, reuse=True) elif trainee_model == 'resnet': resnet_size = 18 resnet_18 = resnet_model.FastCifar10Model( resnet_size=resnet_size, data_format='channels_first') train_logits = resnet_18(feature, True) train_logits = tf.cast(train_logits, tf.float32) valid_logits = resnet_18(valid_feature, False) valid_logits = tf.cast(valid_logits, tf.float32) test_logits = resnet_18(test_feature, False) test_logits = tf.cast(test_logits, tf.float32) else: raise ValueError('Wrong trainee_model flag value.') prediction = tf.nn.softmax(train_logits) valid_prediction = tf.nn.softmax(valid_logits) test_prediction = tf.nn.softmax(test_logits) if trainee_model == 'resnet': w1 = tf.get_default_graph().get_tensor_by_name( 'resnet_model/dense/kernel:0') w2 = tf.get_default_graph().get_tensor_by_name( 'resnet_model/dense/bias:0') else: w1 = tf.get_default_graph().get_tensor_by_name('w1/kernel:0') w2 = tf.get_default_graph().get_tensor_by_name('w2/kernel:0') mean_w1, var_w1 = tf.nn.moments(w1, axes=[0, 1]) if trainee_model == 'resnet': mean_w2, var_w2 = tf.nn.moments(w2, axes=[0]) else: mean_w2, var_w2 = tf.nn.moments(w2, axes=[0, 1]) loss = tf.losses.sparse_softmax_cross_entropy( labels=label, logits=train_logits) train_op = tf.train.AdamOptimizer( learning_rate=learned_learning_rate).minimize(loss) if trainee_model == 'resnet': update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) train_op = tf.group([train_op, update_ops]) valid_loss = tf.losses.sparse_softmax_cross_entropy( labels=valid_label, logits=valid_logits) valid_accuracy = compute_accuracy(valid_prediction, valid_label) test_loss = tf.losses.sparse_softmax_cross_entropy( labels=test_label, logits=test_logits) test_accuracy = compute_accuracy(test_prediction, test_label) for i_episode in range(num_episode): obs_t, action_t, reward_t, obs_t_1 = [], [], [], [] best_target_valid_loss = 10.0 with tf.Session(config=tf.ConfigProto(), graph=gnn) as sess: sess.run(tf.global_variables_initializer()) observation = init_observation prev_prediction = np.random.rand(batch_size, 10) prediction_log_var_ema = 0 prediction_change_log_var_ema = 0 reward = 0 num_reward = 0 average_reward = 0 reward_sum = 0 keep_lr_interval = 1 exp_decay = 0.8 action = 0.0 track_reward = [] adaptive_tuner.proposed_learning_rate = FLAGS.default_learning_rate for i in range(train_steps): if keep_lr_interval == 1 or i > train_steps - 10: action, action_multiplier = adaptive_tuner.choose_action(observation) keep_lr_interval = FLAGS.keep_lr_interval learning_rate_matrix[i_episode, i] = action _, loss_value, prediction_value, valid_loss_value, valid_accuracy_value, mean_w1_value, var_w1_value, mean_w2_value, var_w2_value = ( sess.run([ train_op, loss, prediction, valid_loss, valid_accuracy, mean_w1, var_w1, mean_w2, var_w2 ], feed_dict={learned_learning_rate: action})) if valid_loss_value < best_target_valid_loss: best_target_valid_loss = valid_loss_value if trainee_model == 'resnet': total_test_loss = 0.0 total_test_accuracy = 0.0 for _ in range(test_batch_num): batch_test_loss_value, batch_test_accuracy_value = sess.run( [test_loss, test_accuracy]) total_test_loss += batch_test_loss_value total_test_accuracy += batch_test_accuracy_value test_loss_value = total_test_loss / test_batch_num test_accuracy_value = total_test_accuracy / test_batch_num else: test_loss_value, test_accuracy_value = sess.run( [test_loss, test_accuracy]) test_loss_matrix[i_episode, i] = test_loss_value test_accuracy_matrix[i_episode, i] = test_accuracy_value train_loss_matrix[i_episode, i] = loss_value valid_loss_matrix[i_episode, i] = valid_loss_value valid_accuracy_matrix[i_episode, i] = valid_accuracy_value diff_prediction = prediction_value - prev_prediction if prediction_log_var_ema == 0: prediction_log_var_ema = np.log(np.var(prediction_value)) else: prediction_log_var_ema = exp_decay * prediction_log_var_ema + ( 1 - exp_decay) * np.log(np.var(prediction_value) + LOG_BASE) if prediction_change_log_var_ema == 0: prediction_change_log_var_ema = np.log(np.var(diff_prediction)) else: prediction_change_log_var_ema = exp_decay * prediction_change_log_var_ema + ( 1 - exp_decay) * np.log(np.var(diff_prediction) + LOG_BASE) # Collect all state observations. new_observation = np.array([ valid_loss_value, prediction_log_var_ema, prediction_change_log_var_ema, loss_value, mean_w1_value, var_w1_value, mean_w2_value, var_w2_value, action ]) observation_matrix[i_episode, i, :] = observation # Different reward functions. if FLAGS.reward_baseline == 'Fixed': reward = FLAGS.fixed_baseline_value - valid_loss_value elif FLAGS.reward_baseline == 'Average': reward_sum += valid_loss_value num_reward += 1 average_reward = float(reward_sum) / num_reward reward = average_reward - valid_loss_value elif FLAGS.reward_baseline == 'Exponential': if average_reward == 0: average_reward = valid_loss_value else: average_reward = average_reward * exp_decay + ( valid_loss_value * (1 - exp_decay)) reward = average_reward - valid_loss_value elif FLAGS.reward_baseline == 'Ratio': reward = FLAGS.reward_numerator / valid_loss_value - FLAGS.fixed_baseline_value else: raise ValueError('Wrong reward_baseline flag value.') reward_matrix[i_episode, i] = reward track_reward.append(reward) obs_t.append(observation) action_t.append(np.squeeze(action_multiplier)) reward_t.append(reward) obs_t_1.append(new_observation) observation = new_observation prev_prediction = prediction_value else: keep_lr_interval -= 1 sess.run([train_op], feed_dict={learned_learning_rate: action}) if i == train_steps - 1: current_best_validation_loss = valid_loss_matrix[i_episode, i] if current_best_validation_loss < best_validation_loss: best_validation_loss = current_best_validation_loss obs_t_ts, action_t_ts, reward_t_ts, obs_t_1_ts = np.vstack( obs_t), np.vstack(action_t), np.vstack(reward_t), np.vstack( obs_t_1) value_t = adaptive_tuner.get_value(obs_t_1_ts) td_reward = reward_t_ts + GAMMA * value_t adaptive_tuner.update(obs_t_ts, action_t_ts, td_reward) obs_t, action_t, reward_t, obs_t_1 = [], [], [], [] episode_reward = sum(track_reward) if running_reward == 0: running_reward = episode_reward else: running_reward = running_reward * 0.99 + episode_reward * 0.01 running_reward_array[i_episode] = running_reward
def AdaBatch(gpu_id, input_reader, model_type, training_epochs, batch_size, lr_boundaries, lr_values, optimizer_type, update_method, warm_up_period, s_e=100.0, pretrain=0, log_dir="log"): if not os.path.exists(log_dir): os.makedirs(log_dir) text_log = [] text_log.append( "epoch, time(s), learning rate, minibatch loss, minibatch error, test loss, test error" ) num_train_images = input_reader.num_train_images num_val_images = input_reader.num_val_images num_label = input_reader.num_classes image_shape = [input_reader.width, input_reader.height, input_reader.depth] train_batch_patcher = patcher.BatchPatcher(num_train_images, batch_size, num_label, s_e=s_e, update_method=update_method) validation_batch_patcher = patcher.BatchPatcher( num_val_images, batch_size, num_label, update_method=update_method) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.visible_device_list = str(gpu_id) config.gpu_options.allow_growth = True graph = tf.Graph() with graph.as_default(): with tf.device('/gpu:' + str(gpu_id)): with tf.Session(config=config) as sess: # Input Graph Generation ############################################################################# t_ids, t_images, t_labels = input_reader.data_read(batch_size, train=True) v_ids, v_images, v_labels = input_reader.data_read(batch_size, train=False) # Model Graph Construction ########################################################################### if model_type == "DenseNet-25-12": model = DenseNet(25, 12, image_shape, num_label, batch_size, batch_size) elif model_type == "WideResNet16-8": model = WideResNet(16, 8, image_shape, num_label, batch_size, batch_size) train_loss_op, train_accuracy_op, train_op, _, train_distance_op = model.build_train_op( lr_boundaries, lr_values, optimizer_type) test_loss_op, test_accuracy_op, _ = model.build_test_op() # Data load in memeory ############################################################################### print("start to load data set.") coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) train_batch_patcher.bulk_load_in_memory( sess, t_ids, t_images, t_labels) validation_batch_patcher.bulk_load_in_memory( sess, v_ids, v_images, v_labels) start_time = time.time() # Model Initialization ########################################################################### # init params: we share the initial epochs. See paper. if pretrain != 0: start_time = time.time() saver = tf.train.Saver() file_dir = "init_weight/" + input_reader.dataset_name + "/" + model_type + "_" + optimizer_type + "_lr=" + str( lr_values[0]) + "_e=" + str(pretrain) + "/" minus_start_time = 0 with open(file_dir + "text_log.csv") as f: for line in f: print(line, end="") text_log.append(line.rstrip()) minus_start_time = line.split(",")[1] start_time = start_time - float(minus_start_time) saver.restore(sess, file_dir + "model.ckpt") for i in range(train_batch_patcher.num_iters_per_epoch): ids, images, labels = train_batch_patcher.get_init_mini_batch( i) distance = sess.run(train_distance_op, feed_dict={ model.train_image_placeholder: images, model.train_label_placeholder: labels }) train_batch_patcher.update_prob_table(ids, distance) print(train_batch_patcher.prob_table.table) print("shared weight is successfully loaded") else: sess.run(tf.global_variables_initializer()) # Traing Process ##################################################################################### for epoch in range(pretrain, training_epochs): if epoch < warm_up_period: is_warm_up = True else: is_warm_up = False # (1) Mini-batch loss and error along with netowrk updates avg_mini_loss = 0.0 avg_mini_acc = 0.0 for i in range(train_batch_patcher.num_iters_per_epoch): # Next batch depends on the method: {Ada_Boundary, Ada-Hard, Ada-Uniform} ids, images, labels = train_batch_patcher.get_next_mini_batch( num_of_sample=batch_size, is_warm_up=is_warm_up) mini_loss, mini_acc, _, distance = sess.run( [ train_loss_op, train_accuracy_op, train_op, train_distance_op ], feed_dict={ model.train_image_placeholder: images, model.train_label_placeholder: labels }) train_batch_patcher.update_prob_table(ids, distance) avg_mini_loss += mini_loss avg_mini_acc += mini_acc avg_mini_loss /= train_batch_patcher.num_iters_per_epoch avg_mini_acc /= train_batch_patcher.num_iters_per_epoch # (2) Compute training loss and error avg_train_loss = 0.0 avg_train_acc = 0.0 for i in range(train_batch_patcher.num_iters_per_epoch): ids, images, labels = train_batch_patcher.get_init_mini_batch( i) train_loss, train_acc = sess.run( [test_loss_op, test_accuracy_op], feed_dict={ model.test_image_placeholder: images, model.test_label_placeholder: labels }) avg_train_loss += train_loss avg_train_acc += train_acc avg_train_loss /= train_batch_patcher.num_iters_per_epoch avg_train_acc /= train_batch_patcher.num_iters_per_epoch # (3) Validation (or test) loss and error avg_val_loss = 0.0 avg_val_acc = 0.0 for i in range( validation_batch_patcher.num_iters_per_epoch): ids, images, labels = validation_batch_patcher.get_init_mini_batch( i) val_loss, val_acc = sess.run( [test_loss_op, test_accuracy_op], feed_dict={ model.test_image_placeholder: images, model.test_label_placeholder: labels }) avg_val_loss += val_loss avg_val_acc += val_acc avg_val_loss /= validation_batch_patcher.num_iters_per_epoch avg_val_acc /= validation_batch_patcher.num_iters_per_epoch # Log Writing #################################################################################### cur_lr = sess.run(model.learning_rate) print((epoch + 1), ", ", int(time.time() - start_time), ", ", cur_lr, ", ", avg_mini_loss, ", ", (1.0 - avg_mini_acc), ", ", avg_train_loss, ", ", (1.0 - avg_train_acc), ", ", avg_val_loss, ", ", (1.0 - avg_val_acc)) text_log.append( str(epoch + 1) + ", " + str(int(time.time() - start_time)) + ", " + str(cur_lr) + ", " + str(avg_mini_loss) + ", " + str(1.0 - avg_mini_acc) + ", " + str(avg_train_loss) + ", " + str(1.0 - avg_train_acc) + ", " + str(avg_val_loss) + ", " + str(1.0 - avg_val_acc)) coord.request_stop() coord.join(threads) sess.close() # Log Flushing f = open(log_dir + "/text_log.csv", "w") for text in text_log: f.write(text + "\n") f.close()
def testPool(self, pooling_method): batch = 2 depth = 3 height = 4 width = 6 channels = 3 tf.random.set_random_seed(1234) inputs = tf.random_normal([batch, depth, height, width, channels]) stride_d = 3 stride_h = 2 stride_w = 3 graph = mtf.Graph() mesh = mtf.Mesh(graph, "my_mesh") batch_dim = mtf.Dimension("batch", batch) depth_dim = mtf.Dimension("depth", depth) height_dim = mtf.Dimension("height", height) width_dim = mtf.Dimension("width", width) channels_dim = mtf.Dimension("channels", channels) mtf_inputs = mtf.import_tf_tensor(mesh, inputs, shape=mtf.Shape([ batch_dim, depth_dim, height_dim, width_dim, channels_dim ])) if pooling_method == "MAX_2D": mtf_outputs = mtf.layers.max_pool2d(mtf_inputs, ksize=(stride_h, stride_w)) inputs = tf.reshape(inputs, [batch * depth, height, width, channels]) expected_outputs = tf.keras.layers.MaxPooling2D( (stride_h, stride_w))(inputs) expected_outputs = tf.reshape(expected_outputs, [ batch, depth, int(height / stride_h), int(width / stride_w), channels ]) elif pooling_method == "AVG_2D": mtf_outputs = mtf.layers.avg_pool2d(mtf_inputs, ksize=(stride_h, stride_w)) inputs = tf.reshape(inputs, [batch * depth, height, width, channels]) expected_outputs = tf.keras.layers.AveragePooling2D( (stride_h, stride_w))(inputs) expected_outputs = tf.reshape(expected_outputs, [ batch, depth, int(height / stride_h), int(width / stride_w), channels ]) elif pooling_method == "MAX_3D": mtf_outputs = mtf.layers.max_pool3d( mtf_inputs, ksize=[stride_d, stride_h, stride_w]) expected_outputs = tf.keras.layers.MaxPooling3D( [stride_d, stride_h, stride_w])(inputs) elif pooling_method == "AVG_3D": mtf_outputs = mtf.layers.avg_pool3d( mtf_inputs, ksize=[stride_d, stride_h, stride_w]) expected_outputs = tf.keras.layers.AveragePooling3D( [stride_d, stride_h, stride_w])(inputs) mtf_gradient = mtf.gradients([mtf_outputs], [mtf_inputs])[0] mesh_impl = mtf.placement_mesh_impl.PlacementMeshImpl(shape=[], layout={}, devices=[""]) lowering = mtf.Lowering(graph, {mesh: mesh_impl}) actual_outputs = lowering.export_to_tf_tensor(mtf_outputs) actual_gradient = lowering.export_to_tf_tensor(mtf_gradient) tf_group = lowering.copy_masters_to_slices() init = tf.global_variables_initializer() self.evaluate(init) self.evaluate(tf_group) actual, expected = self.evaluate([actual_outputs, expected_outputs]) self.assertAllClose(actual, expected) actual = self.evaluate(actual_gradient) if pooling_method == "MAX_2D": expected_non_zeros = batch * depth * height * width * channels / ( stride_h * stride_w) self.assertEqual(np.count_nonzero(actual), expected_non_zeros) elif pooling_method == "AVG_2D": expected = np.ones((batch, depth, height, width, channels), dtype=np.float32) / stride_h / stride_w self.assertAllClose(actual, expected) elif pooling_method == "MAX_3D": expected_non_zeros = batch * depth * height * width * channels / ( stride_d * stride_h * stride_w) self.assertEqual(np.count_nonzero(actual), expected_non_zeros) elif pooling_method == "AVG_3D": expected = np.ones( (batch, depth, height, width, channels), dtype=np.float32) / stride_d / stride_h / stride_w self.assertAllClose(actual, expected)
def fit(self, X, y): """Train the classifier and adversary (if ``debias == True``) with the given training data. Args: X (pandas.DataFrame): Training samples. y (array-like): Training labels. Returns: self """ if tf.executing_eagerly(): raise RuntimeError("AdversarialDebiasing does not work in eager " "execution mode. To fix, add `tf.disable_eager_execution()`" " to the top of the calling script.") X, y, _ = check_inputs(X, y) rng = check_random_state(self.random_state) ii32 = np.iinfo(np.int32) s1, s2, s3, s4 = rng.randint(ii32.min, ii32.max, size=4) tf.reset_default_graph() self.sess_ = tf.Session() groups, self.prot_attr_ = check_groups(X, self.prot_attr) le = LabelEncoder() y = le.fit_transform(y) self.classes_ = le.classes_ # BUG: LabelEncoder converts to ndarray which removes tuple formatting groups = groups.map(str) groups = le.fit_transform(groups) self.groups_ = le.classes_ n_classes = len(self.classes_) n_groups = len(self.groups_) # use sigmoid for binary case if n_classes == 2: n_classes = 1 if n_groups == 2: n_groups = 1 n_samples, n_features = X.shape with tf.variable_scope(self.scope_name): # Setup placeholders self.input_ph = tf.placeholder(tf.float32, shape=[None, n_features]) self.prot_attr_ph = tf.placeholder(tf.float32, shape=[None, 1]) self.true_labels_ph = tf.placeholder(tf.float32, shape=[None, 1]) self.keep_prob = tf.placeholder(tf.float32) # Create classifier with tf.variable_scope('classifier_model'): W1 = tf.get_variable( 'W1', [n_features, self.classifier_num_hidden_units], initializer=tf.initializers.glorot_uniform(seed=s1)) b1 = tf.Variable(tf.zeros( shape=[self.classifier_num_hidden_units]), name='b1') h1 = tf.nn.relu(tf.matmul(self.input_ph, W1) + b1) h1 = tf.nn.dropout(h1, rate=1-self.keep_prob, seed=s2) W2 = tf.get_variable( 'W2', [self.classifier_num_hidden_units, n_classes], initializer=tf.initializers.glorot_uniform(seed=s3)) b2 = tf.Variable(tf.zeros(shape=[n_classes]), name='b2') self.classifier_logits_ = tf.matmul(h1, W2) + b2 # Obtain classifier loss if self.classifier_logits_.shape[1] == 1: clf_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( labels=self.true_labels_ph, logits=self.classifier_logits_)) else: clf_loss = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=tf.squeeze(tf.cast(self.true_labels_ph, tf.int32)), logits=self.classifier_logits_)) if self.debias: # Create adversary with tf.variable_scope("adversary_model"): c = tf.get_variable('c', initializer=tf.constant(1.0)) s = tf.sigmoid((1 + tf.abs(c)) * self.classifier_logits_) W2 = tf.get_variable('W2', [3, n_groups], initializer=tf.initializers.glorot_uniform(seed=s4)) b2 = tf.Variable(tf.zeros(shape=[n_groups]), name='b2') self.adversary_logits_ = tf.matmul( tf.concat([s, s * self.true_labels_ph, s * (1. - self.true_labels_ph)], axis=1), W2) + b2 # Obtain adversary loss if self.adversary_logits_.shape[1] == 1: adv_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( labels=self.prot_attr_ph, logits=self.adversary_logits_)) else: adv_loss = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=tf.squeeze(tf.cast(self.prot_attr_ph, tf.int32)), logits=self.adversary_logits_)) global_step = tf.Variable(0., trainable=False) init_learning_rate = 0.001 if self.adversary_loss_weight is not None: learning_rate = tf.train.exponential_decay(init_learning_rate, global_step, 1000, 0.96, staircase=True) else: learning_rate = tf.train.inverse_time_decay(init_learning_rate, global_step, 1000, 0.1, staircase=True) # Setup optimizers clf_opt = tf.train.AdamOptimizer(learning_rate) if self.debias: adv_opt = tf.train.AdamOptimizer(learning_rate) clf_vars = [var for var in tf.trainable_variables() if 'classifier_model' in var.name] if self.debias: adv_vars = [var for var in tf.trainable_variables() if 'adversary_model' in var.name] # Compute grad wrt classifier parameters adv_grads = {var: grad for (grad, var) in adv_opt.compute_gradients(adv_loss, var_list=clf_vars)} normalize = lambda x: x / (tf.norm(x) + np.finfo(np.float32).tiny) clf_grads = [] for (grad, var) in clf_opt.compute_gradients(clf_loss, var_list=clf_vars): if self.debias: unit_adv_grad = normalize(adv_grads[var]) # proj_{adv_grad} clf_grad: grad -= tf.reduce_sum(grad * unit_adv_grad) * unit_adv_grad if self.adversary_loss_weight is not None: grad -= self.adversary_loss_weight * adv_grads[var] else: grad -= tf.sqrt(global_step) * adv_grads[var] clf_grads.append((grad, var)) clf_min = clf_opt.apply_gradients(clf_grads, global_step=global_step) if self.debias: with tf.control_dependencies([clf_min]): adv_min = adv_opt.minimize(adv_loss, var_list=adv_vars) self.sess_.run(tf.global_variables_initializer()) # Begin training for epoch in range(self.num_epochs): shuffled_ids = rng.permutation(n_samples) for i in range(n_samples // self.batch_size): batch_ids = shuffled_ids[self.batch_size * i: self.batch_size * (i+1)] batch_features = X.iloc[batch_ids] batch_labels = y[batch_ids][:, np.newaxis] batch_prot_attr = groups[batch_ids][:, np.newaxis] batch_feed_dict = {self.input_ph: batch_features, self.true_labels_ph: batch_labels, self.prot_attr_ph: batch_prot_attr, self.keep_prob: 0.8} if self.debias: _, _, clf_loss_val, adv_loss_val = self.sess_.run( [clf_min, adv_min, clf_loss, adv_loss], feed_dict=batch_feed_dict) if i % 200 == 0 and self.verbose: print("epoch {:>3d}; iter: {:>4d}; batch classifier" " loss: {:.4f}; batch adversarial loss: " "{:.4f}".format(epoch, i, clf_loss_val, adv_loss_val)) else: _, clf_loss_val = self.sess_.run([clf_min, clf_loss], feed_dict=batch_feed_dict) if i % 200 == 0 and self.verbose: print("epoch {:>3d}; iter: {:>4d}; batch classifier" " loss: {:.4f}".format(epoch, i, clf_loss_val)) return self
def testComputeAreaFeatures2D(self): features = tf.constant([[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]], [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1], [9.1, 10.1], [11.1, 12.1]]], dtype=tf.float32) area_mean, area_std, area_sum, area_height, area_widths = ( area_attention.compute_area_features(features, max_area_width=3, max_area_height=2, height=2, epsilon=0.)) with self.test_session() as session: session.run(tf.global_variables_initializer()) res1, _, res3, res4, res5 = session.run([area_mean, area_std, area_sum, area_height, area_widths]) expected_means = [[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [2, 3], [4, 5], [8, 9], [10, 11], [3, 4], [9, 10], [4, 5], [6, 7], [8, 9], [5, 6], [7, 8], [6, 7]], [[1.1, 2.1], [3.1, 4.1], [5.1, 6.1], [7.1, 8.1], [9.1, 10.1], [11.1, 12.1], [2.1, 3.1], [4.1, 5.1], [8.1, 9.1], [10.1, 11.1], [3.1, 4.1], [9.1, 10.1], [4.1, 5.1], [6.1, 7.1], [8.1, 9.1], [5.1, 6.1], [7.1, 8.1], [6.1, 7.1]]] self.assertAllClose(expected_means, res1, msg="mean_1d") expected_heights = [[[1], [1], [1], [1], [1], [1], # 1x2 [1], [1], [1], [1], # 1x3 [1], [1], # 2x1 [2], [2], [2], # 2x2 [2], [2], # 2x3 [2]], [[1], [1], [1], [1], [1], [1], # 1x2 [1], [1], [1], [1], # 1x3 [1], [1], # 2x1 [2], [2], [2], # 2x2 [2], [2], # 2x3 [2]]] self.assertAllEqual(expected_heights, res4, msg="height_1d") expected_widths = [[[1], [1], [1], [1], [1], [1], # 1x2 [2], [2], [2], [2], # 1x3 [3], [3], # 2x1 [1], [1], [1], # 2x2 [2], [2], # 2x3 [3]], [[1], [1], [1], [1], [1], [1], # 1x2 [2], [2], [2], [2], # 1x3 [3], [3], # 2x1 [1], [1], [1], # 2x2 [2], [2], # 2x3 [3]]] self.assertAllEqual(expected_widths, res5, msg="width_1d") sizes = np.multiply(np.array(expected_heights), np.array(expected_widths)) expected_sums = np.multiply(np.array(expected_means), sizes) self.assertAllClose(expected_sums, res3, msg="sum_1d")
relation_network_units = [x for x in args.network if x.isnumeric()] relation_network_activations = [ x for x in args.network if not x.isnumeric() ] model = MHOPLoP( dataset=dataset, embedding=embedding, relation_network_units=relation_network_units, relation_network_activations=relation_network_activations, max_hops=args.hops, batch_size=args.batch_size, patience=args.patience, learning_rate=args.learning_rate) session.run(tensorflow.global_variables_initializer()) weights = model.fit(save_weights=args.save) tensorflow.reset_default_graph() with tensorflow.Session(config=config) as session: relation_network_units = [x for x in args.network if x.isnumeric()] relation_network_activations = [ x for x in args.network if not x.isnumeric() ] model = MHOPLoP(dataset=dataset, embedding=embedding, relation_network_units=relation_network_units,
def __init__(self, A, X, Y, num_hidden_feat, learning_rate=5e-2, gamma=1e-3, idx_gpu="/gpu:2"): self.num_hidden_feat = num_hidden_feat self.learning_rate = learning_rate self.gamma = gamma with tf.Graph().as_default() as g: self.graph = g with tf.device(idx_gpu): # definition of constant matrices self.A = convert_coo_to_sparse_tensor(A.tocoo()) self.X = tf.constant(X, dtype=tf.float32) self.Y = tf.constant(Y, dtype=tf.float32) self.W0 = tf.get_variable( "W0", shape=[X.shape[1], self.num_hidden_feat], initializer=tf.keras.layers.xavier_initializer(), ) self.W1 = tf.get_variable( "W1", shape=[self.num_hidden_feat, Y.shape[1]], initializer=tf.keras.layers.xavier_initializer(), ) # placeholder definition self.idx_nodes = tf.placeholder(tf.int32) self.keep_prob = tf.placeholder(tf.float32) # model definition self.l_input = tf.nn.dropout(self.X, self.keep_prob) self.X0_tilde = tf.sparse_tensor_dense_matmul( self.A, self.l_input) self.X0 = tf.matmul(self.X0_tilde, self.W0) self.X0 = tf.nn.relu(self.X0) self.X0 = tf.nn.dropout(self.X0, self.keep_prob) self.X1_tilde = tf.sparse_tensor_dense_matmul(self.A, self.X0) self.logits = tf.matmul(self.X1_tilde, self.W1) self.l_out = tf.gather(self.logits, self.idx_nodes) self.c_Y = tf.gather(self.Y, self.idx_nodes) # loss function definition self.l2_reg = tf.nn.l2_loss(self.W0) self.data_loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=self.l_out, labels=self.c_Y)) self.loss = self.data_loss + self.gamma * self.l2_reg # solver definition self.optimizer = tf.train.AdamOptimizer( learning_rate=self.learning_rate) self.opt_step = self.optimizer.minimize(self.loss) # predictions and accuracy extraction self.c_predictions = tf.argmax(tf.nn.softmax(self.l_out), 1) self.accuracy = tf.contrib.metrics.accuracy( self.c_predictions, tf.argmax(self.c_Y, 1)) # gradients computation self.trainable_variables = tf.trainable_variables() self.var_grad = tf.gradients(self.loss, tf.trainable_variables()) self.norm_grad = frobenius_norm( tf.concat([tf.reshape(g, [-1]) for g in self.var_grad], 0)) # session creation config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True self.session = tf.Session(config=config) # session initialization init = tf.global_variables_initializer() self.session.run(init)
def build_and_initialize(cfg, sess, categories, mode=common.ModeKeys.TRAIN): """Builds and initializes all parts of the graph. Parameters ---------- cfg : OmegaConf The experiment configuration. sess : tf.Session The TF session used for executing the computation graph. categories : dict of lists of Categories Each list of Categories is used to construct meta-datasets. mode : str, optional (default: common.ModeKeys.TRAIN) Defines the mode of the computation graph (TRAIN or EVAL). Note: this is likely to be removed from the API down the line. Returns ------- exp : Experiment An object that represents the experiment. Contains `meta_learners`, `samplers`, and `task_dists`. """ # Build and initialize data pools. data_pools = { task.set_name: datasets.get_datapool( dataset_name=cfg.data.name, categories=categories[task.set_name], name=f"DP_{task.log_dir.replace('/', '_')}", ) .build(**cfg.data.build_config) .initialize(sess) for task in cfg[mode].tasks } # Build meta-dataset. meta_datasets = { task.set_name: datasets.get_metadataset( dataset_name=cfg.data.name, data_pool=data_pools[task.set_name], batch_size=cfg[mode].meta.batch_size, name=f"MD_{task.log_dir.replace('/', '_')}", **cfg[mode].dataset, ).build() for task in cfg[mode].tasks } # Build model. model = models.get( dataset_name=cfg.data.name, num_classes=cfg[mode].dataset.num_classes, **cfg.model, ) # Build optimizer. optimizer = optimizers.get(**cfg.train.optimizer) # Build task distributions. task_dists = [ tasks.get_distribution( meta_dataset=meta_datasets[task.set_name], name_suffix=task.log_dir.replace("/", "_"), **task.config, ) for task in cfg[mode].tasks ] # Build meta-learners. meta_learners = [ adaptation.get( model=model, optimizer=optimizer, mode=mode, tasks=task_dists[i].task_batch, **cfg.adapt, ) for i, task in enumerate(cfg[mode].tasks) ] # Build samplers. samplers_list = [ samplers.get( learner=meta_learners[i], tasks=task_dists[i].task_batch, **task.sampler ) for i, task in enumerate(cfg[mode].tasks) ] # Run global init. sess.run(tf.global_variables_initializer()) # Initialize task distribution. for task_dist, sampler in zip(task_dists, samplers_list): task_dist.initialize(sampler=sampler, sess=sess) return Experiment( meta_learners=meta_learners, samplers=samplers_list, task_dists=task_dists )
def run_model(opts): training = opts.test_mode in ["all", "training"] testing = opts.test_mode in ["all", "tests"] # Use Keras to get the dataset: mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 # Sizes/shapes for the dataset: image_shape = x_train.shape[1:] num_pixels = image_shape[0] * image_shape[1] batch_size = 16 num_train = y_train.shape[0] num_test = y_test.shape[0] data_shape = [None, num_pixels] w_dense_shape = [num_pixels, h1Size] assert (batch_size % block_size[0] == 0) assert (w_dense_shape[0] % block_size[1] == 0) assert (w_dense_shape[1] % block_size[2] == 0) block_rows = w_dense_shape[0] // block_size[1] block_cols = w_dense_shape[1] // block_size[2] sparsity_mask = None if opts.sparsity >= 0.0: sparsity_mask = utils.create_random_sparse_mask( opts.sparsity, block_rows, block_cols).flatten() # Flatten the images and cast the labels: x_train_flat = x_train.astype(np.float32).reshape(-1, num_pixels) x_test_flat = x_test.astype(np.float32).reshape(-1, num_pixels) y_train = y_train.astype(np.int32) y_test = y_test.astype(np.int32) # Decide how to split epochs into loops up front: epochs = opts.epochs ipu_steps_per_epoch = 15 batches_per_epoch = num_train // batch_size train_batches = (num_train * epochs) // batch_size test_batches = num_test // batch_size batches_per_step = batches_per_epoch // ipu_steps_per_epoch if not batches_per_epoch % ipu_steps_per_epoch == 0: raise ValueError( f"IPU steps per epoch {ipu_steps_per_epoch} must divide batches per epoch {batches_per_epoch}." ) # Put placeholders on the CPU host: with tf.device("cpu"): place_x = tf.placeholder(dtype=tf.float32, shape=data_shape, name="input") place_y = tf.placeholder(dtype=tf.int32, shape=[None], name="label") lr_placeholder = tf.placeholder(tf.float32, shape=[]) # Create dataset and IPU feeds: dataset = tf.data.Dataset.from_tensor_slices((place_x, place_y)) dataset = dataset.cache().repeat().batch(batch_size, drop_remainder=True) infeed_train_queue = ipu_infeed_queue.IPUInfeedQueue( dataset, feed_name="train_infeed") outfeed_train_queue = ipu_outfeed_queue.IPUOutfeedQueue( feed_name="train_outfeed") infeed_test_queue = ipu_infeed_queue.IPUInfeedQueue( dataset, feed_name="test_infeed") outfeed_test_queue = ipu_outfeed_queue.IPUOutfeedQueue( feed_name="test_outfeed") # Use function binding to create all the builder functions that are neeeded: if training: bound_train_model = partial(model, lr_placeholder, outfeed_train_queue, True, sparsity_mask) bound_train_loop = partial(loop_builder, batches_per_step, bound_train_model, infeed_train_queue) if testing: bound_test_model = partial(model, lr_placeholder, outfeed_test_queue, False, sparsity_mask) bound_test_loop = partial(loop_builder, test_batches, bound_test_model, infeed_test_queue) # Use the bound builder functions to place the model on the IPU: with scopes.ipu_scope("/device:IPU:0"): if training: train_loop = ipu_compiler.compile(bound_train_loop, inputs=[]) if testing: test_loop = ipu_compiler.compile(bound_test_loop, inputs=[]) # Initialisers should go on the CPU: with tf.device("cpu"): metrics_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="metrics") metrics_initializer = tf.variables_initializer(var_list=metrics_vars) saver = tf.train.Saver() # Setup and acquire an IPU device: config = ipu_utils.create_ipu_config() config = ipu_utils.auto_select_ipus(config, 1) ipu_utils.configure_ipu_system(config) # These allow us to retrieve the results of IPU feeds: if training: dequeue_train_outfeed = outfeed_train_queue.dequeue() if testing: dequeue_test_outfeed = outfeed_test_queue.dequeue() # Create a benchmark program for the infeed to determine maximum achievable throughput: infeed_perf = dataset_benchmark.infeed_benchmark(infeed_train_queue, epochs, num_train, True) print( f"\nImage shape: {image_shape} Training examples: {num_train} Test examples: {num_test}" ) print( f"Epochs: {epochs} Batch-size: {batch_size} Steps-per-epoch: {ipu_steps_per_epoch} Batches-per-step: {batches_per_step}" ) # Run the model: with tf.Session() as sess: print(f"Benchmarking the infeed...") sess.run(infeed_perf, feed_dict={ place_x: x_train_flat, place_y: y_train }) sess.run(tf.global_variables_initializer()) sess.run(infeed_train_queue.initializer, feed_dict={ place_x: x_train_flat, place_y: y_train }) if training: print(f"Training...") progress = tqdm( range(epochs), bar_format='{desc} Epoch: {n_fmt}/{total_fmt} {bar}') for e in progress: sess.run(metrics_initializer) for i in range(ipu_steps_per_epoch): sess.run(train_loop, feed_dict={lr_placeholder: scheduler(e)}) result = sess.run(dequeue_train_outfeed) if len(result['mean_loss'] != 0) and len( result['acc'] != 0): progress.set_description( f"Loss {result['mean_loss'][0]:.5f} Accuracy {result['acc'][0]:.5f}" ) print(f"Saving...") saver.save(sess, "model") if testing: print(f"Testing...") sess.run(metrics_initializer) sess.run(infeed_test_queue.initializer, feed_dict={ place_x: x_test_flat, place_y: y_test }) sess.run(test_loop) result = sess.run(dequeue_test_outfeed) test_loss = np.mean(result['mean_loss']) test_acc = np.mean(result['acc']) print(f"Test loss: {test_loss:.8f} Test accuracy: {test_acc:.8f}")
with tf.name_scope('correct_prediction'): # 分别将预测和真实的标签中取出最大值的索引,弱相同则返回1(true),不同则返回0(false) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) with tf.name_scope('accuracy'): # 求均值即为准确率 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar('accuracy', accuracy) # summaries合并 merged = tf.summary.merge_all() # 写到指定的磁盘路径中 train_writer = tf.summary.FileWriter(log_dir + '/train', sess.graph) test_writer = tf.summary.FileWriter(log_dir + '/test') # 运行初始化所有变量 tf.global_variables_initializer().run() #### 如何merge的情况: for i in range(max_steps): if i % 10 == 0: # 记录测试集的summary与accuracy summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False)) test_writer.add_summary(summary, i) print('Accuracy at step %s: %s' % (i, acc)) else: # 记录训练集的summary if i % 100 == 99: # Record execution stats run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True), options=run_options,
def main(_): # TensorBoard의 summaries를 write할 directory를 설정한다. if tf.gfile.Exists(FLAGS.summaries_dir): tf.gfile.DeleteRecursively(FLAGS.summaries_dir) tf.gfile.MakeDirs(FLAGS.summaries_dir) # pre-trained graph를 생성한다. maybe_download_and_extract() graph, bottleneck_tensor, jpeg_data_tensor, resized_image_tensor = ( create_inception_graph()) # 폴더 구조를 살펴보고, 모든 이미지에 대한 lists를 생성한다. image_lists = create_image_lists(FLAGS.image_dir, FLAGS.testing_percentage, FLAGS.validation_percentage) class_count = len(image_lists.keys()) if class_count == 0: print('No valid folders of images found at ' + FLAGS.image_dir) return -1 if class_count == 1: print('Only one valid folder of images found at ' + FLAGS.image_dir + ' - multiple classes are needed for classification.') return -1 # 커맨드라인 flag에 distortion에 관련된 설정이 있으면 distortion들을 적용한다. do_distort_images = should_distort_images( FLAGS.flip_left_right, FLAGS.random_crop, FLAGS.random_scale, FLAGS.random_brightness) with tf.Session(graph=graph) as sess: if do_distort_images: # 우리는 distortion들을 적용할것이다. 따라서 필요한 연산들(operations)을 설정한다. (distorted_jpeg_data_tensor, distorted_image_tensor) = add_input_distortions( FLAGS.flip_left_right, FLAGS.random_crop, FLAGS.random_scale, FLAGS.random_brightness) else: # 우리는 계산된 'bottleneck' 이미지 summaries를 가지고 있다. # 이를 disk에 캐싱(caching)할 것이다. cache_bottlenecks(sess, image_lists, FLAGS.image_dir, FLAGS.bottleneck_dir, jpeg_data_tensor, bottleneck_tensor) # 우리가 학습시킬(training) 새로운 layer를 추가한다. (train_step, cross_entropy, bottleneck_input, ground_truth_input, final_tensor) = add_final_training_ops(len(image_lists.keys()), FLAGS.final_tensor_name, bottleneck_tensor) # 우리의 새로운 layer의 정확도를 평가(evalute)하기 위한 새로운 operation들을 생성한다. evaluation_step, prediction = add_evaluation_step( final_tensor, ground_truth_input) # 모든 summaries를 합치고(merge) summaries_dir에 쓴다.(write) merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train', sess.graph) validation_writer = tf.summary.FileWriter( FLAGS.summaries_dir + '/validation') # 우리의 모든 가중치들(weights)과 그들의 초기값들을 설정한다. init = tf.global_variables_initializer() sess.run(init) # 커맨드 라인에서 지정한 횟수만큼 학습을 진행한다. for i in range(FLAGS.how_many_training_steps): # bottleneck 값들의 batch를 얻는다. 이는 매번 distortion을 적용하고 계산하거나, # disk에 저장된 chache로부터 얻을 수 있다. if do_distort_images: (train_bottlenecks, train_ground_truth) = get_random_distorted_bottlenecks( sess, image_lists, FLAGS.train_batch_size, 'training', FLAGS.image_dir, distorted_jpeg_data_tensor, distorted_image_tensor, resized_image_tensor, bottleneck_tensor) else: (train_bottlenecks, train_ground_truth, _) = get_random_cached_bottlenecks( sess, image_lists, FLAGS.train_batch_size, 'training', FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor, bottleneck_tensor) # grpah에 bottleneck과 ground truth를 feed하고, training step을 진행한다. # TensorBoard를 위한 'merged' op을 이용해서 training summaries을 capture한다. train_summary, _ = sess.run( [merged, train_step], feed_dict={bottleneck_input: train_bottlenecks, ground_truth_input: train_ground_truth}) train_writer.add_summary(train_summary, i) # 일정 step마다 graph의 training이 얼마나 잘 되고 있는지 출력한다. is_last_step = (i + 1 == FLAGS.how_many_training_steps) if (i % FLAGS.eval_step_interval) == 0 or is_last_step: train_accuracy, cross_entropy_value = sess.run( [evaluation_step, cross_entropy], feed_dict={bottleneck_input: train_bottlenecks, ground_truth_input: train_ground_truth}) print('%s: Step %d: Train accuracy = %.1f%%' % (datetime.now(), i, train_accuracy * 100)) print('%s: Step %d: Cross entropy = %f' % (datetime.now(), i, cross_entropy_value)) validation_bottlenecks, validation_ground_truth, _ = ( get_random_cached_bottlenecks( sess, image_lists, FLAGS.validation_batch_size, 'validation', FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor, bottleneck_tensor)) # validation step을 진행한다. # TensorBoard를 위한 'merged' op을 이용해서 training summaries을 capture한다. validation_summary, validation_accuracy = sess.run( [merged, evaluation_step], feed_dict={bottleneck_input: validation_bottlenecks, ground_truth_input: validation_ground_truth}) validation_writer.add_summary(validation_summary, i) print('%s: Step %d: Validation accuracy = %.1f%% (N=%d)' % (datetime.now(), i, validation_accuracy * 100, len(validation_bottlenecks))) # 트레이닝 과정이 모두 끝났다. # 따라서 이전에 보지 못했던 이미지를 통해 마지막 test 평가(evalution)을 진행한다. test_bottlenecks, test_ground_truth, test_filenames = ( get_random_cached_bottlenecks(sess, image_lists, FLAGS.test_batch_size, 'testing', FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor, bottleneck_tensor)) test_accuracy, predictions = sess.run( [evaluation_step, prediction], feed_dict={bottleneck_input: test_bottlenecks, ground_truth_input: test_ground_truth}) print('Final test accuracy = %.1f%% (N=%d)' % ( test_accuracy * 100, len(test_bottlenecks))) if FLAGS.print_misclassified_test_images: print('=== MISCLASSIFIED TEST IMAGES ===') for i, test_filename in enumerate(test_filenames): if predictions[i] != test_ground_truth[i].argmax(): print('%70s %s' % (test_filename, list(image_lists.keys())[predictions[i]])) # 학습된 graph와 weights들을 포함한 labels를 쓴다.(write) output_graph_def = graph_util.convert_variables_to_constants( sess, graph.as_graph_def(), [FLAGS.final_tensor_name]) with gfile.FastGFile(FLAGS.output_graph, 'wb') as f: f.write(output_graph_def.SerializeToString()) with gfile.FastGFile(FLAGS.output_labels, 'w') as f: f.write('\n'.join(image_lists.keys()) + '\n')
for i in range(env.player_num): agent = NFSPAgent(sess, scope='nfsp' + str(i), action_num=env.action_num, state_shape=env.state_shape, hidden_layers_sizes=[128, 128], min_buffer_size_to_learn=1000, q_replay_memory_init_size=memory_init_size, q_update_target_estimator_every=norm_step, q_mlp_layers=[128, 128]) agents.append(agent) # with sess.as_default(): #uncomment when loading # saver = tf.train.Saver() # saver.restore(sess, tf.train.latest_checkpoint(save_dir)) sess.run(tf.global_variables_initializer()) # comment out when loading env.set_agents(agents) # Setup all nfsp agents into training environments # Setup random agent for evaluation random_agent = RandomAgent(action_num=eval_env.action_num) eval_env.set_agents([agents[0], random_agent]) ### Step 3: Generate game data and train the agents. ### episode_num = 500 # set the episode number step_counters = [0 for _ in range(env.player_num) ] # Count the number of steps # Init a Logger to plot the learning curve logger = Logger(log_path) for episode in range(episode_num):
def run_training(opts, transformer, x_train, y_train): # Calculate dataset length num_train = len(y_train) batches_per_epoch = num_train // opts.batch_size batches_per_step = batches_per_epoch // (opts.steps_per_epoch) total_steps = (opts.steps_per_epoch) * opts.nepochs logging.info( f"Batches per epoch: {batches_per_epoch} Batches per step: {batches_per_step}" ) if not batches_per_epoch % (opts.steps_per_epoch) == 0: raise ValueError( f"IPU steps per epoch {opts.steps_per_epoch} must divide batches per epoch {batches_per_epoch} exactly." ) # Construct the training graph training_graph = tf.Graph() with training_graph.as_default(): with tf.device("cpu"): input_shape = [None, *x_train.shape[1:]] place_x = tf.placeholder(dtype=opts.dtype, shape=input_shape, name="input") place_y = tf.placeholder(dtype=tf.int32, shape=[None], name="label") lr_placeholder = tf.placeholder(opts.dtype, shape=[]) # Create dataset and IPU feeds: dataset = tf.data.Dataset.from_tensor_slices((place_x, place_y)) dataset = dataset.shuffle(buffer_size=len(y_train), reshuffle_each_iteration=True, seed=opts.random_seed).cache() dataset = dataset.repeat().batch(opts.batch_size, drop_remainder=True) # Queues for streaming from host to device and back train_infeed = IPUInfeedQueue(dataset) train_outfeed = IPUOutfeedQueue() png_outfeed = IPUOutfeedQueue() # Helper function def loop_builder(iterations, builder_func, infeed): return loops.repeat(iterations, builder_func, [], infeed) # Compile the forward and backward pass for training with scopes.ipu_scope("/device:IPU:0"): train_loop = partial(forward_pass, opts, transformer, lr_placeholder, batches_per_step, True, train_outfeed, png_outfeed) train_loop = partial(loop_builder, batches_per_step, train_loop, train_infeed) train_loop = ipu_compiler.compile(train_loop, inputs=[]) transformer.buildSparsityUpdateOps() # Metrics with tf.device("cpu"): metrics_vars = tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES, scope="metrics") metrics_initializer = tf.variables_initializer( var_list=metrics_vars) saver = tf.train.Saver(max_to_keep=5) # These ops are declared here so that the graph can be frozen afterwards global_initializer = tf.global_variables_initializer() train_outfeed_dequeue = train_outfeed.dequeue() png_outfeed_dequeue = png_outfeed.dequeue() # Setup and acquire an IPU device: config = IPUConfig() config.auto_select_ipus = opts.num_shards config.configure_ipu_system() logpath = os.path.join(opts.train_checkpoint_path, "train") summary_writer = tf.summary.FileWriter(logpath) # Run the model: training_graph.finalize() # no more new ops added from here on out with tf.Session(graph=training_graph) as sess: logger.info(f"Creating training session") sess.run(global_initializer) sess.run(train_infeed.initializer, feed_dict={ place_x: x_train, place_y: y_train }) progress = tqdm(range(opts.nepochs), bar_format='{desc} Epoch: {n_fmt}/{total_fmt} {bar}') for e in progress: for i in range(opts.steps_per_epoch): # Train the model sess.run(metrics_initializer) dt = time.perf_counter() sess.run(train_loop, feed_dict={ lr_placeholder: learning_rate_schedule(e, opts) }) dt = time.perf_counter() - dt session_outputs = sess.run(train_outfeed_dequeue) logger.debug(f"Train outputs: {session_outputs}") # Calculate avg throughput num_tokens = transformer.source_sequence_length * batches_per_step * opts.batch_size throughput = num_tokens / dt desc = f"Loss {session_outputs['mean_loss'][-1]:.5f} " \ f"Accuracy {session_outputs['acc'][-1]:.5f} " \ f"Iteration: {session_outputs['iteration'][-1]}" progress.set_description( desc + f" Throughput {throughput:.1f} token/s") # Perform pruning (if using RigL the dense grads from session_outputs are used) step = 1 + i + e * (opts.steps_per_epoch) if transformer.prune_ratio is not None: t0 = time.perf_counter() png_results = sess.run(png_outfeed_dequeue) t1 = time.perf_counter() for k in png_results: png_results[k] = png_results[k][-1] logger.debug( f"Prune and grow outputs: {png_results.keys()}") logger.info( f"Downloaded the prune and grow data from Device to Host in {t1-t0:0.3f} seconds" ) transformer.syncPruneAndRegrowOnHost( opts.cosine_prune_schedule, step, total_steps, png_results) transformer.streamSparsityFromHostToDevice() # Save at the end of each epoch logger.info(f"Saving model") saver.save(sess, os.path.join(opts.train_checkpoint_path, 'model.ckpt'))