################################## main() ######################################### # load model path (./chekckpoint) load_model_path = './checkpoint/test5_pkembed/seqGAN_ours' tf.reset_default_graph() random.seed(SEED) np.random.seed(SEED) gen_data_loader = Gen_Data_loader(BATCH_SIZE, SEQ_LENGTH) vocab_size = len(vocab_to_int) # 6447 print(vocab_size) dis_data_loader = Dis_dataloader(BATCH_SIZE, SEQ_LENGTH) generator = Generator(vocab_size, BATCH_SIZE, EMB_DIM, HIDDEN_DIM, SEQ_LENGTH, START_TOKEN) discriminator = Discriminator(sequence_length=SEQ_LENGTH, num_classes=2, word_embedding_matrix=word_embedding_matrix, embedding_size=dis_embedding_dim, filter_sizes=dis_filter_sizes, num_filters=dis_num_filters, l2_reg_lambda=dis_l2_reg_lambda) rollout = ROLLOUT(generator, 0.8, word_embedding_matrix) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) print('#########################################################################') print('Restore Trained Seqgan parameters...') saver.restore(sess, load_model_path)
def main(): random.seed(SEED) np.random.seed(SEED) # data loaders declaration # loaders for generator, discriminator, and additional validation data loader gen_data_loader = Gen_Data_loader(BATCH_SIZE) dis_data_loader = Dis_dataloader(BATCH_SIZE) eval_data_loader = Gen_Data_loader(BATCH_SIZE) # define generator and discriminator # general structures are same with the original model # learning rates for generator needs heavy tuning for general use # l2 reg for D & G also affects performance generator = Generator(vocab_size, BATCH_SIZE, EMB_DIM, HIDDEN_DIM, SEQ_LENGTH, START_TOKEN) discriminator = Discriminator(sequence_length=SEQ_LENGTH, num_classes=2, vocab_size=vocab_size, embedding_size=dis_embedding_dim, filter_sizes=dis_filter_sizes, num_filters=dis_num_filters, l2_reg_lambda=dis_l2_reg_lambda) # VRAM limitation for efficient deployment显存限制有效的部署 tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True #动态分配GPU sess = tf.Session(config=tf_config) #当我们训练自己的神经网络的时候,无一例外的就是都会加上一句 sess.run(tf.global_variables_initializer()) , #这行代码的官方解释是 初始化模型的参数 sess.run(tf.global_variables_initializer()) # define saver # #tf里面提供模型保存的是tf.train.Saver()模块 #在创建这个Saver对象的时候,有一个参数我们经常会用到,就是 max_to_keep 参数, #这个是用来设置保存模型的个数,默认为5,即 max_to_keep=5,保存最近的5个模型。 #当然,如果你只想保存最后一代的模型,则只需要将max_to_keep设置为1即可 saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=1) #这个用来存 预训练模型 # generate real data from the true dataset gen_data_loader.create_batches(positive_file) # generate real validation data from true validation dataset eval_data_loader.create_batches(valid_file) log = open(path + '/save/experiment-log.txt', 'w') #log文本创建 if config['pretrain'] == True: # pre-train generator print('Start pre-training...' ) ############################################ 预训练开始了 log.write('pre-training...\n') for epoch in xrange( PRE_GEN_EPOCH ): ######################################### 生成器的预训练 # calculate the loss by running an epoch loss = pre_train_epoch( sess, generator, gen_data_loader ) ##该函数了调用了generator的云训练方法,其实就是run了预训练的loss、grad张量 # measure bleu score with the validation set 测量 在验证机上的 bleu分, 为什么要这么做? bleu_score = calculate_bleu( sess, generator, eval_data_loader ) #是看预训练好的generator和eval做bleu是么,只是以bleu这个指标有些简单了。 # since the real data is the true data distribution, only evaluate the pretraining loss # 因为真实的数据是 真实的数据分布, 所以只评估预训练的loss (?) 什么意思 # note the absence of the oracle model which is meaningless for general use buffer = 'pre-train epoch: ' + str( epoch) + ' pretrain_loss: ' + str(loss) + ' bleu: ' + str( bleu_score) print(buffer) log.write(buffer) # generate 5 test samples per epoch 每个周期生成5个样本?? # it automatically samples from the generator and postprocess to midi file 这个自动生成的样本是来自 生成器和后期处理 # midi files are saved to the pre-defined folder midi文件存在了预先定义的文件里 if epoch == 0: #第一个周期,则标号-1 generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file) POST.main(negative_file, 5, -1) elif epoch == PRE_GEN_EPOCH - 1: generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file) POST.main(negative_file, 5, -PRE_GEN_EPOCH) #否则,标号-周期,表示是预训练, 其实也就存了第一个和最后一个对吧 print( 'Start pre-training discriminator...' ) ######################################################## 鉴别器的预训练 # Train 3 epoch on the generated data and do this for 50 times 执行50次, 每次三个epoch (实际上是40次) # this trick is also in spirit of the original work, but the epoch strategy needs tuning for epochs in range(PRE_DIS_EPOCH): # for xxx times generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file) #生成样本??和上面的用了一个函数 这个生成是干嘛的呢?? #生成样本,用当前的generator生成一个样本,然后让鉴别器去评价,是这样么??? 对的,然后把生成的放在 negative_file文件夹下 dis_data_loader.load_train_data( positive_file, negative_file) #然后读取上面生成的负例 正例, 用作训练 D D_loss = 0 # D的loss, 目标函数的标准 for _ in range(3): #epoch是3 dis_data_loader.reset_pointer() #指针重置 for it in xrange(dis_data_loader.num_batch): #for循环 x_batch, y_batch = dis_data_loader.next_batch( ) #下一个batch, X和Y应该就是正和负吧 feed = { discriminator.input_x: x_batch, # discriminator.input_y: y_batch, # discriminator.dropout_keep_prob: dis_dropout_keep_prob #dropout保留的数目 } _ = sess.run(discriminator.train_op, feed) #run(图, 形参),这个只是单纯的算一遍吧 D_loss += discriminator.loss.eval( feed, session=sess) ## 在绘画中求loss的值 #上面这句 , 这个loss是上一个loss,还是这次的又执行了一次? 是执行了整个计算图,应该是算了第二遍了! #你可以使用sess.run()在同一步获取多个tensor中的值,使用Tensor.eval()时只能在同一步当中获取一个tensor值, #并且每次使用 eval 和 run时,都会执行整个计算图 buffer = 'epoch: ' + str(epochs + 1) + ' D loss: ' + str( D_loss / dis_data_loader.num_batch / 3) print(buffer) log.write(buffer) # save the pre-trained checkpoint for future use # if one wants adv. training only, comment out the pre-training section after the save save_checkpoint(sess, saver, PRE_GEN_EPOCH, PRE_DIS_EPOCH) #用saver来保存当前的sess 的信息 ############################################################################################ G 和 D 的预训练结束了 ######################################################################## 下面这个rollout重要!!!! 是强化学习里的操作,于G有关 # define rollout target object 定义 rollout 目标 object, 简称了 TO # the second parameter specifies target update rate 第二个参数指定了 TO的更新率 # the higher rate makes rollout "conservative", with less update from the learned generator # 上面这句, 高的rate让 rollout 更加保守 , 伴随 低的更新 来自已经学习了的生成器 # we found that higher update rate stabilized learning, constraining divergence of the generator #我们发现 高的 更新率 稳定了学习, 约束了 生成器的 分歧 ?? 这句话不是太明白 rollout = ROLLOUT( generator, 0.9 ) #两个参数,一个是生成器, 说明rollout是来自于generator的,于G有明显关系,第二个参数 是 TO的更新率,效果在上面 print( '#########################################################################' ) print('Start Adversarial Training...') log.write('adversarial training...\n') ########################################################################################################开始正经的 对抗训练!!! if config['pretrain'] == False: # load checkpoint of pre-trained model load_checkpoint(sess, saver) for total_batch in range(TOTAL_BATCH): #TOTAL_BATCH, epoch总数, 原本的是2000? G_loss = 0 # Train the generator for one step 训练G 的 one step for it in range(epochs_generator): # 一次 one step 的 epoch samples = generator.generate(sess) #得到样本 #通过强化学习,得到reward,这个reward是seqGAN的特点,用来消除离散不能在gan当中的负面影响,但是具体怎么做的呢? #sample是通过当前的G得到的样本, 给rollout,计算除了rewards rewards = rollout.get_reward(sess, samples, config['rollout_num'], discriminator) #参数是,当前的X,样本,以及rollout的reward,奖励?? feed = {generator.x: samples, generator.rewards: rewards} #开始正式的run,计算loss吧,这里又是先run了一次 _ = sess.run(generator.g_updates, feed_dict=feed) # 这个loss是后算的,给G_loss叠加的 G_loss += generator.g_loss.eval(feed, session=sess) # Update roll-out parameters 更新rollout的参数 rollout.update_params() # Train the discriminator # 训练 D ############################################################################## 其实这里就和pre训练的时候一样 D_loss = 0 for _ in range(epochs_discriminator): # D的 epoch, 默认是5 generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file) # 用当前的G 生成负样本 dis_data_loader.load_train_data( positive_file, negative_file) #把负样本,正样本都提取出来,当作 D 的feed for _ in range(3): # 每次都做三遍 dis_data_loader.reset_pointer() #指针重置 for it in xrange(dis_data_loader.num_batch): x_batch, y_batch = dis_data_loader.next_batch() feed = { discriminator.input_x: x_batch, discriminator.input_y: y_batch, discriminator.dropout_keep_prob: dis_dropout_keep_prob } _ = sess.run( discriminator.train_op, feed) #一般的LOSS是在run后面就返回的,我也不知道为毛这块是分开,并且分两次求loss,计算两次 D_loss += discriminator.loss.eval(feed, session=sess) # measure stability and performance evaluation with bleu score buffer = 'epoch: ' + str(total_batch+1) + \ ', G_adv_loss: %.12f' % (G_loss/epochs_generator) + \ ', D loss: %.12f' % (D_loss/epochs_discriminator/3) + \ ', bleu score: %.12f' % calculate_bleu(sess, generator, eval_data_loader) #在这里,bleu有没有用到呢,有没有作用在D的训练中 #貌似没有,只是一个标准,而且没有用在训练当中 print(buffer) log.write(buffer) # generate random test samples and postprocess the sequence to midi file #生成一个负例,和一个音乐吧 generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file + "_EP_" + str(total_batch)) #生成负样本,有什么用,不知道。。。 POST.main(negative_file + "_EP_" + str(total_batch), 5, total_batch) #生成音乐 log.close()
def run_training( # pylint: disable=too-many-arguments, too-many-locals learnsteps, train_freq, batch_size, exploration_fraction, early_exit_factor, num_processing_steps, latent_size, num_layers, seedgen, rl_seed, experiment_name, prioritized, prioritized_replay_alpha, prioritized_replay_beta0, prioritized_replay_beta_iters, prioritized_replay_eps, learning_starts, buffer_size, lr, gamma, grad_norm_clipping, target_network_update_freq, features, generator_args, restart_reward, success_reward, additional_timeslot_reward, ): """Trains the agent with the given hyperparameters""" parallel_gen = ParallelGenerator(Generator(**generator_args), seedgen) env = gym_environment.WSNEnvironment( features=features, early_exit_factor=early_exit_factor, seedgen=seedgen, problem_generator=parallel_gen.new_instance, restart_reward=restart_reward, success_reward=success_reward, additional_timeslot_reward=additional_timeslot_reward, ) git_label = _git_describe() time_label = datetime.datetime.now().isoformat() logdir = f"logs/{time_label}-{git_label}-{experiment_name}" logger.configure(dir=logdir, format_strs=["stdout", "csv", "tensorboard"]) with open(f"{logdir}/config.pkl", "wb") as config_file: dill.dump(features, config_file, protocol=4) # needs to be lambda since the scope at constructor time is used # pylint: disable=unnecessary-lambda q_model = lambda inp: EdgeQNetwork( edge_filter_idx=POSSIBLE_IDX, num_processing_steps=num_processing_steps, latent_size=latent_size, num_layers=num_layers, # ignore medatadata features during learning ignore_first_edge_features=2, )(inp) assert TIMESLOT_IDX < 2 and POSSIBLE_IDX < 2 learn( env, q_model, make_obs_ph=lambda name: env.observation_space.to_placeholders(), as_is=True, dueling=False, prioritized=prioritized, prioritized_replay_alpha=prioritized_replay_alpha, prioritized_replay_beta0=prioritized_replay_beta0, prioritized_replay_beta_iters=prioritized_replay_beta_iters, prioritized_replay_eps=prioritized_replay_eps, print_freq=1, train_freq=train_freq, batch_size=batch_size, exploration_fraction=exploration_fraction, learning_starts=learning_starts, buffer_size=buffer_size, lr=lr, gamma=gamma, grad_norm_clipping=grad_norm_clipping, target_network_update_freq=target_network_update_freq, checkpoint_freq=1000, eval_freq=1000, eval_hook=partial(_eval_hook, features=features), seed=rl_seed, total_timesteps=learnsteps * train_freq, checkpoint_path=logger.get_dir(), after_step_callback=save_episode_result_callback, )
from player import Player from computer import Computer import menu from time import time, sleep import globals if __name__ == "__main__": logging.basicConfig(filename='rats.log', level=logging.INFO) if config.enable_menu: menu.show_menu() pygame.init() player = Player() generator = Generator(player) game = model.Model(generator) view = render.View(game.map.size) measure_time_period = 20 measure_time_phase = 0 speed = config.initial_speed running = True while running: if measure_time_phase == 0: total_time = 0.0 game_time = 0.0 measure_time_phase = (measure_time_phase + 1) % measure_time_period start_time = time() game.step() view.render(game) for event in pygame.event.get():
def test_unicode(self): g = Generator(os.path.join(SAMPLES_DIR, 'example3', 'slides.rst')) g.execute() s = g.render() self.assertTrue(s.find('<pre>') != -1) self.assertEquals(len(re.findall('<pre><span', s)), 3)
def forward(self, input): out = self.main(input) out = out.view(-1, 64 * CONVT_SIZE) out = self.linear(out) return out if __name__ == '__main__': NOISE_DIM = 100 CONVT_SIZE = 128 LAMBDA = 10 DISC_ITERS = 5 BATCH_SIZE = 64 NUM_EPOCHS = 100 G = Generator(NOISE_DIM) D = Discriminator() # optimizers adam_d = optim.Adam(D.parameters(), lr=1e-4) adam_g = optim.Adam(G.parameters(), lr=1e-4) # set gpu flag use_cuda = torch.cuda.is_available() if use_cuda: gpu = 0 if use_cuda: D = D.cuda(gpu) G = G.cuda(gpu) # values to set gradient sign
def main(): #Get user input parser = create_parser() files, vocab_size, seq_length, gen_n, disc_n, adv_n, MODEL_STRING = assign_parser_args(parser.parse_args()) # Initialize the random seed #random.seed(SEED) #np.random.seed(SEED) #tf.set_random_seed(SEED) assert START_TOKEN == 0 tf.logging.set_verbosity(tf.logging.ERROR) # Initialize the data loaders gen_data_loader = Gen_Data_loader(BATCH_SIZE, seq_length) likelihood_data_loader = Gen_Data_loader(BATCH_SIZE, seq_length) # For testing dis_data_loader = Dis_dataloader(BATCH_SIZE, seq_length) dis_test_data_loader = Dis_dataloader(BATCH_SIZE, seq_length) # For testing # Initialize the Generator generator = Generator(vocab_size, BATCH_SIZE, EMB_DIM, HIDDEN_DIM, seq_length, START_TOKEN) # Initialize the Discriminator discriminator = Discriminator(sequence_length=seq_length, num_classes=2, vocab_size=vocab_size, embedding_size=dis_embedding_dim, filter_sizes=dis_filter_sizes, num_filters=dis_num_filters, l2_reg_lambda=dis_l2_reg_lambda) # Set session configurations. config = tf.ConfigProto() config.gpu_options.allow_growth = True saver = tf.train.Saver() sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) # If restoring from a previous run .... if len(os.listdir("./"+MODEL_STRING)) > 0: saver.restore(sess, tf.train.latest_checkpoint(MODEL_STRING)) # Create batches from the positive file. gen_data_loader.create_batches(files["positive_file"]) # Open log file for writing log = open(files['log_file'], 'w') # Pre_train the generator with MLE. pre_train_generator(sess, saver, MODEL_STRING, generator, gen_data_loader, likelihood_data_loader, files, log, gen_n) print('Start pre-training discriminator...') # Do the discriminator pre-training steps train_discriminator(sess, generator, discriminator, dis_data_loader, dis_test_data_loader, files, log, disc_n) # Do the adversarial training steps rollout = ROLLOUT(generator, 0.8) train_adversarial(sess, saver, MODEL_STRING, generator, discriminator, rollout, dis_data_loader, dis_test_data_loader, likelihood_data_loader, files, log, adv_n) #Use the best model to generate final sample saver.restore(sess, tf.train.latest_checkpoint(MODEL_STRING)) generate_samples(sess, generator, BATCH_SIZE, generated_num, files["eval_file"]) # Calculate the BLEUscore int_to_word = json.load(open(files["int2word"], 'r')) generated = datautil.int_file_to_text_ls(open(files["eval_file"], 'r'), int_to_word) references = datautil.int_file_to_text_ls(open(files["test_file"], 'r'), int_to_word) generated = datautil.remove_filler(generated) references = datautil.remove_filler(references) blue = nltk.translate.bleu_score.corpus_bleu([references]*len(generated), generated) print("Run with args {} {} {}: BLEUscore = {}\n".format(gen_n, disc_n, adv_n, blue)) if files == synth_files: total_correct = 0 for sentence in generated: if datautil.is_phrase_valid_passive(sentence) or datautil.is_phrase_valid_active(sentence): total_correct +=1 prop = total_correct/len(generated) if not os.path.exists("./synth/results.txt"): os.mknod("./synth/results.txt") with open("./synth/results.txt", 'a') as f: outblue = "synth run {} {} {}: BLEUscore = {}\n".format(gen_n, disc_n, adv_n, blue) f.write(outblue) out = "synth run {} {} {}: Proportion Valid = {}\n".format(gen_n, disc_n, adv_n, prop) f.write(out) f.close() log.close()
def _build_GAN(self): self.initializer = tf.contrib.layers.xavier_initializer with tf.variable_scope('gan'): # discriminator input from real data self._X = self.inputs(self._hps.batch_size, self.s_size) # tf.placeholder(dtype=tf.float32, name='X', # shape=[None, self._hps.dis_input_size]) # noise vector (generator input) self._Z = tf.placeholder(dtype=tf.float32, name='Z', shape=[None, self._hps.gen_input_size]) self._Z_sample = tf.placeholder( dtype=tf.float32, name='Z', shape=[None, self._hps.gen_input_size]) self.discriminator_inner = Discriminator( self._hps, scope='discriminator_inner') self.discriminator = Discriminator(self._hps) self.generator = Generator(self._hps) # Generator self.G_sample = self.generator.generate(self._Z) self.G_sample_test = self.generator.generate(self._Z_sample) # Discriminator D_real, D_logit_real = self.discriminator.discriminate(self._X) D_fake, D_logit_fake = self.discriminator.discriminate( self.G_sample) # Inner Discriminator D_in_fake, D_in_logit_fake = self.discriminator_inner.discriminate( self.G_sample) D_in_real, D_in_logit_real = self.discriminator_inner.discriminate( self._X) self.D_in_fake = D_in_fake with tf.variable_scope('D_loss'): D_loss_real = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( logits=D_logit_real, labels=tf.ones_like(D_logit_real))) D_loss_fake = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( logits=D_logit_fake, labels=tf.zeros_like(D_logit_fake))) self._D_loss = D_loss_real + D_loss_fake tf.summary.scalar('D_loss_real', D_loss_real, collections=['Dis']) tf.summary.scalar('D_loss_fake', D_loss_fake, collections=['Dis']) tf.summary.scalar('D_loss', self._D_loss, collections=['Dis']) tf.summary.scalar('D_out', tf.reduce_mean(D_logit_fake), collections=['Dis']) with tf.variable_scope('D_in_loss'): D_in_loss_fake = tf.reduce_mean( tf.losses.mean_squared_error(predictions=D_in_logit_fake, labels=D_logit_fake)) D_in_loss_real = tf.reduce_mean( tf.losses.mean_squared_error(predictions=D_in_logit_real, labels=D_logit_real)) self._D_in_loss = D_in_loss_fake + D_in_loss_real tf.summary.scalar('D_in_loss', self._D_in_loss, collections=['Dis_in']) tf.summary.scalar('D_in_out', tf.reduce_mean(D_in_logit_fake), collections=['Dis_in']) with tf.variable_scope('G_loss'): self._G_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits( logits=D_in_logit_fake, labels=tf.ones_like(D_in_logit_fake))) tf.summary.scalar('G_loss', self._G_loss, collections=['Gen']) with tf.variable_scope('GAN_Eval'): tf.logging.info(self.G_sample_test.shape) eval_fake_images = tf.image.resize_images(self.G_sample_test, [28, 28]) eval_real_images = tf.image.resize_images(self._X[:20], [28, 28]) self.eval_score = util.mnist_score(eval_fake_images, MNIST_CLASSIFIER_FROZEN_GRAPH) self.frechet_distance = util.mnist_frechet_distance( eval_real_images, eval_fake_images, MNIST_CLASSIFIER_FROZEN_GRAPH) tf.summary.scalar('MNIST_Score', self.eval_score, collections=['All']) tf.summary.scalar('frechet_distance', self.frechet_distance, collections=['All'])
def train_gan(args): # prepare dataloader dataloader = create_data_loader(args) # set up device device = torch.device('cuda:0' if (torch.cuda.is_available() and args.ngpu>0) else 'cpu') # Create & setup generator netG = Generator(args).to(device) # handle multiple gpus if (device.type == 'cuda' and args.ngpu>1): netG = nn.DataParallel(netG, list(range(args.ngpu))) # load from checkpoint if available if args.netG: netG.load_state_dict(torch.load(args.netG)) # initialize network with random weights else: netG.apply(weights_init) # Create & setup discriminator netD = Discriminator(args).to(device) # handle multiple gpus if (device.type == 'cuda' and args.ngpu>1): netD = nn.DataParallel(netD, list(range(args.ngpu))) # load from checkpoint if available if args.netD: netD.load_state_dict(torch.load(args.netD)) # initialize network with random weights else: netD.apply(weights_init) # setup up loss & optimizers criterion = nn.BCELoss() optimizerG = optim.Adam(netG.parameters(), lr=args.lr, betas=(args.beta1, 0.999)) optimizerD = optim.Adam(netD.parameters(), lr=args.lr, betas=(args.beta1, 0.999)) # For input of generator in testing fixed_noise = torch.randn(64, args.nz, 1, 1, device=device) # convention for training real_label = 1 fake_label = 0 # training data for later analysis img_list= [] G_losses = [] D_losses = [] iters = 0 # epochs num_epochs = 150 print('Starting Training Loop....') # For each epoch for e in range(args.num_epochs): # for each batch in the dataloader for i, data in enumerate(dataloader, 0): ########## Training Discriminator ########## netD.zero_grad() # train with real data real_data = data[0].to(device) # make labels batch_size = real_data.size(0) labels = torch.full((batch_size,), real_label, device=device) # forward pass real data through D real_outputD = netD(real_data).view(-1) # calc error on real data errD_real = criterion(real_outputD, labels) # calc grad errD_real.backward() D_x = real_outputD.mean().item() # train with fake data noise = torch.randn(batch_size, args.nz, 1, 1, device=device) fake_data = netG(noise) labels.fill_(fake_label) # classify fake fake_outputD = netD(fake_data.detach()).view(-1) # calc error on fake data errD_fake = criterion(fake_outputD, labels) # calc grad errD_fake.backward() D_G_z1 = fake_outputD.mean().item() # add all grad and update D errD = errD_real + errD_fake optimizerD.step() ######################################## ########## Training Generator ########## netG.zero_grad() # since aim is fooling the netD, labels should be flipped labels.fill_(real_label) # forward pass with updated netD fake_outputD = netD(fake_data).view(-1) # calc error errG = criterion(fake_outputD, labels) # calc grad errG.backward() D_G_z2 = fake_outputD.mean().item() # update G optimizerG.step() ######################################## # output training stats if i%500==0: print(f'[{e+1}/{args.num_epochs}][{i+1}/{len(dataloader)}]\ \tLoss_D:{errD.item():.4f}\ \tLoss_G:{errG.item():.4f}\ \tD(x):{D_x:.4f}\ \tD(G(z)):{D_G_z1:.4f}/{D_G_z2:.4f}') # for later plot G_losses.append(errG.item()) D_losses.append(errD.item()) # generate fake image on fixed noise for comparison if ((iters % 500== 0) or ((e == args.num_epochs -1) and (i==len(dataloader)-1))): with torch.no_grad(): fake = netG(fixed_noise).detach().cpu() img_list.append(vutils.make_grid(fake, padding=2, normalize=True)) iters +=1 if e%args.save_every==0: # save at args.save_every epoch torch.save(netG.state_dict(), args.outputG) torch.save(netD.state_dict(), args.outputD) print(f'Made a New Checkpoint for {e+1}') torch.save(netG.state_dict(), args.outputG) torch.save(netD.state_dict(), args.outputD) print(f'Saved Final model at {args.outputG} & {args.outputD}') # return training data for analysis return img_list, G_losses, D_losses
sdim = args.sdim batch_size = args.batchsize epochs = args.epoch trainidx = args.trainidx trainertype = args.trainertype p1 = PreprocImg("resize_divide", [dim]) if trainertype in [3, 4]: p1 = PreprocImg("resize_divide_histeq", [dim]) p2 = PreprocImg("proc_annot_resize_one_hot", [sdim, 2]) loss = Loss("categor_iou") aug_data = trainertype == 4 if trainertype == 3: g1 = Generator("data/tv_split/train_" + trainidx + ".csv", "data/train/images/", batch_size, p1, p2, aug_data) g2 = Generator("data/tv_split/valid_" + trainidx + ".csv", "data/train/images/", batch_size, p1, p2, aug_data) train_steps = g1.img_cnt // batch_size val_steps = g2.img_cnt // batch_size else: g1 = Generator("data/train_" + trainidx + ".csv", "data/train/images/", batch_size, p1, p2, aug_data) g2 = Generator("data/validation.csv", "data/train/images/", batch_size, p1, p2, aug_data) train_steps = 10000 // batch_size val_steps = 1000 // batch_size run_code = args.runcode
def main(): g = Generator(SQL_FILE, AVATAR_PATH) p = poster.Poster() drawers = {"github": github_drawer.GithubDrawer(p)} args_parser = argparse.ArgumentParser() args_parser.add_argument( "--year", metavar="YEAR", type=str, default="all", help='Filter tracks by year; "NUM", "NUM-NUM", "all" (default: all years)', ) types = '", "'.join(drawers.keys()) args_parser.add_argument( "--background-color", dest="background_color", metavar="COLOR", type=str, default="#222222", help='Background color of poster (default: "#222222").', ) args_parser.add_argument( "--track-color", dest="track_color", metavar="COLOR", type=str, default="#4DD2FF", help='Color of tracks (default: "#4DD2FF").', ) args_parser.add_argument( "--track-color2", dest="track_color2", metavar="COLOR", type=str, help="Secondary color of tracks (default: none).", ) args_parser.add_argument( "--text-color", dest="text_color", metavar="COLOR", type=str, default="#FFFFFF", help='Color of text (default: "#FFFFFF").', ) args_parser.add_argument( "--special-color", dest="special_color", metavar="COLOR", default="#FFFF00", help='Special track color (default: "#FFFF00").', ) args_parser.add_argument( "--special-color2", dest="special_color2", metavar="COLOR", help="Secondary color of special tracks (default: none).", ) args_parser.add_argument( "--units", dest="units", metavar="UNITS", type=str, choices=["metric", "imperial"], default="metric", help='Distance units; "metric", "imperial" (default: "metric").', ) args_parser.add_argument( "--clear-cache", dest="clear_cache", action="store_true", help="Clear the track cache.", ) args_parser.add_argument( "--verbose", dest="verbose", action="store_true", help="Verbose logging." ) args_parser.add_argument("--logfile", dest="logfile", metavar="FILE", type=str) args_parser.add_argument( "--special-likes", dest="special_likes", metavar="DISTANCE", type=float, default=500.0, ) args_parser.add_argument( "--special-likes2", dest="special_likes2", metavar="DISTANCE", type=float, default=1000.0, ) args_parser.add_argument( "--min-distance", dest="min_distance", metavar="DISTANCE", type=float, default=1.0, help="min distance by km for track filter", ) for _, drawer in drawers.items(): drawer.create_args(args_parser) args = args_parser.parse_args() for _, drawer in drawers.items(): drawer.fetch_args(args) log = logging.getLogger("audioTracker") # get first secode is djs audios = g.load()[0] p.title = "Gcores Calendar" p.athlete = "Gcores Audios" p.special_likes = { "special_likes": args.special_likes, "special_likes2": args.special_likes2, } p.colors = { "background": args.background_color, "track": args.track_color, "track2": args.track_color2 or args.track_color, "special": args.special_color, "special2": args.special_color2 or args.special_color, "text": args.text_color, } p.units = args.units p.set_tracks(audios) p.height = 45 + len(p.years) * 43 p.draw(drawers["github"], SAVE_DIR)
from sjf import SJF from lfu import Lfu from lru import Lru from output_handling import Output_handling from generator import Generator choices = { 'type': 'replacement', # generate data for page raplacement or cpu scheduling. Option between 'scheduling' and 'replacement' 'length': 30, # length of lists 'range': 50, # range that numbers can be generated in 'specified': 3, # for page replacement algorithms, it's capacity 'runfromfile': 1 # 1 is for prepared data } gen = Generator(choices) # runs generator preprocessed_data = gen.return_numbers() print(preprocessed_data) # display generated data def firstcomefirstserve(preprocessed_data): alg = FCFS(preprocessed_data) alg.sort_arrival() alg.waiting_time() alg.turn_around_time() alg.save_data() alg.output_data() def shortestjobfirst(preprocessed_data):
#encoding: utf-8 from fractions import * import random import codecs import sys sys.path.append('../') from generator import Generator from plugins import mixed_french g = Generator() g.data_path = ('../data') g.load(mixed_french) g.load_word_lexicon() g.load_neighbor_lexicon() g.load_lookup_lexicon() words = g.lookup_lexicon ncandidates = -1 # no limit f_out = codecs.open('french_pseudohomophones.txt', 'w', 'utf-8') headers = [ 'base.spelling', 'base.spelling.onc', 'base.phonology.onc', 'match.spelling', 'match.spelling.onc', 'match.lexicality' ] f_out.write(u'\t'.join(headers) + u'\n') for word in words: g.set_reference_sequence(g.lookup(word)) spelling = word onc_spelling, onc_phonology = g.lookup(word).split(u'|')
def main(): random.seed(SEED) np.random.seed(SEED) assert START_TOKEN == 0 # 定义gen_data_loader, 专门读取真实样本集 gen_data_loader = Gen_Data_loader(BATCH_SIZE) # 定义gen_data_loader, 专门读取验证样本集 likelihood_data_loader = Gen_Data_loader(BATCH_SIZE) # For testing vocab_size = 5000 # 生成混合数据 dis_data_loader = Dis_dataloader(BATCH_SIZE) # 定义生成模型 generator = Generator(vocab_size, BATCH_SIZE, EMB_DIM, HIDDEN_DIM, SEQ_LENGTH, START_TOKEN) # pickle.load(open("./dataset/atis.pkl", "rb"), encoding='iso-8859-1') pickle_pack = open('save/target_params_py3.pkl', 'rb') target_params = pickle.load(pickle_pack) # 定义oracle模型 target_lstm = TARGET_LSTM(vocab_size, BATCH_SIZE, EMB_DIM, HIDDEN_DIM, SEQ_LENGTH, START_TOKEN, target_params) # The oracle model # 定义判别模型 discriminator = Discriminator(sequence_length=20, num_classes=2, vocab_size=vocab_size, embedding_size=dis_embedding_dim, filter_sizes=dis_filter_sizes, num_filters=dis_num_filters, l2_reg_lambda=dis_l2_reg_lambda) config = tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.5 # 分配50% config.gpu_options.allow_growth = True # 显存自适应 sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) # First, use the oracle model to provide the positive examples, which are sampled from the oracle data distribution # 通过oracle模型生成generated_num条真实数据, generated_num/BATCH_SIZE个batch(list) generate_samples(sess, target_lstm, BATCH_SIZE, generated_num, positive_file) gen_data_loader.create_batches(positive_file) print('begin to record save/experiment-log.txt') log = open('save/experiment-log.txt', 'w') # pre-train generator # print('Start pre-training generator...') # log.write('pre-training...\n') # for epoch in range(PRE_EPOCH_NUM): # # 训练生成模型 # loss = pre_train_epoch(sess, generator, gen_data_loader) # if epoch % 5 == 0: # # 使用生成模型生成数据写入eval_file # generate_samples(sess, generator, BATCH_SIZE, generated_num, eval_file) # likelihood_data_loader.create_batches(eval_file) # # 用oracle模型测试生成数据 # test_loss = target_loss(sess, target_lstm, likelihood_data_loader) # print('pre-train epoch ', epoch, 'test_loss ', test_loss) # buffer = 'epoch:\t' + str(epoch) + '\tnll:\t' + str(test_loss) + '\n' # log.write(buffer) # # print('Start pre-training discriminator...') # # Train 3 epoch on the generated data and do this for 50 times # for _ in range(50): # generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file) # dis_data_loader.load_train_data(positive_file, negative_file) # for _ in range(3): # dis_data_loader.reset_pointer() # set next_batch pointer to 0 # for it in range(dis_data_loader.num_batch): # x_batch, y_batch = dis_data_loader.next_batch() # feed = { # discriminator.input_x: x_batch, # discriminator.input_y: y_batch, # discriminator.dropout_keep_prob: dis_dropout_keep_prob # } # _ = sess.run(discriminator.train_op, feed) print('define a rollout object!') rollout = ROLLOUT(generator, 0.8) print( '#########################################################################' ) print('Start Adversarial Training...') log.write('adversarial training...\n') for total_batch in range(TOTAL_BATCH): # Train the generator for one step print('begin to train generator with rollout policy') for it in range(1): samples = generator.generate(sess) print( 'start a rollout and get reward from discriminator(rollout number is 16)...' ) print('rollout samples shape is', samples.shape) print(samples[:5]) rewards = rollout.get_reward(sess, samples, 16, discriminator) feed = {generator.x: samples, generator.rewards: rewards} _ = sess.run(generator.g_updates, feed_dict=feed) # Test if total_batch % 5 == 0 or total_batch == TOTAL_BATCH - 1: generate_samples(sess, generator, BATCH_SIZE, generated_num, eval_file) likelihood_data_loader.create_batches(eval_file) test_loss = target_loss(sess, target_lstm, likelihood_data_loader) buffer = 'epoch:\t' + str(total_batch) + '\tnll:\t' + str( test_loss) + '\n' print('total_batch: ', total_batch, 'test_loss: ', test_loss) log.write(buffer) # Update roll-out parameters using exponentially weighted averages beta=0.8 rollout.update_params() # Train the discriminator print( 'begin to train discriminator with positive and negative samples') for _ in range(5): print( 'generate %d negative samples from generator and write in %s' % (int(generated_num / BATCH_SIZE) * BATCH_SIZE, negative_file)) generate_samples(sess, generator, BATCH_SIZE, generated_num, negative_file) print('load pos and neg samples and shuffle and bootstrap') dis_data_loader.load_train_data(positive_file, negative_file) for _ in range(3): dis_data_loader.reset_pointer() for it in range(dis_data_loader.num_batch): x_batch, y_batch = dis_data_loader.next_batch() feed = { discriminator.input_x: x_batch, discriminator.input_y: y_batch, discriminator.dropout_keep_prob: dis_dropout_keep_prob } _ = sess.run(discriminator.train_op, feed) log.close()
generator = Generator(latent_dim).generator gan = GAN(discriminator, generator, latent_dim) gan.compile( d_optimizer=keras.optimizers.Adam(learning_rate_d), g_optimizer=keras.optimizers.Adam(learning_rate_g), loss_function=keras.losses.BinaryCrossentropy(from_logits=True), ) gan.fit(dataset, epochs=epochs, callbacks=[GANMonitor(num_images, latent_dim)]) # gan.save("gan_model.h5") generator.save("generator_model.h5") discriminator.save("discriminator_model.h5") if __name__ == "__main__": # train(prepare_data()) pil_img = tf.keras.preprocessing.image.array_to_img( prepare_image("000013.jpg")) pil_img.save("test.png") # plt.imshow(pil_img) # plt.show() discriminator = Discriminator() generator = Generator(256) print(discriminator.summary()) print(generator.summary())
def __init__(self, X_train_file='', Y_train_file='', batch_size=1, image_size=(270, 480), norm='instance', lambda1=10.0, lambda2=10.0, optimizer='Adam', lossfunc='use_lsgan', learning_rate=2e-4, start_decay_step=100000, decay_steps=100000, beta1=0.5, ngf=64): """ Args: X_train_file: string, X tfrecords file for training Y_train_file: string Y tfrecords file for training batch_size: integer, batch size image_size: integer, image size lambda1: integer, weight for forward cycle loss (X->Y->X) lambda2: integer, weight for backward cycle loss (Y->X->Y) use_lsgan: boolean norm: 'instance' or 'batch' learning_rate: float, initial learning rate for Adam beta1: float, momentum term of Adam ngf: number of gen filters in first conv layer """ self.lambda1 = lambda1 self.lambda2 = lambda2 self.batch_size = batch_size self.image_size = image_size self.optimizer = optimizer self.learning_rate = learning_rate self.start_decay_step = start_decay_step self.decay_steps = decay_steps self.beta1 = beta1 self.X_train_file = X_train_file self.Y_train_file = Y_train_file self.lossfunc = lossfunc self.is_training = tf.placeholder_with_default(True, shape=[], name='is_training') self.is_training = True self.Ga2b = Generator('Ga2b', self.is_training, norm=norm, image_size=image_size, mean=0.0) self.Da2b = Discriminator('Da2b', self.is_training, norm=norm) self.Gb2a = Generator('Gb2a', self.is_training, norm=norm, image_size=image_size, mean=0.0) self.Db2a = Discriminator('Db2a', self.is_training, norm=norm) self.fake_a = tf.placeholder( tf.float32, shape=[batch_size, image_size[0], image_size[1], 3]) self.fake_b = tf.placeholder( tf.float32, shape=[batch_size, image_size[0], image_size[1], 3])
def main(): if not torch.cuda.is_available(): logging.info('No GPU device available') sys.exit(1) np.random.seed(args.seed) cudnn.benchmark = True torch.manual_seed(args.seed) cudnn.enabled = True torch.cuda.manual_seed(args.seed) logging.info("args = %s", args) logging.info("unparsed args = %s", unparsed) num_gpus = torch.cuda.device_count() genotype = eval("genotypes.%s" % args.arch) print('---------Genotype---------') logging.info(genotype) print('--------------------------') disc = Network(args.init_channels, CIFAR_CLASSES, args.layers, args.auxiliary, genotype) disc = torch.nn.DataParallel(disc) disc = disc.cuda() logging.info("param size = %fMB", utils.count_parameters_in_MB(disc)) adversarial_loss = nn.MSELoss() adversarial_loss.cuda() criterion = nn.CrossEntropyLoss() criterion = criterion.cuda() optimizer_disc = torch.optim.SGD(disc.parameters(), args.learning_rate, momentum=args.momentum, weight_decay=args.weight_decay) scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( optimizer_disc, float(args.epochs)) gen = Generator(100) gen.cuda() model = Resnet18() model.cuda() logging.info("param size gen= %fMB", utils.count_parameters_in_MB(gen)) logging.info("param size model= %fMB", utils.count_parameters_in_MB(model)) optimizer_gen = torch.optim.Adam(gen.parameters(), lr=args.lr, betas=(args.b1, args.b2)) optimizer_model = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4) scheduler_model = torch.optim.lr_scheduler.CosineAnnealingLR( optimizer_model, T_max=200) if args.cifar100: train_transform, valid_transform = utils._data_transforms_cifar100( args) else: train_transform, valid_transform = utils._data_transforms_cifar10(args) if args.cifar100: train_data = dset.CIFAR100(root=args.tmp_data_dir, train=True, download=True, transform=train_transform) valid_data = dset.CIFAR100(root=args.tmp_data_dir, train=False, download=True, transform=valid_transform) else: train_data = dset.CIFAR10(root=args.tmp_data_dir, train=True, download=True, transform=train_transform) valid_data = dset.CIFAR10(root=args.tmp_data_dir, train=False, download=True, transform=valid_transform) train_queue = torch.utils.data.DataLoader(train_data, batch_size=args.batch_size, shuffle=True, pin_memory=True, num_workers=args.workers) valid_queue = torch.utils.data.DataLoader(valid_data, batch_size=args.batch_size, shuffle=False, pin_memory=True, num_workers=args.workers) best_acc = 0.0 for epoch in range(args.epochs): # scheduler_model.step() # lr_gen = args.lr # lr_disc = args.learning_rate # lr_model = scheduler_model.get_lr()[0] # logging.info('Epoch: %d lr_model %e', epoch, lr_model) # disc.module.drop_path_prob = args.drop_path_prob * epoch / args.epochs # disc.drop_path_prob = args.drop_path_prob * epoch / args.epochs # start_time = time.time() # train_acc, train_obj = train(train_queue, gen ,disc, model, criterion, adversarial_loss, optimizer_disc, optimizer_gen, optimizer_model) # logging.info('Train_acc: %f', train_acc) # valid_acc, valid_obj = infer(valid_queue, model, criterion) # if valid_acc > best_acc: # best_acc = valid_acc # logging.info('Valid_acc: %f', valid_acc) logging.info('Epoch: %d', epoch) epoch_start = time.time() train_acc, train_obj = train_gan(epoch, train_queue, valid_queue, gen, disc, criterion, adversarial_loss, optimizer_gen, optimizer_disc, 0, 0, 0, 0, train_arch=True) epoch_duration = time.time() - epoch_start logging.info('Epoch time: %ds', epoch_duration) end_time = time.time() duration = end_time - start_time print('Epoch time: %ds.' % duration)
#!/usr/bin/python3 from generator import Generator, gen_text_plain, gen_boundary import email.message good_sub = 'headers in top-level MIME: subject restored to original' bad_sub = "headers in top-level MIME: tampered subject and from" m = Generator( bad_sub, __file__, '''\ This email demonstrates including the memoryhole headers inside the top-level MIME object. The signed Subject and From are headers of the text/plain part, rather than having their own rfc822-headers part. This email has been tampered with. It was originally sent by Winston, who signed the body (including the memoryhole headers). Eve has fiddled with the From and Subject headers in-flight. ''') m.add_header('Date', 'Wed, 29 Jul 2015 09:31:44 +0100') m.add_header('To', 'Julia <*****@*****.**>') m.add_header('From', 'Eve <*****@*****.**>') s = gen_text_plain(m) s.add_header('Subject', good_sub) s.add_header('From', 'Winston <*****@*****.**>') s.set_boundary(gen_boundary()) m.sign(s, 'Winston <*****@*****.**>') m.main()
host = cloud_data_types.Host() xml_operator.output(host) for z in range(10): cluster = cloud_data_types.Cluster() xml_operator.output(cluster) else: gen = Generator( datetime.datetime.timestamp(CONF.start_time), CONF.cron_interval, CONF.count, CONF.users_count, int( floor((0.8 * CONF.average_occupancy / 100) * CONF.max_objects / CONF.users_count)), int( ceil((1.2 * CONF.average_occupancy / 100) * CONF.max_objects / CONF.users_count)), CONF.groups_count, CONF.cloud_name, CONF.records_per_file) if CONF.flood: logging.debug("FLOOD in record mode") gen.generate_cloud_records() gen.generate_ip_records() gen.generate_storage_records() else: if CONF.mode == "vm": logging.debug("Generating VM records") gen.generate_cloud_records() if CONF.mode == "storage":
#!/usr/bin/python3.5 # -*- coding: utf-8 -*- from console import Console from generator import Generator c = Console() g = Generator(c.getPositionnalArgs(), c.getLength()) print(g.generatePassword())
return generated_loss def id_loss(real, generated, Lambda=2e-4): return Lambda * torch.mean(torch.absolute(real - generated)) def weights_init(m): if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d): torch.nn.init.normal_(m.weight, 0.0, 0.02) if isinstance(m, nn.BatchNorm2d): torch.nn.init.normal_(m.weight, 0.0, 0.02) torch.nn.init.constant_(m.bias, 0) monet_generator = Generator().to(device) photo_generator = Generator().to(device) monet_discriminator = Discriminator().to(device) photo_discriminator = Discriminator().to(device) monet_generator = monet_generator.apply(weights_init) photo_generator = photo_generator.apply(weights_init) monet_discriminator = monet_discriminator.apply(weights_init) photo_discriminator = photo_discriminator.apply(weights_init) n_epoch = 30 BATCH_SIZE = 8 LAMBDA = 10 lr = 2e-4 save = True
def main(): random.seed(SEED) np.random.seed(SEED) tf.random.set_seed(SEED) assert START_TOKEN == 0 vocab_size = 5000 physical_devices = tf.config.experimental.list_physical_devices("GPU") if len(physical_devices) > 0: for dev in physical_devices: tf.config.experimental.set_memory_growth(dev, True) # 生成器の初期設定 generator = Generator(vocab_size, BATCH_SIZE, EMB_DIM, HIDDEN_DIM, SEQ_LENGTH, START_TOKEN) # 最初のパラメータをpicklファイルから参照 target_params = pickle.load(open('save/target_params_py3.pkl', 'rb')) target_lstm = TARGET_LSTM(BATCH_SIZE, SEQ_LENGTH, START_TOKEN, target_params) # The oracle model # 識別器の初期設定 discriminator = Discriminator(sequence_length=SEQ_LENGTH, num_classes=2, vocab_size=vocab_size, embedding_size=dis_embedding_dim, filter_sizes=dis_filter_sizes, num_filters=dis_num_filters, dropout_keep_prob=dis_dropout_keep_prob, l2_reg_lambda=dis_l2_reg_lambda) # First, use the oracle model to provide the positive examples, which are sampled from the oracle data distribution # GANの学習で使用する正解データを作成する if not os.path.exists(positive_file): target_lstm.generate_samples(generated_num // BATCH_SIZE, positive_file) gen_dataset = dataset_for_generator(positive_file, BATCH_SIZE) log = open('save/experiment-log.txt', 'w') # 事前学習での文章生成をlstmで行い、生成器の重みを保存する if not os.path.exists("generator_pretrained.h5"): print('Start pre-training...') log.write('pre-training...\n') generator.pretrain(gen_dataset, target_lstm, PRE_EPOCH_NUM, generated_num // BATCH_SIZE, eval_file) generator.save("generator_pretrained.h5") else: generator.load("generator_pretrained.h5") # 識別器の事前学習での重み if not os.path.exists("discriminator_pretrained.h5"): print('Start pre-training discriminator...') # Train 3 epoch on the generated data and do this for 50 times # 3エポックの識別器の訓練を50回繰り返す for _ in range(50): print("Dataset", _) # まず生成器が偽物を作成 generator.generate_samples(generated_num // BATCH_SIZE, negative_file) # 偽物と本物を混ぜたデータセットを作成 dis_dataset = dataset_for_discriminator(positive_file, negative_file, BATCH_SIZE) # 識別器を学習させる discriminator.train(dis_dataset, 3, (generated_num // BATCH_SIZE) * 2) discriminator.save("discriminator_pretrained.h5") else: discriminator.load("discriminator_pretrained.h5") rollout = ROLLOUT(generator, 0.8) print('#########################################################################') print('Start Adversarial Training...') log.write('adversarial training...\n') # 学習の実行 # 今回は200回の訓練を行う for total_batch in range(TOTAL_BATCH): print("Generator", total_batch) # Train the generator for one step for it in range(1): samples = generator.generate_one_batch() rewards = rollout.get_reward(samples, 16, discriminator) generator.train_step(samples, rewards) # Test if total_batch % 5 == 0 or total_batch == TOTAL_BATCH - 1: generator.generate_samples(generated_num // BATCH_SIZE, eval_file) likelihood_dataset = dataset_for_generator(eval_file, BATCH_SIZE) test_loss = target_lstm.target_loss(likelihood_dataset) buffer = 'epoch:\t' + str(total_batch) + '\tnll:\t' + str(test_loss) + '\n' print('total_batch: ', total_batch, 'test_loss: ', test_loss) log.write(buffer) # Update roll-out parameters rollout.update_params() # Train the discriminator print("Discriminator", total_batch) for _ in range(5): generator.generate_samples(generated_num // BATCH_SIZE, negative_file) dis_dataset = dataset_for_discriminator(positive_file, negative_file, BATCH_SIZE) discriminator.train(dis_dataset, 3, (generated_num // BATCH_SIZE) * 2) generator.save("generator.h5") discriminator.save("discriminator.h5") generator.generate_samples(generated_num // BATCH_SIZE, output_file) log.close()
def test_skip_presenter_notes(self): g = Generator(os.path.join(SAMPLES_DIR, 'example1', 'slides.md'), presenter_notes=False) svars = g.get_slide_vars("<h1>heading</h1>\n<p>foo</p>\n" "<h1>Presenter Notes</h1>\n<p>bar</p>\n") self.assertEquals(svars['presenter_notes'], None)
def main(args, local_rank): vocabs = dict() vocabs['concept'] = Vocab(args.concept_vocab, 5, [CLS]) vocabs['token'] = Vocab(args.token_vocab, 5, [STR, END]) vocabs['predictable_token'] = Vocab(args.predictable_token_vocab, 5, [END]) vocabs['token_char'] = Vocab(args.token_char_vocab, 100, [STR, END]) vocabs['concept_char'] = Vocab(args.concept_char_vocab, 100, [STR, END]) vocabs['relation'] = Vocab(args.relation_vocab, 5, [CLS, rCLS, SEL, TL]) lexical_mapping = LexicalMap() for name in vocabs: print((name, vocabs[name].size, vocabs[name].coverage)) torch.manual_seed(19940117) torch.cuda.manual_seed_all(19940117) random.seed(19940117) #device = torch.device('cpu') device = torch.device('cuda', local_rank) model = Generator(vocabs, args.token_char_dim, args.token_dim, args.concept_char_dim, args.concept_dim, args.cnn_filters, args.char2word_dim, args.char2concept_dim, args.rel_dim, args.rnn_hidden_size, args.rnn_num_layers, args.embed_dim, args.ff_embed_dim, args.num_heads, args.dropout, args.snt_layers, args.graph_layers, args.inference_layers, args.pretrained_file, device) if args.world_size > 1: torch.manual_seed(19940117 + dist.get_rank()) torch.cuda.manual_seed_all(19940117 + dist.get_rank()) random.seed(19940117 + dist.get_rank()) model = model.to(device) train_data = DataLoader(vocabs, lexical_mapping, args.train_data, args.train_batch_size, for_train=True) #dev_data = DataLoader(vocabs, lexical_mapping, args.dev_data, args.dev_batch_size, for_train=False) train_data.set_unk_rate(args.unk_rate) weight_decay_params = [] no_weight_decay_params = [] for name, param in model.named_parameters(): if name.endswith('bias') or 'layer_norm' in name: no_weight_decay_params.append(param) else: weight_decay_params.append(param) grouped_params = [{ 'params': weight_decay_params, 'weight_decay': 1e-4 }, { 'params': no_weight_decay_params, 'weight_decay': 0. }] optimizer = AdamWeightDecayOptimizer(grouped_params, lr=args.lr, betas=(0.9, 0.999), eps=1e-6) batches_acm, loss_acm = 0, 0 discarded_batches_acm = 0 queue = mp.Queue(10) train_data_generator = mp.Process(target=data_proc, args=(train_data, queue)) train_data_generator.start() model.train() epoch = 0 while batches_acm < args.total_train_steps: batch = queue.get() if isinstance(batch, str): epoch += 1 print('epoch', epoch, 'done', 'batches', batches_acm) continue batch = move_to_device(batch, device) loss = model(batch) loss_value = loss.item() if batches_acm > args.warmup_steps and loss_value > 5. * (loss_acm / batches_acm): discarded_batches_acm += 1 print('abnormal', loss_value) continue loss_acm += loss_value batches_acm += 1 loss.backward() if args.world_size > 1: average_gradients(model) torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0) update_lr(optimizer, args.embed_dim, batches_acm, args.warmup_steps) optimizer.step() optimizer.zero_grad() if args.world_size == 1 or (dist.get_rank() == 0): if batches_acm % args.print_every == -1 % args.print_every: print( 'Train Epoch %d, Batch %d, Discarded Batch %d, loss %.3f' % (epoch, batches_acm, discarded_batches_acm, loss_acm / batches_acm)) model.train() if batches_acm > args.warmup_steps and batches_acm % args.eval_every == -1 % args.eval_every: #model.eval() #bleu, chrf = validate(model, dev_data) #print ("epoch", "batch", "bleu", "chrf") #print (epoch, batches_acm, bleu, chrf) torch.save({ 'args': args, 'model': model.state_dict() }, '%s/epoch%d_batch%d' % (args.ckpt, epoch, batches_acm)) model.train()
def main(cfg): # 再現性のためにシード値をセット manualSeed = 999 # manualSeed = random.randint(1, 10000) # 新しい結果がほしい場合に使用 print("Random Seed: ", manualSeed) random.seed(manualSeed) torch.manual_seed(manualSeed) # datsetの作成 dataset = make_dataset(cfg.dataroot, cfg.image_size) # データローダの作成 dataloader = torch.utils.data.DataLoader(dataset, batch_size=cfg.batch_size, shuffle=True, num_workers=cfg.workers) # どのデバイスで実行するか決定 device = torch.device("cuda:0" if ( torch.cuda.is_available() and cfg.ngpu > 0) else "cpu") # 訓練画像をプロット real_batch = next(iter(dataloader)) plt.figure(figsize=(8, 8)) plt.axis("off") plt.title("Training Images") plt.imshow( np.transpose( vutils.make_grid(real_batch[0].to(device)[:64], padding=2, normalize=True).cpu(), (1, 2, 0), )) plt.savefig("training_img") # Generator作成 netG = Generator(cfg.ngpu, cfg.nz, cfg.ngf, cfg.nc).to(device) # マルチGPUを望むなら if (device.type == "cuda") and (cfg.ngpu > 1): netG = nn.DataParallel(netG, list(range(cfg.ngpu))) # 重みの初期化関数を適用 netG.apply(weights_init) # モデルの印字 print(netG) # Discriminator作成 netD = Discriminator(cfg.ngpu, cfg.ndf, cfg.nc).to(device) # マルチGPUを望むなら if (device.type == "cuda") and (cfg.ngpu > 1): netD = nn.DataParallel(netD, list(range(cfg.ngpu))) # 重みの初期化関数を適用 netD.apply(weights_init) # モデルの印字 print(netD) # 損失関数の定義 criterion = nn.BCELoss() # 潜在ベクトルを作成 Generatorの進歩を可視化するため fixed_noise = torch.randn(64, cfg.nz, 1, 1, device=device) # 学習中の本物と偽物のラベルを作成 real_label = 1 fake_label = 0 # 最適化関数Adamを設定 optimizerD = optim.Adam(netD.parameters(), lr=cfg.lr, betas=(cfg.beta1, 0.999)) optimizerG = optim.Adam(netG.parameters(), lr=cfg.lr, betas=(cfg.beta1, 0.999)) # 学習ループ # 結果を保存しておくリスト img_list = [] G_losses = [] D_losses = [] iters = 0 print("Starting Training Loop...") # For each epoch for epoch in range(cfg.num_epochs): # For each batch in the dataloader for i, data in enumerate(dataloader, 0): ############### # 1. Discriminatorの更新 ############### # 本物のバッチの学習 netD.zero_grad() # Format batch real_cpu = data[0].to(device) b_size = real_cpu.size(0) label = torch.full((b_size, ), real_label, device=device) # 本物のバッチをDiscriminatorへ output = netD(real_cpu).view(-1) # 損失値の計算 errD_real = criterion(output, label) # bpによる勾配計算 errD_real.backward() D_x = output.mean().item() # 偽物のバッチ学習 # 潜在ベクトル生成 noise = torch.randn(b_size, cfg.nz, 1, 1, device=device) # 偽画像の生成 fake = netG(noise) label.fill_(fake_label) # 偽画像の分類 output = netD(fake.detach()).view(-1) # 偽画像の損失値の計算 errD_fake = criterion(output, label) # 勾配の計算 errD_fake.backward() D_G_z1 = output.mean().item() # 本物と偽物の勾配を足す errD = errD_real + errD_fake # Discriminatorの更新 optimizerD.step() ########## # 2. Generatorの更新 ########## netG.zero_grad() label.fill_(real_label) output = netD(fake).view(-1) # GenerotorのLoss計算 errG = criterion(output, label) # Generatorの勾配計算 errG.backward() D_G_z2 = output.mean().item() # Generatorの更新 optimizerG.step() # 学習の状態を出力 if i % 50 == 0: print( "[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f" % ( epoch, cfg.num_epochs, i, len(dataloader), errD.item(), errG.item(), D_x, D_G_z1, D_G_z2, )) # Lossを保存する G_losses.append(errG.item()) D_losses.append(errD.item()) # Generatorの動作確認と出力画像を保存 if (iters % 500 == 0) or ((epoch == cfg.num_epochs - 1) and (i == len(dataloader) - 1)): with torch.no_grad(): fake = netG(fixed_noise).detach().cpu() img_list.append( vutils.make_grid(fake, padding=2, normalize=True)) iters += 1 plt.figure(figsize=(10, 5)) plt.title("Generator and Discriminator Loss During Training") plt.plot(G_losses, label="G") plt.plot(D_losses, label="D") plt.xlabel("iterations") plt.ylabel("Loss") plt.legend() plt.show() plt.savefig("Genarator_Discriminator_Loss.png") # データローダから本物の画像を取得 real_batch = next(iter(dataloader)) # Real images のplot plt.figure(figsize=(15, 15)) plt.subplot(1, 2, 1) plt.axis("off") plt.title("Real Images") plt.imshow( np.transpose( vutils.make_grid(real_batch[0].to(device)[:64], padding=5, normalize=True).cpu(), (1, 2, 0), )) # 最後のエポックの偽画像を表示 plt.subplot(1, 2, 2) plt.axis("off") plt.title("Fake Images") plt.imshow(np.transpose(img_list[-1], (1, 2, 0))) plt.show() plt.savefig("result_img.png")
def main(_): print("CONFIG: ") pprint(FLAGS.__flags) np.random.seed(FLAGS.random_seed) tf.reset_default_graph() tf.set_random_seed(FLAGS.random_seed) save_dir = os.path.join(FLAGS.ckpt_dir) generator = Generator(**FLAGS.__flags) discriminator = Discriminator(**FLAGS.__flags) global_step = tf.Variable(0, name="global_step", trainable=False) noises = generator.get_noise_tensor() x = tf.placeholder(dtype=tf.float32, shape=[FLAGS.batch_size, FLAGS.sequence_length, 64, 64, FLAGS.num_channels]) is_training = tf.placeholder(tf.bool, shape=()) dropout_kept_prob = tf.placeholder(tf.float32, shape=()) # x_noise = tf.placeholder(tf.float32, shape=[FLAGS.batch_size, FLAGS.sequence_length, 64, 64, # FLAGS.num_channels]) # x_fake_noise = tf.placeholder(tf.float32, shape=[FLAGS.batch_size, FLAGS.sequence_length, 64, 64, # FLAGS.num_channels]) x_fake = generator.g_output_unit(noises, is_training) d_real, d_real_logit = discriminator.d_output_unit(x, dropout_kept_prob, is_training) d_fake, d_fake_logit = discriminator.d_output_unit(x_fake, dropout_kept_prob, is_training) if FLAGS.loss_name == "BASIC": g_loss = -tf.reduce_mean(tf.log(d_fake)) d_loss_real = -tf.reduce_mean(tf.log(d_real)) d_loss_fake = -tf.reduce_mean(tf.log(1. - d_fake)) d_loss = -tf.reduce_mean(tf.log(d_real) + tf.log(1. - d_fake)) elif FLAGS.loss_name == "ALTERNATIVE": g_loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=d_fake_logit, labels=tf.ones_like(d_fake_logit))) d_loss_real = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=d_real_logit, labels=tf.one_hot(tf.ones(shape=[FLAGS.batch_size], dtype=tf.uint8), depth=2) * tf.random_uniform([FLAGS.batch_size, 1], 0.7, 1.0))) d_loss_fake = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=d_fake_logit, labels=tf.one_hot(tf.zeros(shape=[FLAGS.batch_size], dtype=tf.uint8), depth=2) * tf.random_uniform([FLAGS.batch_size, 1], 0.7, 1.0))) d_loss = d_loss_real + d_loss_fake elif FLAGS.loss_name == "WGAN-GP": g_loss = -tf.reduce_mean(d_fake_logit) d_loss_real = tf.reduce_mean(d_real_logit) d_loss_fake = tf.reduce_mean(d_fake_logit) d_loss = tf.reduce_mean(d_fake_logit) - tf.reduce_mean(d_real_logit) epsilon = tf.random_uniform([FLAGS.batch_size, 1, 1, 1, 1], 0.0, 1.0) x_hat = epsilon * x + (1 - epsilon) * x_fake d_hat, d_hat_logit = discriminator.d_output_unit(x_hat, dropout_kept_prob, is_training) ddx = tf.gradients(d_hat_logit, x_hat)[0] ddx = tf.sqrt(tf.reduce_sum(tf.square(ddx), reduction_indices=[1, 2, 3, 4])) ddx = tf.reduce_mean(tf.square(ddx - 1.0) * FLAGS.gradient_policy_scale) d_loss = d_loss + ddx if FLAGS.regularizer_scale > 0: regularizer = tf.contrib.layers.l2_regularizer(scale=FLAGS.regularizer_scale) reg_term = tf.contrib.layers.apply_regularization(regularizer, discriminator.get_weights) d_loss += reg_term if FLAGS.loss_name == "WGAN-GP": d_batch_norm_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope="discriminator") if d_batch_norm_update_ops: d_loss = control_flow_ops.with_dependencies(d_batch_norm_update_ops, d_loss) g_batch_norm_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope="while/generator") if g_batch_norm_update_ops: g_loss = control_flow_ops.with_dependencies(g_batch_norm_update_ops, g_loss) d_loss_summary_op = tf.summary.scalar("d_loss/total", d_loss) d_loss_real_summary_op = tf.summary.scalar("d_loss/real", d_loss_real) d_loss_fake_summary_op = tf.summary.scalar("d_loss/fake", d_loss_fake) d_loss_summary_op = tf.summary.merge([d_loss_real_summary_op, d_loss_fake_summary_op, d_loss_summary_op]) if FLAGS.loss_name == "WGAN-GP": ddx_summary_op = tf.summary.scalar("d_loss/ddx", ddx) d_loss_summary_op = tf.summary.merge([d_loss_summary_op, ddx_summary_op]) g_loss_summary_op = tf.summary.scalar("g_loss", g_loss) add_variable_summaries(tf.identity(d_real_logit, name="d_activation/real")) add_variable_summaries(tf.identity(d_fake_logit, name="d_activation/fake")) increase_global_step = tf.assign(global_step, global_step + 1) if FLAGS.learning_rate_decay_schedule != 0: d_lr = tf.train.exponential_decay(FLAGS.d_lr, global_step, FLAGS.learning_rate_decay_schedule, FLAGS.learning_rate_decay_rate, staircase=True) g_lr_lstm = tf.train.exponential_decay(FLAGS.g_lr_lstm, global_step, FLAGS.learning_rate_decay_schedule, FLAGS.learning_rate_decay_rate, staircase=True) g_lr_conv = tf.train.exponential_decay(FLAGS.g_lr_conv, global_step, FLAGS.learning_rate_decay_schedule, FLAGS.learning_rate_decay_rate, staircase=True) else: d_lr = FLAGS.d_lr g_lr_lstm = FLAGS.g_lr_lstm g_lr_conv = FLAGS.g_lr_conv d_opt = tf.train.AdamOptimizer(d_lr, beta1=FLAGS.adam_b1, beta2=FLAGS.adam_b2) g_opt_lstm = tf.train.MomentumOptimizer(g_lr_lstm, momentum=FLAGS.g_momentum_lstm) #g_opt_lstm = tf.train.AdamOptimizer(g_lr_lstm, beta1=FLAGS.adam_b1, beta2=FLAGS.adam_b2) g_opt_conv = tf.train.AdamOptimizer(g_lr_conv, beta1=FLAGS.adam_b1, beta2=FLAGS.adam_b2) d_gvs = d_opt.compute_gradients(d_loss, var_list=list(discriminator.get_params())) g_gvs_lstm = g_opt_lstm.compute_gradients(g_loss, var_list=list(generator.get_lstm_params())) g_gvs_conv = g_opt_conv.compute_gradients(g_loss, var_list=list(generator.get_conv_params())) # clip by individual norm if FLAGS.d_gradient_clip: d_gvs = [(tf.clip_by_norm(grad, clip_norm=FLAGS.d_clip_norm), var) for grad, var in d_gvs] if FLAGS.g_gradient_clip_lstm: #gradients, variables = list(zip(*g_gvs_lstm)) #gradients, _ = tf.clip_by_global_norm(gradients, clip_norm=FLAGS.g_clip_norm_lstm) #g_gvs_lstm = [(grad, var) for grad, var in zip(gradients, variables)] g_gvs_lstm = [(tf.clip_by_norm(grad, clip_norm=FLAGS.g_clip_norm_lstm), var) for grad, var in g_gvs_lstm] if FLAGS.g_gradient_clip_conv: g_gvs_conv = [(tf.clip_by_norm(grad, clip_norm=FLAGS.g_clip_norm_conv), var) for grad, var in g_gvs_conv] add_gradient_summaries(d_gvs) add_gradient_summaries(g_gvs_lstm) add_gradient_summaries(g_gvs_conv) d_solver = d_opt.apply_gradients(d_gvs) g_solver_lstm = g_opt_lstm.apply_gradients(g_gvs_lstm) g_solver_conv = g_opt_conv.apply_gradients(g_gvs_conv) if FLAGS.loss_name in ["BASIC"]: moving_mnist = read_data_sets("data/Moving_MNIST", train_size=FLAGS.train_size, rescale=False, num_channels=FLAGS.num_channels) else: moving_mnist = read_data_sets("data/Moving_MNIST", train_size=FLAGS.train_size, rescale=True, num_channels=FLAGS.num_channels) mkdir(FLAGS.out_dir) mkdir(FLAGS.log_dir) if FLAGS.cleanup and FLAGS.ckpt_fn is None: cleanup_dir(FLAGS.out_dir) cleanup_dir(FLAGS.log_dir) discriminator.create_summaries_for_variables() generator.create_summaries_for_variables() summary_op = tf.summary.merge_all() sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(write_version=tf.train.SaverDef.V2) writer = tf.summary.FileWriter(logdir=FLAGS.log_dir, graph=sess.graph) restore_vars(saver, sess, save_dir, FLAGS.ckpt_fn) iter = int(global_step.eval(sess)) start_iter = iter json.dump(FLAGS.__flags, open(os.path.join(save_dir, "config.txt"), "w")) summary_noises = generator.generate_noise() num_iters_pretrain_d = int(25.0 * FLAGS.batch_size / FLAGS.train_size) while True: if iter > start_iter and iter % FLAGS.sample_every_n_batches == 0: videos = sess.run(x_fake, feed_dict={noises: summary_noises, is_training: True}) save_gif_from_sampled_videos(videos, fn="{}/{}.gif".format(FLAGS.out_dir, str(iter).zfill(7))) if iter > start_iter and iter % FLAGS.save_every_n_batches == 0: print("Saving model begins...") saver.save(sess, os.path.join(save_dir, "model"), global_step=iter) print("Save completed!") real_videos = moving_mnist.train.next_batch(FLAGS.batch_size) if iter < num_iters_pretrain_d or iter % 500 == 0: d_iters = 100 else: d_iters = FLAGS.d_steps g_iters = FLAGS.g_steps for j in range(d_iters): _, d_loss_curr, d_loss_summary = sess.run([d_solver, d_loss, d_loss_summary_op], feed_dict={noises: generator.generate_noise(), x: real_videos, dropout_kept_prob: FLAGS.dropout_kept_prob, is_training: True}) if d_loss_curr < FLAGS.d_threshold: break for j in range(g_iters): _, _, g_loss_curr, g_loss_summary = sess.run([g_solver_lstm, g_solver_conv, g_loss, g_loss_summary_op], feed_dict={noises: generator.generate_noise(), dropout_kept_prob: FLAGS.dropout_kept_prob, is_training: True}) if g_loss_curr < FLAGS.g_threshold: break print("Step {}: D loss {:.6f}, G loss {:.6f}".format(iter, d_loss_curr, g_loss_curr)) if iter > start_iter and iter % FLAGS.save_every_n_batches == 0: summary = sess.run(summary_op, feed_dict={noises: summary_noises, x: real_videos, dropout_kept_prob: FLAGS.dropout_kept_prob, is_training: True}) writer.add_summary(summary, global_step=iter) writer.flush() else: writer.add_summary(d_loss_summary, global_step=iter) writer.add_summary(g_loss_summary, global_step=iter) sess.run([increase_global_step]) iter += 1 if iter == FLAGS.total_batches: break
def train(): gen = Generator() model = Graphsage_unsup() num_batchs = 10 with tf.Session() as sess: tf.global_variables_initializer().run() for i in range(num_batchs): s = time.time() u_i, u_j, unsup_label = gen.deepwalk_walk() u_s1, u_s2 = gen.fetch_batch(u_i) j_s1, j_s2 = gen.fetch_batch(u_j) feed_dict_unsup = { model.ui_nodes: u_i, model.uj_nodes: u_j, model.ui_s1_neighs: u_s1, model.ui_s2_neighs: u_s2, model.uj_s1_neighs: j_s1, model.uj_s2_neighs: j_s2, model.unsup_label: unsup_label } _, ls_train = sess.run([model.unsup_op, model.loss_unsup], feed_dict=feed_dict_unsup) e = time.time() t = e - s print(' num of batchs = {:d} TrainLoss = {:.5f} Time = {:.3f}\n'. format(i + 1, ls_train, t)) #test start = 0 embedding = np.zeros((cfg.num_nodes, cfg.dims)) while (start < cfg.num_nodes): end = min(start + cfg.batchsize, cfg.num_nodes) unique_nodes = list(range(start, end)) samp_neighs_1st, samp_neighs_2nd = gen.fetch_batch(unique_nodes) x = sess.run(model.ui_result, feed_dict={ model.ui_nodes: unique_nodes, model.ui_s1_neighs: samp_neighs_1st, model.ui_s2_neighs: samp_neighs_2nd }) embedding[unique_nodes] = x start = end print(embedding.shape) X, Y = [i for i in range(cfg.num_nodes) ], [int(cfg.labels[i]) for i in range(cfg.num_nodes)] state = random.getstate() random.shuffle(X) random.setstate(state) random.shuffle(Y) index = int(cfg.num_nodes * cfg.clf_ratio) X_train = embedding[X[0:index]] Y_train = Y[0:index] X_test = embedding[X[index:]] Y_test = Y[index:] clf = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial').fit( X_train, Y_train) print('TestAccuracy = ', clf.score(X_test, Y_test))
}} }} // namespace oneflow """) yaml_file_path = os.path.join( args.project_source_dir, "oneflow/api/python/functional/dispatch_stateful_ops.yaml") generated_api_dir = "oneflow/api/python/functional" generated_pybind_dir = "oneflow/api/python/functional" if __name__ == "__main__": assert os.path.isfile(yaml_file_path), ( "It is not a regular file for the yaml file which is " + yaml_file_path) g = Generator(yaml_file_path) assert os.path.isdir(generated_api_dir), ( "Could not locate the api generate directory which is " + generated_api_dir) target_header_file = os.path.join(generated_api_dir, "dispatch_stateful_ops.yaml.h") g.generate_cpp_header_file(header_fmt, target_header_file) target_source_file = os.path.join(generated_api_dir, "dispatch_stateful_ops.yaml.cpp") g.generate_cpp_source_file(source_fmt, target_source_file) assert os.path.isdir(generated_pybind_dir), ( "Could not locate the pybind generate directory which is " + generated_pybind_dir) target_pybind_header_file = os.path.join(
def __init__(self, X_train_file='', Y_train_file='', batch_size=1, image_width=256, image_height=256, use_lsgan=True, norm='instance', lambda1=10, lambda2=10, learning_rate=2e-4, beta1=0.5, ngf=64): """ Args: X_train_file: string, X tfrecords file for training Y_train_file: string Y tfrecords file for training batch_size: integer, batch size image_size: integer, image size lambda1: integer, weight for forward cycle loss (X->Y->X) lambda2: integer, weight for backward cycle loss (Y->X->Y) use_lsgan: boolean norm: 'instance' or 'batch' learning_rate: float, initial learning rate for Adam beta1: float, momentum term of Adam ngf: number of gen filters in first conv layer """ self.lambda1 = lambda1 self.lambda2 = lambda2 self.use_lsgan = use_lsgan use_sigmoid = not use_lsgan self.batch_size = batch_size self.image_width = image_width self.image_height = image_height self.learning_rate = learning_rate self.beta1 = beta1 self.X_train_file = X_train_file self.Y_train_file = Y_train_file self.is_training = tf.placeholder_with_default(True, shape=[], name='is_training') self.G = Generator('G', self.is_training, ngf=ngf, norm=norm, image_width=image_width, image_height=image_height) self.D_Y = Discriminator('D_Y', self.is_training, norm=norm, use_sigmoid=use_sigmoid) self.F = Generator('F', self.is_training, norm=norm, image_width=image_width, image_height=image_height) self.D_X = Discriminator('D_X', self.is_training, norm=norm, use_sigmoid=use_sigmoid) self.fake_x = tf.placeholder( tf.float32, shape=[batch_size, image_width, image_height, 3]) self.fake_y = tf.placeholder( tf.float32, shape=[batch_size, image_width, image_height, 3])
help="from authorization token for download data", ) options = parser.parse_args() if options.from_session_id: x = Xingzhe( session_id=str(options.mobile_or_token), user_id=str(options.password_or_user_id), ) else: x = Xingzhe( mobile=str(options.mobile_or_token), password=str(options.password_or_user_id), ) x.login_by_password() generator = Generator(SQL_FILE) old_tracks_ids = generator.get_old_tracks_ids() tracks = x.get_old_tracks() new_tracks = [i for i in tracks if str(i["id"]) not in old_tracks_ids] print(f"{len(new_tracks)} new activities to be downloaded") async def download_new_activities(): await gather_with_concurrency( 3, [x.download_xingzhe_gpx(track) for track in new_tracks]) loop = asyncio.get_event_loop() future = asyncio.ensure_future(download_new_activities()) loop.run_until_complete(future) make_activities_file(SQL_FILE, GPX_FOLDER, JSON_FILE)