def parse_command_line(): parser = argparse.ArgumentParser( description="""Train, validate, and test a classifier that will detect clouds in remote sensing data.""") parser.add_argument("-p", "--prepare-data", help="Prepare training and validation data.", action="store_true") parser.add_argument("-t", "--train", help="""Train classifier. Use --graph to generate quality graphs""", action="store_true") parser.add_argument("-g", "--graph", help="Generate training graphs.", action="store_true") parser.add_argument("--weights", help="""The trained model weights to use; if not provided defaults to the network that was just trained""", type=str, default=None) parser.add_argument("--note", help="Adds extra note onto generated quality graph.", type=str) args = vars(parser.parse_args()) if os.environ.get("CAFFE_HOME") == None: print "You must set CAFFE_HOME to point to where Caffe is installed. Example:" print "export CAFFE_HOME=/usr/local/caffe" exit(1) # Ensure the random number generator always starts from the same place for consistent tests. random.seed(0) if args["prepare_data"] == True: prepare_data(); if args["train"] == True: train(args["graph"], weight_file=args["weights"], note=args["note"])
def main(params): # c ^= coarse, f ^= fine (Xtr, ytr_c, ytr_f), (Xva, yva_c, yva_f), (Xte, yte_c, yte_f), (le_c, le_f) = df.datasets.cifar100.data() model = cnn(32, df.Parallel( df.Sequential(df.Linear(1024, 20, init=df.init.const(0)), df.SoftMax()), df.Sequential(df.Linear(1024,100, init=df.init.const(0)), df.SoftMax()), ) ) crit = df.ParallelCriterion( (0.75, df.ClassNLLCriterion()), df.ClassNLLCriterion() # No weight defaults to one. ) crit.add_penalty(1e-4, df.L1WeightDecay(model)) crit.add_penalty(1e-5, df.L2WeightDecay(model)) optim = df.AdaDelta(params['adadelta_rho']) for epoch in range(100): model.training() train(Xtr, ytr_c, ytr_f, model, optim, crit, epoch, params['batch_size'], 'train') train(Xtr, ytr_c, ytr_f, model, optim, crit, epoch, params['batch_size'], 'stats') model.evaluate() validate(Xva, yva_c, yva_f, model, epoch, params['batch_size'])
def load_train_save(options, adt, pbt, sfx, save_dir): options_path = os.path.join(save_dir, "options") save_dict_csv(options_path, options) if options['load_params'] is True: adt.load_params(options['params_file']) if options['save_params'] is True: path = os.path.join(save_dir, "final" + sfx) adt.save_params(path) if options['train'] is True: train(adt, pbt, num_epochs=options['num_epochs'], sfx=sfx, save_dir=save_dir, save_every=options['save_every'])
def test_arch(p): acc = [] auc = [] prec = [] rec = [] f1 = [] plot_data = [] filename = ''.join(choice(ascii_uppercase) for i in range(6)) with open('arch/'+root_filename+'_'+str(p['test_fraction'])+'_'+filename+'.txt', 'w') as outfile: json.dump(p, outfile) for i in range(0,nb_rand): result = train(batch_size=p['batch_size'], dropout=p['dropout'], nb_hidden=p['layers'], path=p['data'][0], test_fraction=p['test_fraction'], classifier=p['classifier'][0], nb_epoch=p['nb_epoch'], bidirectional=p['bidirectional'], rnn_type=p['rnn'], save_model=p['save_model'], plot_loss=p['plot_loss'], filename=root_filename+'_'+filename, plot_data=plot_data, optimizer=p['optimizer'], nb_augment=p['nb_augment']) acc.append(result[0]) auc.append(result[1]) prec.append(result[2].tolist()) rec.append(result[3].tolist()) f1.append(result[4]) plot_data = result[5] p['acc'] = acc p['auc'] = auc p['prec'] = prec p['rec'] = rec p['f1'] = f1 with open('arch/'+root_filename+'_'+str(p['test_fraction'])+'_'+filename+'.txt', 'w') as outfile: json.dump(p, outfile)
def main(params): train_set, valid_set, test_set = df.datasets.mnist.data() train_set_x, train_set_y = train_set test_set_x, test_set_y = test_set model = lenet() criterion = df.ClassNLLCriterion() optimiser = df.SGD(lr=params['lr']) for epoch in range(100): model.training() train(train_set_x, train_set_y, model, optimiser, criterion, epoch, params['batch_size'], 'train') train(train_set_x, train_set_y, model, optimiser, criterion, epoch, params['batch_size'], 'stats') model.evaluate() validate(test_set_x, test_set_y, model, epoch, params['batch_size'])
def main(): args = parse_arguments() if args.sub == "crawl": lc = LawCrawler(args.domain, args.num_process, args.verbose) data = lc.start() with open(args.output, "w") as w: w.write("{0}" .format(data)) if args.sub == "word2vec": # To store result files if not os.path.exists("result/"): os.makedirs("result/") # Store/load preprocessed data try: with open("result/preprocess", "rb") as pre_data: print("Use preprocessd data.") word_dict = pickle.load(pre_data) word2idx = pickle.load(pre_data) except IOError as e: word2idx, word_dict = preprocess(args) with open("result/preprocess", "wb") as pre_data: pickle.dump(word_dict, pre_data) pickle.dump(word2idx, pre_data) start_time = time.time() result = train(word2idx, args) end_time = time.time() print("Train word2vec done. {0:.2f} sec." .format(end_time-start_time)) # Store trained data (word vector). with open("result/"+args.result, "wb") as w2v_data: pickle.dump(result, w2v_data) if args.sub == "vis": try: with open("result/result", "rb") as w2v_data: result = pickle.load(w2v_data) with open("result/preprocess", "rb") as pre_data: word_dict = pickle.load(pre_data) except IOError as e: print("No result files.") log.close() start_time = time.time() visualization(result, word_dict) end_time = time.time() print("t-SNE and visualization done. {0:.2f} sec." .format(end_time-start_time))
def main(): i = neurons(3) h = neurons(5) o = neurons(1) n = network() n += e_hidden(i, h) n += e_out(h, o) x = xor_dataset() t = train(n) while True: print t.train(x)
def run(template, path): cifar = prepare_cifar10() cifar_train = cifar.train cifar_train_stream = cifar.train_stream cifar_validation = cifar.validation cifar_validation_stream = cifar.validation_stream cifar_test = cifar.test cifar_test_stream = cifar.test_stream print("Compiling...", end=" ") sys.stdout.flush() network = compile(template) print("DONE") sys.stdout.flush() train(network, cifar_train_stream, cifar_validation_stream, 1e-3, 0.7, path) print("Test error rate is %f%%" % (compute_error_rate(cifar_test_stream, network.predict) * 100.0,))
def main(): # args should include the following in sequence: # file path for negative words # file path for positive words # directory of negative training/testing examples # directory of positive training/testing examples # partial=True means model test on testing split; otherwise on training split # neg_hand=True means negation handling is enabled # bigram_hand=True means bigram handling is enabled partial = False neg_hand = False bigram_hand = False args = ['/Users/tianshuren/Google Drive/10701/Homework 1/opinion-lexicon-English/negative-words.txt', '/Users/tianshuren/Google Drive/10701/Homework 1/opinion-lexicon-English/positive-words.txt', '/Users/tianshuren/Google Drive/10701/Homework 1/review_polarity/txt_sentoken/neg/*', '/Users/tianshuren/Google Drive/10701/Homework 1/review_polarity/txt_sentoken/pos/*', partial, neg_hand, bigram_hand] N_Voc = args[0] P_Voc = args[1] N_Train = args[2] P_Train = args[3] N_Test = N_Train P_Test = P_Train config = [partial, neg_hand, bigram_hand] bigrams = ['extremely', 'quite', 'just', 'almost', 'very', 'too', 'enough'] print 'building vocabulary...' vocab = build(N_Voc, P_Voc) print 'training neg files at ' + N_Train + '; pos files at ' + P_Train + '...' (num_neg, num_pos, vocab) = train(N_Train, P_Train, vocab, bigrams, config) print 'testing on negative files at ' + N_Test + '...' (neg_correct, neg_total) = test(N_Test, 'negative', vocab, bigrams, num_neg, num_pos, config) print 'testing on positive files at ' + P_Test + '...' (pos_correct, pos_total) = test(P_Test, 'positive', vocab, bigrams, num_neg, num_pos, config) total_accuracy = float(neg_correct + pos_correct) / (neg_total + pos_total) print 'total accuracy is %f' % total_accuracy
def getanswer(msg,query,orig_query): if msg == "<train status>": ans = train(query) elif msg == "<weather status>": ans = weather(query) elif msg == "<cricinfo module>": ans = cric_info(orig_query) elif msg == "<festival module>": ans = festival(orig_query) elif msg == "<tennis module>": ans = tennis(orig_query) elif msg == "<car module>": ans=car(orig_query) elif msg == "<tank module>": ans=tank(orig_query) elif msg == "<disease module>": ans=disease(orig_query) elif msg == "<flight module>": ans = flight(orig_query) elif msg == "<recipe module>": ans = recipe(orig_query) elif msg=="<discography module>": ans=discography(orig_query) elif msg=="<electronic module>": ans=electronic(orig_query) elif msg == "<wiki module>": ans = wiki_module(orig_query) elif msg == "<bank module>": ans = bank_module(orig_query) #elif msg == "<restaurant module>": #ans = restaurant(orig_query) #elif msg == "<website module>": # ans = get_website(orig_query) elif msg == "<stock status>": ans = stock(query) elif msg == "<mineral status>": ans = mineral(query) elif msg == "<sports status>": ans = sports(query) elif msg == "<movie review>": ans = movie(query,orig_query) elif msg == "<exam status>": ans = exam(query) elif msg == "<locationcentric module>": ans = location(query) elif msg == "<highway module>": ans = highway(query) elif msg == "<differences module>": ans = differences(orig_query) elif msg == "<currency module>": ans = currency(query) elif msg == "<meanings module>": ans= meanings(orig_query) elif msg == "<theatre module>": ans= theatre(orig_query) elif msg == "<minister module>": ans= minister(orig_query) elif msg == "<std module>": ans= std(query) elif msg == "<bday module>": ans= [{"bday":[]}] elif msg == "<highcourt module>": ans = highcourt(orig_query) #print "here" return ans
def main(root_dir, logdir): #------------------------------------------------------------------------------- # SETUP #------------------------------------------------------------------------------- global_step = tf.Variable(0,trainable=False,name="global_step") keep_prob = tf.placeholder(tf.float32, name="drop_prob") l_r = tf.placeholder(tf.float32, name="learning_rate") images_ = tf.placeholder(tf.float32, shape=[BATCH_SIZE,ROWS,COLS,DEPTH],name="image_placeholder") labels_ = tf.placeholder(tf.int64, shape=[None], name="labels_plaeholder") #read inputs filenames_list = [os.path.join(root_dir, f) for f in os.listdir(root_dir) if os.path.isfile(os.path.join(root_dir, f)) and f[-8:] == 'tfrecord'] filenames_train = [f for f in filenames_list if "39_42" not in f] random.shuffle(filenames_train) filenames_val = [f for f in filenames_list if "39_42" in f] random.shuffle(filenames_val) print('Going to train on ', filenames_train) print('Going to test on ', filenames_val) #train_batch images,labels = read_and_decode_single_example(filenames_train, IMAGE_SHAPE, ONLY_DEPTH) #images = randomFlips(images) images_batch,labels_batch = getShuffledMiniBatch(BATCH_SIZE,images,labels) #val batch images_v,labels_v = read_and_decode_single_example(filenames_val, IMAGE_SHAPE, ONLY_DEPTH) images_batch_v, labels_batch_v = getShuffledMiniBatch(BATCH_SIZE,images_v,labels_v) #visualize image in tensorboard tf.image_summary('images',images_batch) #models model = buildDeepNet(images_,keep_prob, BATCH_SIZE,CLASSES,WORKING_SHAPE) #weights to handle unbalanced training set #weights = tf.constant([NEG_EXAMPLE_TRAIN/TOTAL_EXAMPLE,POS_EXAMPLE_TRAIN/TOTAL_EXAMPLE]) weights = tf.ones([1,CLASSES],tf.float32) #loss loss = computeLoss(model,labels_,weights,False) #train train_op = train(loss,global_step,l_r) with tf.device("/cpu:0"): #create a saver to save and restore models saver = tf.train.Saver(tf.all_variables()) #accuracy op softmaxed = tf.nn.softmax(model) correct_prediction = tf.equal(tf.arg_max(softmaxed,1), labels_) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #placeholder to print average_loss average_pl = tf.placeholder(tf.float32) average_summary = tf.scalar_summary("average_loss", average_pl) #placeholder to print average validation accuracy average_val_acc = tf.placeholder(tf.float32) val_summary = tf.scalar_summary("average_validation_accuracy",average_val_acc) #placeholder to print average train accuracy average_train_acc = tf.placeholder(tf.float32) val_summary = tf.scalar_summary("average_train_accuracy",average_train_acc) #init operation init = tf.initialize_all_variables() #summary operation for visualization summary_op = tf.merge_all_summaries() #----------------------------------------------------------------------------------------------- # TRAINING #----------------------------------------------------------------------------------------------- with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: #logging folder if not os.path.exists(logdir+'/log/'): os.makedirs(logdir+'/log/') if not os.path.exists(logdir+'/weights/'): os.makedirs(logdir+'/weights/') sum_writer = tf.train.SummaryWriter(logdir+'/log/',sess.graph) step = 0 ckpt = tf.train.get_checkpoint_state(logdir+'/weights/') if ckpt and ckpt.model_checkpoint_path: print('Founded valid checkpoint file at: '+ckpt.model_checkpoint_path) #restore variable saver.restore(sess, ckpt.model_checkpoint_path) #restore step step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]) print('Restored %d step model'%(step)) else: print('No checkpoint file found, initialization instead') #init all variables sess.run(init) #start queue tf.train.start_queue_runners(sess=sess) best_loss = np.inf lr_countdown = LR_COUNTDOWN working_lr = LEARNING_RATE losses_history = [] val_acc_history = [] train_acc_history = [] while step < 40000: start_time = time.time() train_images, train_labels = sess.run([images_batch,labels_batch]) _, loss_value = sess.run([train_op, loss], feed_dict={ keep_prob:0.5, l_r:working_lr, images_:train_images, labels_:train_labels }) losses_history.append(loss_value) duration = time.time() - start_time assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: num_examples_per_step = BATCH_SIZE examples_per_sec = num_examples_per_step / duration sec_per_batch = float(duration) #print(losses_history,'\n',sum(losses_history),'\n',len(losses_history),'\n') format_str = ('%s: epoch %d, step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)') print (format_str % (datetime.now(), step/STEP_PER_EPOCH, step, loss_value,examples_per_sec, sec_per_batch)) #compute validation score, save average loss val_images, val_labels = sess.run([images_batch_v,labels_batch_v]) val_accuracy = sess.run(accuracy, feed_dict={ keep_prob:1.0, l_r:working_lr, images_:val_images, labels_:val_labels }) train_accuracy=sess.run(accuracy, feed_dict={ keep_prob:1.0, l_r:working_lr, images_:train_images, labels_:train_labels }) val_acc_history.append(val_accuracy) train_acc_history.append(train_accuracy) if step % 100 == 0: #print('Losses_avg: ',sum(losses_history),'\n',len(losses_history),'\n') avg_loss = sum(losses_history)/len(losses_history) avg_val = sum(val_acc_history)/len(val_acc_history) avg_train = sum(train_acc_history)/len(train_acc_history) if avg_loss<best_loss: best_loss = avg_loss lr_countdown=LR_COUNTDOWN else: lr_countdown-=1 if lr_countdown==0: best_loss = avg_loss working_lr = working_lr/2 lr_countdown = LR_COUNTDOWN summary_str = sess.run(summary_op, feed_dict={ keep_prob:1.0, l_r:working_lr, images_:val_images, labels_:val_labels, average_pl: avg_loss, average_val_acc: avg_val, average_train_acc: avg_train }) print("step %d, avg validation accuracy %g, avg train accuracy %g, avg loss %g, learning rate %g, countdown %d"%(step,avg_val,avg_train, avg_loss, working_lr, lr_countdown)) sum_writer.add_summary(summary_str, step) losses_history = [] val_acc_history = [] train_acc_history = [] # Save the model checkpoint periodically. if step % 1000 == 0 or (step + 1) == 10000 or (step<1000 and step%100==0): checkpoint_path = os.path.join(logdir+'/weights/', 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) # Increase step step+=1
ret5 = fully(ret4, [1024, 1], [1]) with tf.variable_scope("q1"): retq1 = fully(ret4, [1024, 128], [128]) with tf.variable_scope("q_u"): retq_u = tf.tanh(fully(retq1, [128, 1], [1])) with tf.variable_scope("q_c"): retq_c = tf.nn.softmax(fully(retq1, [128, cat_num], [cat_num])) retq = [retq_u, retq_c] return ret5, retq def q_loss_computation(noise, retq): [retq_u, retq_c] = retq global cat_num loss_u = 2 * tf.reduce_mean( tf.square(noise[:, -1 - cat_num:-cat_num] - retq_u)) noise_oh = noise[:, -cat_num:] q_relevant = tf.reduce_max(noise_oh * retq_c, axis=1) loss_c = tf.reduce_mean(-tf.log(q_relevant)) return (loss_c + loss_u) / 2 train(generator, discriminator, tf_noise_generator, cpu_noise_generator, cpu_noise_generator_tricky, q_loss_computation)
DEVICE = t.device('cuda') map_location = None else: DEVICE = t.device('cpu') map_location = 'cpu' args.DEVICE = DEVICE save_dir = os.path.join(os.getcwd(), args.output_dir) # 构建两个相同结构的net,参数定期同步 RolloutNet = AttentionModel(args) RolloutNet = RolloutNet.to(DEVICE) baseNet = AttentionModel(args) baseNet = baseNet.to(DEVICE) baseNet.load_state_dict(RolloutNet.state_dict()) is_train = True # 是 if is_train: if args.optimizer == 'adam': optimizer = optim.Adam(RolloutNet.parameters(), lr=args.lr) elif args.optimizer == 'sgd': optimizer = optim.SGD(RolloutNet.parameters(), lr=args.lr) elif args.optimizer == 'rmsprop': optimizer = optim.RMSprop(RolloutNet.parameters(), lr=args.lr) else: raise ValueError('optimizer undefined: ', args.optimizer) # 训练部分 train(args, optimizer, baseNet, RolloutNet) else: # 测试部分 evaluate(args, RolloutNet, map_location)
def main(): args = arg_parse() print_args(args) print("==> Creating dataloader...") data_dir = args.data_path train_list = './list/image/train.txt' train_set = get_train_set(data_dir, train_list, args) train_list1 ='./list/video/train.txt' train_set1 = get_train_set(data_dir, train_list1, args) train_list2 = './list/audio/train.txt' train_set2 = get_train_set(data_dir, train_list2, args) train_list3 = './list/text/train.txt' train_set3 = get_text_set(data_dir, train_list3, args, 'train') test_list = './list/image/test.txt' test_set = get_test_set(data_dir, test_list, args) test_list1 = './list/video/test.txt' test_set1 = get_test_set(data_dir, test_list1, args) test_list2 = './list/audio/test.txt' test_set2 = get_test_set(data_dir, test_list2, args) test_list3 = './list/text/test.txt' test_set3 = get_text_set(data_dir, test_list3, args, 'test') test_loader=DataLoader(dataset=test_set, num_workers=args.workers, batch_size=args.batch_size, shuffle=False) test_loader1=DataLoader(dataset=test_set1, num_workers=args.workers, batch_size=args.batch_size, shuffle=False) test_loader2=DataLoader(dataset=test_set2, num_workers=args.workers, batch_size=args.batch_size, shuffle=False) test_loader3=DataLoader(dataset=test_set3, num_workers=args.workers, batch_size=args.batch_size, shuffle=False) print("==> Loading the network ...") model = resnet50(num_classes=200) center_loss = CenterLoss(200, 200, True) if args.gpu is not None: # model = nn.DataParallel(model, device_ids=range(args.gpu)) model = model.cuda() cudnn.benchmark = True if True: # os.path.isfile(args.snapshot): print("==> loading checkpoint '{}'".format(args.snapshot)) checkpoint = torch.load(args.snapshot) model_dict = model.state_dict() restore_param = {k: v for k, v in checkpoint.items() if k in model_dict} model_dict.update(restore_param) model.load_state_dict(model_dict) print("==> loaded checkpoint '{}'".format(args.snapshot)) else: print("==> no checkpoint found at '{}'".format(args.snapshot)) # exit() criterion = nn.CrossEntropyLoss() # '''只训练指定层''' #for i, v in center_loss.named_parameters(): # v.requires_grad = False # for i, v in model.named_parameters(): # if i != 'embed.weight' and i != "conv01.weight" \ # and i != "conv01.bias" and i != "conv02.weight" \ # and i != "conv02.bias" and i != "conv0.weight" \ # and i != 'conv0.bias'and i != 'fc1.weight'and i != 'fc1.bias': # v.requires_grad = False params = list(model.parameters()) + list(center_loss.parameters()) optimizer = optim.SGD(filter(lambda p: p.requires_grad,params), lr=0.001, momentum=0.9) for name, param in model.named_parameters(): if param.requires_grad: print(name) for name, param in center_loss.named_parameters(): if param.requires_grad: print(name) scheduler = lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.8) savepath = args.model_path if not os.path.exists(savepath): os.makedirs(savepath) if True: print('-' * 20) print("Video Acc:") text_acc = validate(test_loader1, model, args,'v') exit() for epoch in range(args.epochs): scheduler.step() train_loader = DataLoader(dataset=train_set, num_workers=args.workers, batch_size=args.batch_size, shuffle=True) train_loader1 = DataLoader(dataset=train_set1, num_workers=args.workers, batch_size=args.batch_size, shuffle=True) train_loader2 = DataLoader(dataset=train_set2, num_workers=args.workers, batch_size=args.batch_size, shuffle=True) train_loader3 = DataLoader(dataset=train_set3, num_workers=args.workers, batch_size=args.batch_size, shuffle=True) train(train_loader, train_loader1, train_loader2, train_loader3, args, model, criterion, center_loss, optimizer, epoch, args.epochs) if(epoch%1==0): print("Image Acc:") image1_acc = validate(test_loader, model, args, 'i') print("Audio Acc:") image_acc = validate(test_loader2, model, args, 'a') print("Video Acc:") text_acc = validate(test_loader1, model, args,'v') print("Text Acc:") image_acc = validate(test_loader3, model, args, 't') save_model_path = savepath + 'epoch_' + str(epoch) + '_' + str(image_acc) + '.pkl' torch.save(model.state_dict(), save_model_path)
from train import * if __name__ == '__main__': train = Train("E:\mtcnn_data", 12) train(10000) # train = Train("D:\celeba_3", 24) # train(10000)
print("===> Building model") model = MODEL(args) criterion = nn.MSELoss() print("===> Setting GPU") if cuda: model = model.cuda() criterion = criterion.cuda() # optionally resume from a checkpoint if args.resume: if os.path.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint["epoch"] + 1 model.load_state_dict(checkpoint["model"].state_dict()) else: print("=> no checkpoint found at '{}'".format(args.resume)) print("===> Setting Optimizer") optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr, betas=(0.9, 0.99), eps=1e-08) print("===> Training") for epoch in range(args.start_epoch, args.nEpochs + 1): train(data_loader, optimizer, model, criterion, epoch, args) save_checkpoint(model, epoch, 1)
val_ds = logSpecData(val_path, classes, sample_ratio=subsample) print(f'{len(train_ds.img_paths)} training samples, {len(val_ds.img_paths)} validation samples') print(f'classes {classes}') weights, weight_dict = get_sampler_weights(train_ds) sampler = WeightedRandomSampler(weights, len(weights)) train_dl = DataLoader(train_ds, 128, sampler=sampler, drop_last=True, num_workers=8) valid_dl = DataLoader(val_ds, 128, num_workers=8) #define network #distribute module to gpu to work on parallel net = cnn_trad_pool2_net() net.cuda() print(f'Training...') # print(f'Network ready') # if torch.cuda.is_available(): # net.cuda() # n_devices = torch.cuda.device_count() # print(f'Running model on {n_devices} GPUs\n') # net = nn.DataParallel(net) # else: print(f'Running model on cpu') # define optimizer and criterion criterion = F.cross_entropy optimizer = optim.SGD(net.parameters(),lr=0.001, momentum=0.9, weight_decay=0.0001) #optimizer = optim.Adam(net.parameters(), lr=0.01) training_loss = train(net, train_dl, valid_dl, criterion, optimizer, epochs)
def run_seq2seq(train_data, test_data, word2index, index2word, word_embeddings, out_fname, encoder_params_file, decoder_params_file, max_target_length, n_epochs, batch_size, n_layers): # Initialize q models print 'Initializing models' encoder = EncoderRNN(HIDDEN_SIZE, word_embeddings, n_layers, dropout=DROPOUT) decoder = AttnDecoderRNN(HIDDEN_SIZE, len(word2index), word_embeddings, n_layers) # Initialize optimizers encoder_optimizer = optim.Adam( [par for par in encoder.parameters() if par.requires_grad], lr=LEARNING_RATE) decoder_optimizer = optim.Adam( [par for par in decoder.parameters() if par.requires_grad], lr=LEARNING_RATE * DECODER_LEARNING_RATIO) # Move models to GPU if USE_CUDA: encoder.cuda() decoder.cuda() # Keep track of time elapsed and running averages start = time.time() print_loss_total = 0 # Reset every print_every # epoch = 0.0 epoch = 60.0 print 'Loading encoded, decoder params' encoder.load_state_dict( torch.load(encoder_params_file + '.epoch%d' % epoch)) decoder.load_state_dict( torch.load(decoder_params_file + '.epoch%d' % epoch)) input_seqs, input_lens, output_seqs, output_lens = train_data n_batches = len(input_seqs) / batch_size teacher_forcing_ratio = 1.0 # decr = teacher_forcing_ratio/n_epochs while epoch < n_epochs: epoch += 1 for input_seqs_batch, input_lens_batch, \ output_seqs_batch, output_lens_batch in \ iterate_minibatches(input_seqs, input_lens, output_seqs, output_lens, batch_size): start_time = time.time() # Run the train function loss = train(input_seqs_batch, input_lens_batch, output_seqs_batch, output_lens_batch, encoder, decoder, encoder_optimizer, decoder_optimizer, word2index[SOS_token], max_target_length, batch_size, teacher_forcing_ratio) # Keep track of loss print_loss_total += loss # teacher_forcing_ratio = teacher_forcing_ratio - decr print_loss_avg = print_loss_total / n_batches print_loss_total = 0 print_summary = '%s %d %.4f' % (time_since( start, epoch / n_epochs), epoch, print_loss_avg) print(print_summary) print 'Epoch: %d' % epoch if epoch % 10 == 0: print 'Saving model params' torch.save(encoder.state_dict(), encoder_params_file + '.epoch%d' % epoch) torch.save(decoder.state_dict(), decoder_params_file + '.epoch%d' % epoch) if (epoch > n_epochs - 10) or (epoch % 10 == 0): out_file = open(out_fname + '.epoch%d' % int(epoch), 'w') evaluate(word2index, index2word, encoder, decoder, test_data, batch_size, out_file)
def trainIters(encoder, decoder, vocab, n_iters, print_every=1000, plot_every=100, batch_size=32, learning_rate=0.01, teacher_forcing_ratio=1.0): start = time.time() plot_losses = [] plot_scores = [] print_loss_total = 0 # Reset every print_every plot_loss_total = 0 # Reset every plot_every encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate) decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate) # create dataloader trainset = SpellingLoader('train', vocab) trainloader = data.DataLoader(trainset, batch_size, shuffle=True) criterion = nn.CrossEntropyLoss() max_score = 0.8785 for iter in range(1, n_iters + 1): for input_tensor, target_tensor in trainloader: input_tensor = input_tensor.to(device) target_tensor = target_tensor.to(device) loss = train(input_tensor, target_tensor, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, teacher_forcing_ratio) print_loss_total += loss plot_loss_total += loss # evaluate and save model _, avg_bleu = evaluate(encoder, decoder, vocab) plot_scores.append(avg_bleu) if avg_bleu > max_score: max_score = avg_bleu print("Model save...") torch.save(encoder, "./models/encoder_{:.4f}.ckpt".format(avg_bleu)) torch.save(decoder, "./models/decoder_{:.4f}.ckpt".format(avg_bleu)) if iter % print_every == 0: print_loss_avg = print_loss_total / print_every print_loss_total = 0 print('%s (%d %d%%) Loss: %.4f BLEU: %.4f' % (timeSince(start, iter / n_iters), iter, iter / n_iters * 100, print_loss_avg, avg_bleu)) plot_losses.append(plot_loss_total) plot_loss_total = 0 print("The highest score is %s" % max_score) return plot_scores, plot_losses
help='then crop to this size') parser.add_argument('--n_epoch_init', type=int, default=10, help='# of iter at pretrained') parser.add_argument('--n_epoch', type=int, default=2000, help='# of iter at training') parser.add_argument('--beta1', type=float, default=0.9, help='momentum term of adam') parser.add_argument('--lr_init', type=float, default=1e-4, help='initial learning rate for pretrain') parser.add_argument('--lr_decay', type=float, default=0.1, help='initial learning rate for adam') parser.add_argument('--train', type=bool, default=True, help='train or valid') opt = parser.parse_args() print(opt) if opt.train: train(opt) else: valid(opt)
from time import localtime, strftime import logging, scipy import tensorflow as tf import tensorlayer as tl from model import * from train import * from evaluate import * from utils import * from config import config, log_config if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('--mode', type=str, default='srgan', help='srgan, evaluate') args = parser.parse_args() tl.global_flag['mode'] = args.mode if tl.global_flag['mode'] == 'srgan': train() elif tl.global_flag['mode'] == 'evaluate': evaluate() else: raise Exception("Unknow --mode")
for p in model.parameters()) / 1000000.0)) print('Total params: %.2fk' % (sum(p.numel() for p in model.parameters()) / 1000.0)) print('************') print(model) #============================================================================== # Start training #============================================================================== model, optimizer, epoch_hist = train(model, train_loader, lr=args.lr, weight_decay=args.wd, lamb=args.lamb, num_epochs=args.epochs, learning_rate_change=args.lr_decay, epoch_update=args.lr_update, nu=args.nu, eta=args.eta, backward=args.backward, steps=args.steps, steps_back=args.steps_back, gradclip=args.gradclip) torch.save(model.state_dict(), args.folder + '/model' + '.pkl') #****************************************************************************** # Prediction #****************************************************************************** Xinput, Xtarget = Xtest[:-1], Xtest[1:] _, Xtarget = Xtest_clean[:-1], Xtest_clean[1:]
from train import * from sklearn.cross_validation import KFold from tensorflow.python.framework import ops df = df.sample(frac=1) kf = KFold(df.shape[0], n_folds=10, shuffle=True) epoch_number = 1 i = 0 print(df.shape) logger.info('start validation') step = 0 for train_index, test_index in kf: train_set = df.iloc[train_index, :] test_set = df.iloc[test_index, :] ops.reset_default_graph() with tf.Session() as sess: graphs = build_graph(sess) train(train_set, './models/validate', epoch_number, graphs, sess) test(test_set, './models/validate', graphs, sess, str(step)) step += 1
def test_rmse_should_be_less_than_1900000(self): score = train() self.assertLess(score, 1900000)
def main(): train()
weights, weight_dict = get_sampler_weights(train_ds) sampler = WeightedRandomSampler(weights, len(weights)) train_dl = DataLoader(train_ds, 128, sampler=sampler, drop_last=True, num_workers=8) valid_dl = DataLoader(val_ds, 128, num_workers=8) #define network #distribute module to gpu to work on parallel net = cnn_trad_pool2_net() net.cuda() print(f'Training...') # print(f'Network ready') # if torch.cuda.is_available(): # net.cuda() # n_devices = torch.cuda.device_count() # print(f'Running model on {n_devices} GPUs\n') # net = nn.DataParallel(net) # else: print(f'Running model on cpu') # define optimizer and criterion criterion = F.cross_entropy optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9, weight_decay=0.0001) #optimizer = optim.Adam(net.parameters(), lr=0.01) training_loss = train(net, train_dl, valid_dl, criterion, optimizer, epochs)
print("\tconstructing datasets and network...") training_tweets, training_users, training_seq_lengths, valid_tweets, valid_users, valid_seq_lengths, test_tweets, test_users, test_seq_lengths = partite_dataset(tweets, users, seq_lengths) #hyperparameter optimization if it is set if FLAGS.optimize == False: #print specs print("---TRAINING STARTED---") model_specs = "with parameters: Learning Rate:" + str(FLAGS.learning_rate) + ", Regularization parameter:" + str(FLAGS.l2_reg_lambda) + ", fully connected size:" model_specs+= str(FLAGS.fc_size) + ", language:" + FLAGS.lang print(model_specs) #run the network tf.reset_default_graph() net = network(embeddings_char, embeddings_word) train(net, training_tweets, training_users, training_seq_lengths, valid_tweets, valid_users, valid_seq_lengths, \ target_values, vocabulary_word, vocabulary_char, embeddings_char, embeddings_word) else: for i in range(len(FLAGS.rnn_cell_sizes)): for learning_rate in FLAGS.l_rate: for regularization_param in FLAGS.reg_param: #prep the network tf.reset_default_graph() FLAGS.learning_rate = learning_rate FLAGS.l2_reg_lambda = regularization_param FLAGS.rnn_cell_size = FLAGS.rnn_cell_sizes[i] FLAGS.num_filters = FLAGS.cnn_filter_counts[i] net = network(embeddings_char, embeddings_word) #print specs
model.cuda() criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=learning_rate) def callback(model, epoch): torch.save( model.state_dict(), "./checkpoints/{:s}_trial{:d}_epoch{:d}.pt".format( name, trial + 1, epoch + 1)) train_accuracy[:, trial], test_accuracy[:, trial], train_loss[:, trial] = train( model, criterion, optimizer, trainloader, testloader, n_epochs, device, callback) print("Trial took {:.1f} seconds".format(time.time() - trial_start)) torch.save(train_accuracy, './{:s}_train_accuracy_t{:d}.pt'.format(name, n_trials)) torch.save(test_accuracy, './{:s}_test_accuracy_t{:d}.pt'.format(name, n_trials)) torch.save(train_loss, './{:s}_train_loss_t{:d}.pt'.format(name, n_trials)) #
def main(): args = arg_parse() print_args(args) print("==> Creating dataloader...") data_dir = args.data_path train_list = './list/image/train.txt' train_loader = get_train_set(data_dir, train_list, args) train_list1 = './list/video/train.txt' train_loader1 = get_train_set(data_dir, train_list1, args) train_list = './list/audio/train.txt' train_loader2 = get_train_set(data_dir, train_list, args) train_list3 = './list/text/train.txt' train_loader3 = get_text_set(data_dir, train_list3, args, 'train') test_list = './list/image/test.txt' test_loader = get_test_set(data_dir, test_list, args) test_list1 = './list/video/test.txt' test_loader1 = get_test_set(data_dir, test_list1, args) test_list = './list/audio/test.txt' test_loader2 = get_test_set(data_dir, test_list, args) test_list3 = './list/text/test.txt' test_loader3 = get_text_set(data_dir, test_list3, args, 'test') print("==> Loading the network ...") model = resnet50(num_classes=200) if args.gpu is not None: model = nn.DataParallel(model, device_ids=range(args.gpu)) model = model.cuda() cudnn.benchmark = True if os.path.isfile(args.snapshot): print("==> loading checkpoint '{}'".format(args.snapshot)) checkpoint = torch.load(args.snapshot) model_dict = model.state_dict() restore_param = {k: v for k, v in checkpoint.items() if k in model_dict} model_dict.update(restore_param) model.load_state_dict(model_dict) print("==> loaded checkpoint '{}'".format(args.snapshot)) else: print("==> no checkpoint found at '{}'".format(args.snapshot)) exit() criterion = nn.CrossEntropyLoss() center_loss = CenterLoss(200, 200, True) params = list(model.parameters()) + list(center_loss.parameters()) optimizer = optim.SGD(params, lr=0.001, momentum=0.9) scheduler = lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.5) savepath = args.model_path if not os.path.exists(savepath): os.makedirs(savepath) for epoch in range(args.epochs): scheduler.step() train(train_loader, train_loader1, train_loader2, train_loader3, args, model, criterion, center_loss, optimizer, epoch, args.epochs) print('-' * 20) print("Image Acc:") image_acc = validate(test_loader, model, args, False) print("Text Acc:") text_acc = validate(test_loader3, model, args, True) save_model_path = savepath + 'epoch_' + str(epoch) + '_' + str(image_acc) +'.pkl' torch.save(model.state_dict(), save_model_path)
parser.add_argument("--ny", default=256, type=int, dest="ny") parser.add_argument("--nx", default=256, type=int, dest="nx") parser.add_argument("--nch", default=3, type=int, dest="nch") parser.add_argument("--nker", default=64, type=int, dest="nker") parser.add_argument("--wgt_cycle", default=1e1, type=float, dest="wgt_cycle") parser.add_argument("--wgt_ident", default=5e-1, type=float, dest="wgt_ident") parser.add_argument("--norm", default='inorm', type=str, dest="norm") parser.add_argument("--network", default="PGGAN", choices=['DCGAN', 'pix2pix', 'CycleGAN','PGGAN'], type=str, dest="network") parser.add_argument("--learning_type", default="plain", choices=["plain", "residual"], type=str, dest="learning_type") args = parser.parse_args() if __name__ == "__main__": if args.mode == "train_single": train(0,1,args) elif args.mode == "train_multi": gpu_devices = ','.join([str(id) for id in args.gpu_devices]) os.environ["CUDA_VISIBLE_DEVICES"] = gpu_devices ngpus_per_node = torch.cuda.device_count() args.world_size = ngpus_per_node * args.world_size mp.spawn(train, nprocs=ngpus_per_node, args=(ngpus_per_node, args)) elif args.mode == "test": test(args)
args['class_num'] = len(LABEL.vocab) args['embedding_matrix'] = TEXT.vocab.vectors args["lr"] = 1e-3 args["log"] = False args['epochs'] = 200 args["val_interval"] = 100 args["features_per_size"] = [100, 100, 100] args["kernel_sizes"] = [3, 4, 5] args["dropout_rate"] = 0.5 args["log_interval"] = 100 args["cuda"] = torch.cuda.is_available() args["static"] = False args["rand"] = True rand_model = textCNN(args) train(rand_model, train_iter, val_iter, args) rand_model.load_state_dict(torch.load("rand_model.pt")) _, test_acc = eval(rand_model, val_iter, args) print("accuracy over test set:{:.6f}".format(test_acc)) args["rand"] = False args["static"] = True static_model = textCNN(args) train(static_model, train_iter, val_iter, args) static_model.load_state_dict(torch.load("static_model.pt")) _, test_acc = eval(static_model, val_iter, args) print("Accuracy on test set:{:.6f}".format(test_acc)) args["static"] = False non_static_model = textCNN(args) train(non_static_model, train_iter, val_iter, args)
def main(): if not torch.cuda.is_available(): print('no gpu device available') sys.exit(1) num_gpus = torch.cuda.device_count() np.random.seed(args.seed) args.gpu = args.local_rank % num_gpus torch.cuda.set_device(args.gpu) cudnn.benchmark = True cudnn.deterministic = True torch.manual_seed(args.seed) cudnn.enabled = True torch.cuda.manual_seed(args.seed) group_name = 'search_space_shrinking' print('gpu device = %d' % args.gpu) print("args = %s", args) torch.distributed.init_process_group(backend='nccl', init_method='env://', group_name=group_name) args.world_size = torch.distributed.get_world_size() args.batch_size = args.batch_size // args.world_size criterion_smooth = CrossEntropyLabelSmooth(args.classes, args.label_smooth).cuda() total_iters = args.epochs * per_epoch_iters # Max shrinking iterations iters = config.op_num # Prepare data train_loader = get_train_dataloader(args.train_dir, args.batch_size, args.local_rank, total_iters) train_dataprovider = DataIterator(train_loader) operations = [] for _ in range(config.edges): operations.append(list(range(config.op_num))) print('operations={}'.format(operations)) # Prepare model base_model = Network_ImageNet().cuda(args.gpu) model, seed = get_warmup_model(train_dataprovider, criterion_smooth, operations, per_epoch_iters, args.seed, args) print('arch = {}'.format(model.module.architecture())) optimizer, scheduler = get_optimizer_schedule(model, args, total_iters) start_iter, ops_dim = 0, 0 checkpoint_tar = config.checkpoint_cache if os.path.exists(checkpoint_tar): checkpoint = torch.load( checkpoint_tar, map_location={'cuda:0': 'cuda:{}'.format(args.local_rank)}) start_iter = checkpoint['iter'] + 1 seed = checkpoint['seed'] operations = checkpoint['operations'] model.load_state_dict(checkpoint['state_dict']) now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print('{} load checkpoint..., iter = {}, operations={}'.format( now, start_iter, operations)) # Reset the scheduler cur_iters = (config.first_stage_epochs + (start_iter - 1) * config.other_stage_epochs ) * per_epoch_iters if start_iter > 0 else 0 for _ in range(cur_iters): if scheduler.get_lr()[0] > args.min_lr: scheduler.step() # Save the base weights for computing angle if start_iter == 0 and args.local_rank == 0: torch.save(model.module.state_dict(), config.base_net_cache) print('save base weights ...') for i in range(start_iter, iters): print('search space size: {}'.format( get_search_space_size(operations))) # ABS finishes when the size of search space is less than the threshold if get_search_space_size( operations) <= config.shrinking_finish_threshold: # Save the shrunk search space pickle.dump(operations, open(args.operations_path, 'wb')) break per_stage_iters = config.other_stage_epochs * per_epoch_iters if i > 0 else config.first_stage_epochs * per_epoch_iters seed = train(train_dataprovider, optimizer, scheduler, model, criterion_smooth, operations, i, per_stage_iters, seed, args) if args.local_rank == 0: # Search space shrinking load(base_model, config.base_net_cache) operations = ABS(base_model, model.module, operations, i) now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print('{} |=> iter = {}, operations={}, seed={}'.format( now, i + 1, operations, seed)) save_checkpoint( { 'operations': operations, 'iter': i, 'state_dict': model.state_dict(), 'seed': seed }, config.checkpoint_cache) operations = merge_ops(operations) ops_dim = len(operations) # Synchronize variable cross multiple processes ops_dim = broadcast(obj=ops_dim, src=0) if not args.local_rank == 0: operations = np.zeros(ops_dim, dtype=np.int) operations = broadcast(obj=operations, src=0) operations = split_ops(operations)
def main(): train(0.01)
patience=3, verbose=True) min_valid_loss = 2147483647.0 train_losses = [] valid_losses = [] train_acs = [] valid_acs = [] for ep in range(epochs): print("Epoch {}".format(ep + 1)) train_loss, train_ac = train(model, optimizer, train_dataloader) print("Training Loss:{}\tTraining Accuracy:{}".format( train_loss, train_ac)) train_losses.append(train_loss) train_acs.append(train_ac) valid_loss, valid_ac = evaluate(model, valid_dataloader) print("Validaiton Loss:{}\tValidation Accuracy:{}".format( valid_loss, valid_ac)) if valid_loss < min_valid_loss: min_valid_loss = valid_loss torch.save(model.state_dict(), "../../model/Task1/best_model") valid_losses.append(valid_loss)
def main(): train(0.0001) # print predict([55.48216114069585,35.57070347228866]) predict()
parser.add_argument('--lr', type=float, default=2e-4) args = parser.parse_args() if __name__ == "__main__": # devie--------------------------------------------------------------------------- device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Fix random seed--------------------------------------------------------------------------- torch.manual_seed(1) torch.cuda.manual_seed_all(1) # dataset------------------------------------------------------------------------------------ train_dataloader = getDataLoader(args.dataset, args.batch_size, args.dataset_path) # model------------------------------------------------------------------------------------ model = build_model(args.experiment, image_size=args.image_size, lr=args.lr) model = checkpointNet.load_part_network(model, args.checkpoint, args.which_epoch) model = model.to(device) # save_dir_path----------------------------------------------------------------------------------- save_dir_path = os.path.join(args.save_path, args.dataset) os.makedirs(save_dir_path, exist_ok=True) # train ----------------------------------------------------------------------------------- train(train_dataloader, model, device, save_dir_path, args)
from __future__ import print_function from train import * import unet K.set_image_dim_ordering('th') # Theano dimension ordering in this code history, model_weights = train(50) model = unet.get_unet(dropout=True) if inner: model = unet.get_unet_inner(dropout=True) model.load_weights(model_weights) plot_loss_history(history) test, uncertainty = evaluate_model(model)
target_optim = optim.Adam(target_net.parameters(), lr=lr) shadow_net = shadow_net_type().to(device) shadow_net.apply(models.weights_init) shadow_loss = nn.CrossEntropyLoss() shadow_optim = optim.Adam(shadow_net.parameters(), lr=lr) attack_net = models.mlleaks_mlp(n_in=k).to(device) attack_net.apply(models.weights_init) attack_loss = nn.BCELoss() attack_optim = optim.Adam(attack_net.parameters(), lr=lr) train(shadow_net, shadow_train_loader, cifar10_testloader, shadow_optim, shadow_loss, n_epochs, classes=classes) train_attacker(attack_net, shadow_net, shadow_train_loader, shadow_out_loader, attack_optim, 0.01, n_epochs=1, k=k) # train_attacker(attack_net, shadow_net, shadow_train_loader, shadow_out_loader, attack_optim, attack_loss, n_epochs=1, k=k) train(target_net, target_train_loader, cifar10_testloader,
from train import * from tools import * from libsvm.svmutil import svm_train, svm_save_model from ml_ops import search, train, bootstrap import argparse, sys, datetime modes = ['train', 'bootstrap', 'search'] parser = argparse.ArgumentParser() parser.add_argument("-m", "--mode", help="specify the mode [-m train|bootstrap|search]", required=True, choices=modes) args = parser.parse_args() if args.mode == 'train': hists, labels = train() svm = svm_train(labels, hists, '-s 0 -t 0 -c 1') model_name = 'svm_trained_' + str(datetime.datetime.now()).replace(' ', '_') + '.dat' svm_save_model(model_name, svm) if args.mode == 'bootstrap': hists, labels = train() hists, labels = bootstrap(hists, labels) svm = svm_train(labels, hists, '-s 0 -t 0 -c 1') model_name = 'svm_bootstrapped_' + str(datetime.datetime.now()).replace(' ', '_') + '.dat' svm_save_model(model_name, svm) if args.mode == 'search': search()
errors[m + t] = [] for i in range(args['repeats']): print 'Model:', m print 'Augmentation:', t print 'Repeat:', i + 1 modelfn = os.path.join(args['modelpath'], m) err, _, _ = train( modelfn=modelfn, trainfn=args['trainfn'], valfn=args['valfn'], epochs=model_epochs[m], batchsize=128, opt='Adam', opt_kwargs={'alpha': 0.001}, net_kwargs={}, transformations=t, val_freq=25, save_freq=25, seed=i, gpu=0, silent=False, logme=None ) errors[m + t].append(err) print '-- Evaluation run complete --' print 'Model:', m, print 'augmentations:', t print 'error rates:', errors[m + t] print 'mean error rate:', np.mean(errors[m + t]), '+/-', np.std(errors[m + t])
train_file_path = os.path.expanduser('~/.keras/datasets/VOC2012/combined_imageset_train.txt') #Data/VOClarge/VOC2012/ImageSets/Segmentation # train_file_path = os.path.expanduser('~/.keras/datasets/oneimage/train.txt') #Data/VOClarge/VOC2012/ImageSets/Segmentation val_file_path = os.path.expanduser('~/.keras/datasets/VOC2012/combined_imageset_val.txt') data_dir = os.path.expanduser('~/.keras/datasets/VOC2012/VOCdevkit/VOC2012/JPEGImages') label_dir = os.path.expanduser('~/.keras/datasets/VOC2012/combined_annotations') if dataset is 'VOC2012': train_file_path = os.path.expanduser('~/.keras/datasets/VOC2012/VOCdevkit/VOC2012/ImageSets/Segmentation/train.txt') #Data/VOClarge/VOC2012/ImageSets/Segmentation # train_file_path = os.path.expanduser('~/.keras/datasets/oneimage/train.txt') #Data/VOClarge/VOC2012/ImageSets/Segmentation val_file_path = os.path.expanduser('~/.keras/datasets/VOC2012/VOCdevkit/VOC2012/ImageSets/Segmentation/val.txt') data_dir = os.path.expanduser('~/.keras/datasets/VOC2012/VOCdevkit/VOC2012/JPEGImages') label_dir = os.path.expanduser('~/.keras/datasets/VOC2012/VOCdevkit/VOC2012/SegmentationClass') classes = 21 class_weight = None elif dataset is 'COCO': train_file_path = os.path.expanduser('~/.keras/datasets/coco/annotations/train2014.txt') #Data/VOClarge/VOC2012/ImageSets/Segmentation # train_file_path = os.path.expanduser('~/.keras/datasets/oneimage/train.txt') #Data/VOClarge/VOC2012/ImageSets/Segmentation val_file_path = os.path.expanduser('~/.keras/datasets/coco/annotations/test2014.txt') data_dir = os.path.expanduser('~/.keras/datasets/coco/train2014') label_dir = os.path.expanduser('~/.keras/datasets/coco/seg_mask/train2014') stats_file = os.path.expanduser('~/.keras/datasets/coco/seg_mask/train2014/image_segmentation_class_stats.json') classes = 91 # class_weight = data_coco.class_weight(image_segmentation_stats_file=stats_file) class_weight = None config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)) session = tf.Session(config=config) K.set_session(session) train(batch_size, epochs, lr_base, lr_power, weight_decay, classes, model_name, train_file_path, val_file_path, data_dir, label_dir, target_size=target_size, batchnorm_momentum=batchnorm_momentum, resume_training=resume_training, class_weight=class_weight, dataset=dataset)
def run_sim(params=None,parent=None): if not params: params=default_params() elif isinstance(params,basestring): fname=params d=zpickle.load(fname) params=d['params'] if parent: save_sim_file=params['tmpfile'] params['display']=1 else: save_sim_file=params['save_sim_file'] if params['load_sim_file']==params['tmpfile']: params['load_sim_file']='' if not save_sim_file: save_sim_file='sims/untitled.dat' # Deal the random number seed here if params['random_seed']=='clock': numpy.random.seed(None) else: numpy.random.seed(params['random_seed']) params['actual_random_seed']=numpy.random.get_state() params['random_seed2']=int(round(numpy.random.rand()*1e6)) start_time=time.time() end_time=None sim_time=None num_channels=len(params['pattern_input']) num_moments=get_num_moments(params) layer=0 test_stimulus=params['test_stimulus'][layer]['type'] total_num_inputs=0 for p in params['pattern_input']: total_num_inputs=total_num_inputs+p['num_inputs'] num_neurons=prod(params['num_neurons']) if params['load_sim_file']: d=zpickle.load(params['load_sim_file']) load_sim_params=d['params'] weights=d['weights'] moments=d['moments'] initial_weights=d['weights'] initial_moments=d['moments'] t_mat=None if params['continue']: t_mat=d['t_mat'] moments_mat=d['moments_mat'] response_mat=d['response_mat'] weights_mat=d['weights_mat'] if not t_mat: moments_mat=None response_mat=None else: params['initial_weights']=weights params['initial_moments']=moments else: params['continue']=0 full_mask=[] for p in params['pattern_input']: full_mask.append(get_circle_mask(p['num_inputs'])) full_mask=numpy.array(full_mask).ravel() if not params['initial_weights']: params['initial_weights']=[numpy.asarray(numpy.random.rand(num_neurons,total_num_inputs)*(params['initial_weight_range'][1]-params['initial_weight_range'][0])+params['initial_weight_range'][0],numpy.float64)] params['initial_weights'][layer]=params['initial_weights'][layer]*(full_mask.repeat(num_neurons)) initial_weights=params['initial_weights'] if not params['initial_moments']: params['initial_moments']=[numpy.asarray(numpy.random.rand(num_moments,num_neurons)*(params['initial_moment_range'][1]-params['initial_moment_range'][0])+params['initial_moment_range'][0],numpy.float64)] initial_moments=params['initial_moments'] moments_mat=numpy.dstack( (initial_moments[0],) ) t_mat=None if not t_mat: # not loaded from file t_mat=[0] start_epoch=1 moments_mat=numpy.dstack( (initial_moments[0],) ) if params['keep_every_epoch']: weights_mat=[initial_weights] else: weights_mat=[] response_mat=[] response_var=[] test_stimulus_type=params['test_stimulus'][layer]['type'] if test_stimulus_type==1: # test OR single response_var=test_OR_single(params,initial_weights) if not response_var: params['test_stimulus'][layer]['type']=0 else: for r in response_var: response_mat.append([r[0]]) else: start_epoch=len(t_mat) test_stimulus_type=params['test_stimulus'][layer]['type'] response_var=[] if test_stimulus_type==1: # test OR single response_var=test_OR_single(params,initial_weights) if not response_var: params['test_stimulus'][layer]['type']=0 else: for r,m in zip(response_var,response_mat): m.append(r[0]) weights=deepcopy(initial_weights) moments=deepcopy(initial_moments) response_var_list=[response_var] extra_mat=[] sim={'params':params, 'weights':weights, 'moments':moments, 'moments_mat':moments_mat, 'weights_mat':weights_mat, 'response_mat':response_mat, 'response_var_list':response_var_list, 'initial_weights':weights, 'initial_moments':moments, 't_mat':t_mat, 'start_time':start_time, 'end_time':end_time, 'sim_time':sim_time, 'extra_mat':extra_mat} params_with_images=deepcopy(params) # make these point to the same thing params_with_images['saved_input_vectors']=params['saved_input_vectors'] fix_images_directories(params_with_images) status('Preemtive Save: %s' % save_sim_file,parent) zpickle.save(sim,save_sim_file) w=weights[layer] m=moments[layer] t0=time.time() for epoch in range(start_epoch,start_epoch+params['epoch_number']): t1=time.time() extra=train(epoch-start_epoch,params_with_images,w,m) if extra: extra_mat.append(extra) # copy over mask stuff if epoch==start_epoch: for i,p in enumerate(params['pattern_input']): params['pattern_input'][i]['mask']=params_with_images['pattern_input'][i]['mask'] dt1=time.time()-t1 dt0=time.time()-t0 frac=(epoch-start_epoch+1.0)/params['epoch_number'] eta=sec2str(round(dt0/frac-dt0)) status("%.4f...ETA %s" % (dt1,eta),parent) sim['moments_mat']=numpy.dstack( (sim['moments_mat'],m) ) test_stimulus_type=params['test_stimulus'][layer]['type'] if test_stimulus_type==1: # test OR single response_var=test_OR_single(params,weights) response_var_list[0]=response_var if not response_var: params['test_stimulus'][layer]['type']=0 else: for r,mat in zip(response_var,response_mat): mat.append(r[0]) t_mat.append(t_mat[-1]+params['iter_per_epoch']) if params['keep_every_epoch']: weights_mat.append(deepcopy(weights)) if params['display'] and epoch%params['epoch_per_display']==0: if parent: parent.Plot(sim) else: Plot(sim) if parent: parent.Yield() if parent.Stopping(): break if dt0>(60*60*20): # every 20 minutes status('Incremental Save: %s' % (save_sim_file),parent) zpickle.save(sim,save_sim_file) end_time=time.time() sim_time=end_time-start_time if params['display']: if parent: parent.Plot(sim) else: Plot(sim) status('Save: %s' % (save_sim_file),parent) zpickle.save(sim,save_sim_file)
nb_epoch = config.getint('Train','num_epochs') batch_size = config.getint('Train','batch_size') nb_augment = config.getint('Data','num_augment') path = config.get('Data','data') classifier_type = config.get('Data','classifier') if classifier_type == 'SN1a': classifier = sn1a_classifier elif classifier_type == '123': classifier = type_classifier elif classifier_type.lower() == 'Sub': classifier = subtype_classifier else: raise Exception('Incorrect classifier') root_filename = config.get('Options','filename') plot_loss = config.getboolean('Options','plot_loss') save_model = config.getboolean('Options','save_model') with open('save/' + root_filename + '.ini', 'w') as f: config.write(f) start_time = time.time() result = train(batch_size=batch_size, dropout=dropout, nb_hidden=nb_hidden, path=path, test_fraction=test_fraction, classifier=classifier, nb_epoch=nb_epoch, bidirectional=bidirectional, rnn_type=rnn_type, save_model=save_model, plot_loss=plot_loss, filename=root_filename, optimizer=optimizer, nb_augment=nb_augment, consensus=consensus) print("--- %s seconds ---" % (time.time() - start_time))
criterion = nn.NLLLoss(reduce=False) #Define a learning rate optimizer encoder_lr_decay_scheduler= optim.lr_scheduler.ReduceLROnPlateau(encoder_optimizer, factor=0.5,patience=5) decoder_lr_decay_scheduler= optim.lr_scheduler.ReduceLROnPlateau(decoder_optimizer,factor=0.5,patience=5) #Start Training start = time.time() print_loss_total = 0 #Reset every print_every iter_count = 0 for epoch in range(1,n_epochs + 1): for batch_x,batch_y,batch_x_lengths,batch_y_lengths,_ in data_generator(train_data_index,batch_size): input_variable = batch_x #B*W_x target_variable = batch_y #B*W_y #Run the train function loss = train(input_variable, target_variable, batch_x_lengths,batch_y_lengths,encoder, decoder, encoder_optimizer, decoder_optimizer, criterion) print_loss_total += loss if iter_count == 0: iter_count += 1 continue if iter_count%print_every == 0: print_loss_avg = print_loss_total / print_every print_loss_total = 0 print_summary = '%s (%d %d%%) %.4f' % (time_since(start, iter_count / n_epochs / batch_num), iter_count, iter_count / n_epochs / batch_num * 100, print_loss_avg) print(print_summary) if iter_count%eval_every == 0: #Print the Bleu Score and loss for Dev Dataset val_losses = 0
textCreate(pygame, screen, "Your average pace: %s, Your standard deviation: %s" %(mean_now, int(std_now)), white) pygame.time.delay(4000) textCreate(pygame, screen,"You are done! Thank you!", white) elif trial == 10 and first == 0: textCreate(pygame, screen, "Your average pace: %s, Your standard deviation: %s" %(mean_now, int(std_now)), white) pygame.time.delay(4000) textCreate(pygame, screen, "The pace will now change to 550ms", white) pygame.time.delay(2000) textCreate(pygame, screen,"Trial complete. Prepare for next trial.", white) elif trial == 10 and first == 1: textCreate(pygame, screen, "Your average pace: %s, Your standard deviation: %s" %(mean_now, int(std_now)), white) pygame.time.delay(4000) textCreate(pygame, screen, "The pace will now change to 350ms", white) pygame.time.delay(2000) textCreate(pygame, screen,"Trial complete. Prepare for next trial.", white) else: textCreate(pygame, screen, "Your average pace: %s, Your standard deviation: %s" %(mean_now, int(std_now)), white) pygame.time.delay(4000) textCreate(pygame, screen,"Trial complete. Prepare for next trial.", white) pygame.time.delay(3000) if __name__ == '__main__': train(pygame, screen) run() createFile(name, trial_type, session, player_times, std, m, r_std, r_m) # Mouse response rate # 500hz - 2ms # plot covariance Fn # if person speeds over time shows + corr need to detrend data # Wing analysis
parser.add_argument( '-ee', '--eval-every', type=int, default=10, help='The number of epochs after which to print the validation loss') parser.add_argument('--cuda', type=bool, default=False, help='Whether to use cuda or not') parser.add_argument('--mode', choices=['train', 'test'], default='train', help='Whether to train or test') FLAGS, unparsed = parser.parse_known_args() FLAGS.cuda = torch.device('cuda:0' if torch.cuda.is_available() and FLAGS.cuda \ else 'cpu') if FLAGS.mode.lower() == 'train': train(FLAGS) elif FLAGS.mode.lower() == 'test': test(FLAGS) else: raise RuntimeError( 'Unknown mode passed. \n Mode passed should be either \ of "train" or "test"')
criterion = BCEDiceLoss if config["optimizer"] == "Adam": optimizer = optim.Adam(params, lr=config["lr"], weight_decay=config["weight_decay"]) else: optimizer = optim.SGD(params, lr=config["lr"], momentum=0.9, weight_decay=config["weight_decay"]) scheduler = CosineAnnealingLR(optimizer, T_max=config["epochs"], eta_min=config["min_lr"]) train_dl, test_dl = get_loader(config["batch_size"], config["num_workers"]) log = train(config, train_dl, test_dl, model, optimizer, scheduler, criterion, metric=iou) # analysis plot_log(log)
#! /usr/local/bin/python3 import os import json _train = True _inference = True from train import * os.environ["SM_MODEL_DIR"] = "./mock/out" os.environ["SM_CHANNEL_TRAIN"] = "./mock/data" if _train: train({"team": "jmc"}, [], 0, out_dir=os.environ["SM_MODEL_DIR"], current_host="local") if _inference: from host import * model = model_fn("./mock/out") body = json.dumps({ "opponent": "test", "width": 10, "height": 10, "ruleSet": { "shipCells": 11, "connectedShipCells": 5 } }) response = transform_fn(model, body, 'image/jpeg', 'image/jpeg') print(response)
# lstm = LSTM_plain(input_size=args.max_prev_node, embedding_size=args.embedding_size_lstm, # hidden_size=args.hidden_size, num_layers=args.num_layers).cuda() if 'GraphRNN_VAE_conditional' in args.note: rnn = GRU_plain(input_size=args.max_prev_node, embedding_size=args.embedding_size_rnn, hidden_size=args.hidden_size_rnn, num_layers=args.num_layers, has_input=True, has_output=False).cuda() output = MLP_VAE_conditional_plain(h_size=args.hidden_size_rnn, embedding_size=args.embedding_size_output, y_size=args.max_prev_node).cuda() elif 'GraphRNN_MLP' in args.note: rnn = GRU_plain(input_size=args.max_prev_node, embedding_size=args.embedding_size_rnn, hidden_size=args.hidden_size_rnn, num_layers=args.num_layers, has_input=True, has_output=False).cuda() output = MLP_plain(h_size=args.hidden_size_rnn, embedding_size=args.embedding_size_output, y_size=args.max_prev_node).cuda() elif 'GraphRNN_RNN' in args.note: rnn = GRU_plain(input_size=args.max_prev_node, embedding_size=args.embedding_size_rnn, hidden_size=args.hidden_size_rnn, num_layers=args.num_layers, has_input=True, has_output=True, output_size=args.hidden_size_rnn_output).cuda() output = GRU_plain(input_size=1, embedding_size=args.embedding_size_rnn_output, hidden_size=args.hidden_size_rnn_output, num_layers=args.num_layers, has_input=True, has_output=True, output_size=1).cuda() ### start training train(args, dataset_loader, rnn, output) ### graph completion # train_graph_completion(args,dataset_loader,rnn,output) ### nll evaluation # train_nll(args, dataset_loader, dataset_loader, rnn, output, max_iter = 200, graph_validate_len=graph_validate_len,graph_test_len=graph_test_len)
opti = optim.Adam(net.parameters(), lr=lr) scheduler = torch.optim.lr_scheduler.StepLR( opti, step_size=step_size) print('Start training, with batch size ', batch_size, ' maxlen ', maxlen, ' random sample ', doRandom_train) print('Proportion of random ', proportionRandom, ', lr ', lr, 'stepSize', step_size, ' Epochs ', epochs) net, train_error_all, val_error_all = train( net, criterion, opti, scheduler, train_loader, val_loader, epochs=epochs, use_cuda=use_cuda) print( "Finished training, best RMSE on the validation was ", np.sqrt(np.max(val_error_all))) res[count] = { 'batch_size': batch_size, 'maxlen': maxlen, 'propRandom': proportionRandom, 'lr': lr, 'step_size': step_size,
def main(train_file, model_file, test_file, test_predict, params): targets, features = read_stack_data(train_file) model = train(targets, features, model_file, params) predict_stacked(model, test_file, test_predict)
gamma = 0.99 env = make_env() obs_shape = env.observation_space.shape n_actions = env.action_space.n print("Observation shape:", obs_shape) print("Num actions:", n_actions) print("Action names:", env.env.env.get_action_meanings()) n_parallel_games = n_parallel_games gamma = gamma from agent import Agent agent = Agent(obs_shape, n_actions, n_parallel_games) if cuda: agent.cuda() chkpt_dir = "./chkpt" pool = EnvPool(agent, make_env, n_parallel_games) train(agent, pool, 50000, n_parallel_games, gamma, save_path=chkpt_dir, curiosity=True)
#!/usr/bin/env python #coding=utf-8 from parameters import * from model import * from train import * if __name__ == '__main__': net = ResNet(ResidualBlock) net.loadIfExist() ''' for elem in net.named_parameters(): print(elem) ''' train(net)
'~/.keras/datasets/coco/annotations/test2014.txt') data_dir = os.path.expanduser('~/.keras/datasets/coco/train2014') label_dir = os.path.expanduser( '~/.keras/datasets/coco/seg_mask/train2014') stats_file = os.path.expanduser( '~/.keras/datasets/coco/seg_mask/train2014/image_segmentation_class_stats.json' ) classes = 91 # class_weight = data_coco.class_weight(image_segmentation_stats_file=stats_file) class_weight = None config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)) session = tf.Session(config=config) K.set_session(session) train(batch_size, epochs, lr_base, lr_power, weight_decay, classes, model_name, train_file_path, val_file_path, data_dir, label_dir, target_size=target_size, batchnorm_momentum=batchnorm_momentum, resume_training=resume_training, class_weight=class_weight, dataset=dataset)