def save_model(sessions_dir, session_id, model): path = get_session_path(sessions_dir, session_id) if not os.path.isdir(path): os.mkdir(path) checkpoint_id = get_max_checkpoint_id(path) + 1 filename = os.path.join(path, f'checkpoint_{session_id}_{checkpoint_id}.vad') model.save(filename)
def save_body(path, d, m, env): if d['type'] == 'leaf': original=copy.copy(m) model.check(path, m) errors = [] if m['to'] and m['from'] and m['to'] < m['from']: errors.extend( ('to','from') ) if (not 'load' in original or original['load'] == '') and (not 'effort' in original or original['effort'] == ''): errors.extend( ('load', 'effort') ) if len(errors) > 0: raise model.ParseException(original, errors) model.create(path) model.save(path, m) return "\n", model.parent(path) if d['type'] == 'list' and 'name' in m: name = m['name'] name = name.replace('?', '') name = name.replace('/', '') name = name.replace('#', '') new_path = path+"/"+name if path.strip() == '': return "\n", path model.create(new_path) if model.describe(new_path)['type'] == 'leaf' or model.describe(new_path)['type'] == 'render': model.save(new_path, { 'name': m['name'] }) return "\n", new_path return None, path
def train(train_dir): image_list, label_list = get_image(train_dir) (train_x, test_x, train_y, test_y) = train_test_split(image_list, label_list, test_size=0.25, random_state=42) train_y = to_categorical(train_y, num_classes=n_classes) test_y = to_categorical(test_y, num_classes=n_classes) # 数据增广 aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest") print("[INFO] Compiling Model...") # network inception_v4 = model.create_inception_v4(img_height, img_height, nb_classes=10, load_weight=False) optimizer = optimizers.Adam(lr=init_learning_rate, decay=init_learning_rate / max_epochs) inception_v4.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) print("[INFO] Training Network...") history = inception_v4.fit_generator(ImageDataGenerator().flow(train_x, train_y, batch_size=batch_size), steps_per_epoch=len(train_x) // batch_size, epochs=max_epochs, validation_data=(test_x, test_y)) print("[INFO] Saving Model...") model_base = 'trained_model' + '.h5' model.save(model_base)
def create(cls, *args, **kwargs): # This is a convenience. Don't override b/c want preserve # ability to create objects with only knowledge of the Model. # This is not a 'new' or '__init__' b/c we may enforce id-map. model = cls.model_class(*args, **kwargs) model.save() return get(model.key)
def save_model(model_qualifier, model, location): name = model_qualifier + "-{}".format( datetime.date.today()) + "-{}".format(time.time()) try: model.save(os.path.join(location, name)) except IOError: print("Exception occured while saving the model to disc.")
def train(mdl, input_data, output, folds, epochs): """ Trains the model on input data :param mdl: compiled keras model :param input_data: data input tuple (data,labels) :param output: output file name for weights :param folds: number of folds to perform over the data (Stratified k-fold) :param epochs: number of epochs to run """ print("Training params:") print(" epochs:", epochs) X, Y = input_data kfold = StratifiedKFold(n_splits=folds, shuffle=True) # random_state=seed filepath = output + "_weights.hdf5" callbacks_list = [] X = np.expand_dims(X, axis=2) # Iterate over kfold for train, test in kfold.split(X, Y): mdl.fit(X[train], Y[train], validation_data=(X[test], Y[test]), epochs=epochs, batch_size=10, verbose=1, callbacks=callbacks_list) model.save(mdl, filepath)
def main(data, model): print("Reading images...") x_train = data.read_images('./data/trainImage256.txt', TRAIN_DATA_SIZE) x_test = data.read_images('./data/testImage256.txt', TEST_DATA_SIZE) y_train = data.read_labels('./data/trainLABEL256.txt', TRAIN_DATA_SIZE) y_test = data.read_labels('./data/testLABEL256.txt', TEST_DATA_SIZE) print("Creating model...") model.create_model(multi_gpu=False) print("Now training...") history = model.training(x_train, y_train, x_test, y_test) accuracy = history.history["accuracy"] loss = history.history["loss"] eval = model.evaluate(x_test, y_test) print("accuracy = " + str(eval)) model.save('./model.h5') if not os.path.exists('./result_keras'): os.mkdir('./result_keras') for i in range(TEST_DATA_SIZE): ret = model.predict(x_test[i, :, :, 0].reshape([1, IMG_SIZE, IMG_SIZE, 1]), 1) np.savetxt('./result_keras/' + str(i) + '.txt', ret[0, :, :, 0]) with open("training_log.txt", "w") as f: for i in range(training_epochs): f.write(str(loss[i]) + "," + str(accuracy[i]) + "\n") ax1 = plt.subplot() ax1.plot(loss, color="blue") ax2 = ax1.twinx() ax2.plot(accuracy, color="orange") plt.show()
def main(): monitoring.restart() try: monitoring.print_config() t_start = time() for i_epoch in range(-c.pre_low_lr, c.n_epochs): if i_epoch < 0: for param_group in model.optim.param_groups: param_group['lr'] = c.lr_init * 1e-1 train_losses = train_epoch(i_epoch) test_losses = train_epoch(i_epoch, test=True) monitoring.show_loss(np.concatenate([train_losses, test_losses])) model.scheduler_step() except: model.save(c.filename_out + '_ABORT') raise finally: print("\n\nTraining took %f minutes\n\n" % ((time()-t_start)/60.)) model.save(c.filename_out)
def save_body(path, d, m, env): if d['type'] == 'leaf': original = copy.copy(m) model.check(path, m) errors = [] if m['to'] and m['from'] and m['to'] < m['from']: errors.extend(('to', 'from')) if (not 'load' in original or original['load'] == '') and (not 'effort' in original or original['effort'] == ''): errors.extend(('load', 'effort')) if len(errors) > 0: raise model.ParseException(original, errors) model.create(path) model.save(path, m) return "\n", model.parent(path) if d['type'] == 'list' and 'name' in m: name = m['name'] name = name.replace('?', '') name = name.replace('/', '') name = name.replace('#', '') new_path = path + "/" + name if path.strip() == '': return "\n", path model.create(new_path) if model.describe(new_path)['type'] == 'leaf' or model.describe( new_path)['type'] == 'render': model.save(new_path, {'name': m['name']}) return "\n", new_path return None, path
def mainmenu(account): while True: view.show_menu(account) selection = view.get_input() if selection == '4': answer = view.quit_input() if answer == "y": model.save() break if answer == "n": view.show_menu(account) elif selection == '1': balance = model.check_balance(account) view.show_balance(balance) elif selection == '2': depbalance = float(view.get_amount_input()) model.deposit(account, depbalance) balance = model.check_balance(account) view.show_balance(balance) elif selection == '3': wdbalance = float(view.get_amount_input()) model.withdraw(account, wdbalance) balance = model.check_balance(account) view.show_balance(balance) else: view.bad_input()
def train(mymodel, myoptimizer, output_dir, epoch, train_dataloader, eval_dataloader, UsingGPU=True, min_f1score=0.8, maxtokeep=3, CVAfterEpoch=2, classnum=3): featuremodel = features.features() if UsingGPU: mymodel = mymodel.cuda() featuremodel = featuremodel.cuda() num_train_steps = int(epoch * len(train_dataloader.dataset) / train_dataloader.batch_size) logger.info("***** Do train *****") logger.info(" Num examples = %d", len(train_dataloader.dataset)) logger.info(" Batch size = %d", train_dataloader.batch_size) logger.info(" Num steps = %d", num_train_steps) global_step = 0 # 共迭代的次数 maxf1score = min_f1score for i in range(1, epoch + 1): logger.info("********epoch:{}********".format(i)) for p in myoptimizer.param_groups: p['lr'] = p['lr'] * 0.8 for batch in train_dataloader: global_step += 1 _features, labels = batch if UsingGPU: _features = _features.cuda() labels = labels.cuda() logist, loss = mymodel(featuremodel(_features), labels) loss.backward() # fgm.attack() # 在embedding上添加对抗扰动 # logist,loss_adv = mymodel(input_ids, segment_ids, input_mask, label_ids) # loss_adv.backward() # 反向传播,并在正常的grad基础上,累加对抗训练的梯度 # fgm.restore() # 恢复embedding参数 myoptimizer.step() myoptimizer.zero_grad() if global_step % 100 == 0: logger.info("step:{}, loss:{:.5f}".format( global_step, loss.data)) if global_step % 500 == 0 and i >= CVAfterEpoch: mymodel.eval() precision, recall, f1 = eval(mymodel, eval_dataloader, classnum, UsingGPU) mymodel.train() logger.info( "step:{}, precision:{:.5f}, recall:{:.5f}, f1:{:.5f}". format(global_step, precision, recall, f1)) if f1 > maxf1score: maxf1score = f1 model.save(mymodel, global_step, output_dir, MaxModelCount=maxtokeep)
def on_epoch_end(self, model): if self.epoch in [1, 5, 10, 15, 20]: output_path = get_tmpfile('{}_epoch{}.model'.format( self.path_prefix, self.epoch)) model.save(output_path) print('model save') print("Epoch #{} end".format(self.epoch)) self.epoch += 1
def spy_on_training_process(model_fname): model_instance = model.load(model_fname) n = model_instance['n'] W = model_instance['W'] b = model_instance['b'] vdW = np.zeros(W.shape) vdb = np.zeros(b.shape) alpha0 = 0.3 beta=0.9 iterations = 100000 decay_rate = 4.0 / iterations pos_trains = 0 x_to_investigate = None for i in range(0, iterations): make_movement_fn = lambda x: model.predict2(W, b, x) ex = make_training_examples(make_movement_fn) X = ex['X'] Y = ex['Y'] x = X[:, 0].reshape(9, 1) if x_to_investigate == None: x_to_investigate = x position_to_investigate = x_to_investigate.reshape(3, 3) if (x == x_to_investigate).all(): position.print_position(position_to_investigate) (_, _, aLbefore) = model.predict3(W, b, x_to_investigate) (dW, db, _) = model.back_propagation(n, W, b, X, Y) alpha = alpha0 / (1.0 + decay_rate * i) vdW = beta * vdW + (1 - beta) * dW vdb = beta * vdb + (1 - beta) * db W = W - alpha * dW b = b - alpha * db if i > 0 and i % 1000 == 0: model.save(n, W, b, model_fname) print('========saved=======') if (x == x_to_investigate).all(): pos_trains += 1 position.print_position(position_to_investigate) (_, _, aLafter) = model.predict3(W, b, x_to_investigate) print('\niteration: %d, position trained times: %d' % (i, pos_trains)) y = Y[:, 0].reshape(9, 1) table = np.concatenate((aLbefore, aLafter, y), axis = 1) print(table)
def train_model_scenario_3(n, model_fname, training_examples_fname, m=0, alpha=0.001, beta=0.9, iterations=10000): debug = False modelInstance = model.load(model_fname) W = modelInstance['W'] b = modelInstance['b'] # (W, b) = model.initializeWeights(n) vdW = np.zeros(W.shape) vdb = np.zeros(b.shape) ex = training.read_training_examples(m, fname=training_examples_fname) X = ex['X'] # assert X.shape == (9, 500) Y = ex['Y'] # assert Y.shape == (9, 500) # L is a number of NN layers # (L = 3 for a model 9x18x18x9) L = len(n) - 1 assert len(W) == L for i in range(0, iterations): # (dW, db) = model.calcGradients(W, b, X, Y) (dW, db, _) = model.back_propagation(n, W, b, X, Y) vdW = beta * vdW + (1 - beta) * dW vdb = beta * vdb + (1 - beta) * db # model.updateWeights(W, dW, b, db, alpha) W = W - alpha * vdW b = b - alpha * vdb if i % 30 == 0: print('iteration ' + str(i)) (aL, _) = model.forward_propagation(W, b, X) cost = model.cost_function(Y, aL) # cost = model.costFunction(Y, A[L]) # print('alpha') # print(alpha) print('cost') print(cost) if debug: if i > 0 and i % 3000 == 0: is_back_prop_correct = model.check_back_propagation(n, W, b, X, Y) if not is_back_prop_correct: print("BP is not correct") exit() if i % 1000 == 0: model.save(n, W, b, model_fname) print('------ end -------') model.save(n, W, b, model_fname)
def main(): parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument('--data-dir', type=str, default='data/corona', help='data directory containing input.txt') parser.add_argument('--rnn-type', type=str, default='GRU', help='RNN type used [GRU|LSTM|SMGU]') parser.add_argument('--live-sample', action='store_true', help='live sample the model after each epoch') parser.add_argument( '--word-tokens', action='store_true', help='whether to model the rnn at word level or char level') parser.add_argument( '--pristine-input', action='store_true', help='do not lowercase or attempt fancy tokenization of input') parser.add_argument('--pristine-output', action='store_true', help='do not detokenize output (word-tokens only)') parser.add_argument('--embedding-size', type=int, default=64, help='size of the embedding') parser.add_argument('--rnn-size', type=int, default=128, help='size of RNN layers') parser.add_argument('--num-layers', type=int, default=1, help='number of layers in the RNN') parser.add_argument('--batch-size', type=int, default=32, help='minibatch size') parser.add_argument('--seq-length', type=int, default=50, help='training sequence length') parser.add_argument( '--seq-step', type=int, default=25, help='how often to pull a training sequence from the data') parser.add_argument('--num-epochs', type=int, default=50, help='number of epochs') args = parser.parse_args() model = MetaModel() model.train(**vars(args)) save(model, args.data_dir)
def save_model_fn(epoch=None, it=None, name=None): if name is None: path = os.path.join(out_dir, 'self', 'ckpts', 'epoch-{}_it-{}'.format(epoch, it)) else: path = os.path.join(out_dir, 'self', 'ckpts', '{}'.format(name)) model.save(sess, path, overwrite=True) print(' saved checkpoint to \'{}\''.format(path))
def train_model(X_train, X_test, y_train, y_test, model): X_train = X_train.reshape(X_train.shape[0], 300, 300, 3) X_test = X_test.reshape(X_test.shape[0], 300, 300, 3) print("X_train.shape=", X_train.shape) print("y_train.shape", y_train.shape) print("X_test.shape=", X_test.shape) print("y_test.shape", y_test.shape) # print(y_train[0]) ''' softmax layer -> output=10개의 노드. 각각이 0부터 9까지 숫자를 대표하는 클래스 이를 위해서 y값을 one-hot encoding 표현법으로 변환 0: 1,0,0,0,0,0,0,0,0,0 1: 0,1,0,0,0,0,0,0,0,0 ... 5: 0,0,0,0,0,1,0,0,0,0 ''' # reformat via one-hot encoding y_train = to_categorical(y_train) y_test = to_categorical(y_test) # print(y_train[0]) # catergorical_crossentropy = using when multi classficiation # metrics = output data type model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # batch_size : see batch_size data and set delta in gradient decsending history = model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=16, epochs=30, verbose=1) plot_loss_curve(history.history) # print(history.history) print("train loss=", history.history['loss'][-1]) print("validation loss=", history.history['val_loss'][-1]) # save model in file # offering in KERAS model.save('model-201611263.model') history_df = pd.DataFrame(history.history) with open("history_data.csv", mode='w') as file: history_df.to_csv(file) return model
def welcomemenu(): while True: view.welcome_menu() selection = view.get_input() if selection == '1': create_account() if selection == '2': login() if selection == '3': model.save() return
def create_account(): while True: model.load() view.create_account() account = random.randint(1, 10000) first_name = view.first_name() last_name = view.last_name() create_pin = view.create_pin() model.create_acct(account, first_name, last_name, create_pin) view.new_account(account) model.save() return
def train_model(model, X_train, y_train, name, config): model.compile(loss="mse", optimizer="adadelta", metrics=['mape']) # early = EarlyStopping(monitor='val_loss', patience=30, verbose=0, mode='auto') hist = model.fit(X_train, y_train, batch_size=config["batch"], epochs=config["epochs"], validation_split=0.1) # 训练中 model.save('models/' + name + '.h5') # df = pd.DataFrame.from_dict(hist.history) # df.to_csv('models/' + name + ' loss.csv', encoding='utf-8', index=False) return model
def train_model_scenario_2(n, model_fname, opponet_model_fname, alpha=0.1, iterations=5000): alpha0 = alpha decay_rate = 0.01 modelInstance = model.load(opponet_model_fname) W0 = modelInstance['W'] b0 = modelInstance['b'] if model_fname == opponet_model_fname: W = W0 b = b0 else: make_movement_fn = lambda x: model.predict2(W0, b0, x) (W, b) = model.initialize_weights(n) for i in range(0, iterations): if model_fname == opponet_model_fname: make_movement_fn = lambda x: model.predict2(W, b, x) ex = training.make_training_examples(make_movement_fn) X = ex['X'] Y = ex['Y'] # displayTrainingExamples(X, Y) # (dW, db) = model.calcGradients(W, b, X, Y) (dW, db, _) = model.back_propagation(n, W, b, X, Y) alpha = alpha0 / (1 + decay_rate * i) model.update_weights(W, dW, b, db, alpha) if i % 100 == 0: print('iteration ' + str(i)) (aL, _) = model.forward_propagation(W, b, X) cost = model.cost_function(Y, aL) print('cost') print(cost) print('alpha') print(alpha) # if i % 1000 == 0: # is_back_prop_correct = model.checkBackPropagation(n, W, b, X, Y) # if not is_back_prop_correct: # print("BP is not correct") # exit() print('------ end -------') model.save(n, W, b, model_fname)
def mainmenu(student): while True: view.show_menu(student) selection = view.get_input() if selection == '3': model.save() return elif selection == '1': newgrade = view.get_grade() model.add_grade(student, newgrade) elif selection == '2': view.show_gpa(model.get_gpa(student)) elif selection == '3': student = view.get_student() elif
def train_wrapper(model): '''Data loader''' train_set = data.DataSet(FLAGS.root_dir, FLAGS.dataset, 'train', FLAGS.batch_size, FLAGS.n_label, data_aug=False, shuffle=True) valid_set = data.DataSet(FLAGS.root_dir, FLAGS.dataset, 'val', FLAGS.batch_size, FLAGS.n_label, data_aug=False, shuffle=False) '''create a tf session for training and validation TODO: to run your model, you may call model.train(), model.save(), model.valid()''' best_accuracy = 0 acc_train = [] acc_valid = [] for step in range(1, FLAGS.max_iteration + 1): if not train_set.has_next_batch(): train_set.init_epoch() batch_x, batch_y = train_set.next_batch() if len(batch_x) == FLAGS.batch_size: loss, acc = model.train(batch_x, batch_y) if step == 1 or step % 10 == 0: print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.4f}".format(loss) + ", Training Accuracy= " + "{:.3f}".format(acc)) if step % FLAGS.valid_iteration == 0: acc_train.append(acc) tot_acc = 0.0 tot_input = 0 while valid_set.has_next_batch(): valid_ims, valid_labels = valid_set.next_batch() loss, acc = model.valid(valid_ims, valid_labels) tot_acc += acc * len(valid_ims) tot_input += len(valid_ims) acc = tot_acc / tot_input valid_set.init_epoch() print("Current Accuracy= " + "{:.3f}".format(acc)) acc_valid.append(acc) if acc > best_accuracy: model.save(step) best_accuracy = acc print("Optimization Finished!")
def post(self, table_name): pprint.pprint("POST {0}".format(table_name)) data = self.request.arguments #for k in self.request.arguments: # data[k] = self.get_argument(k) id = model.save(table_name, data) self.redirect("/{0}".format(table_name))
def mainmenu(student): while True: view.show_menu(student) selection = view.get_input() # print(selection) if selection == '3': model.save() return elif selection == '1': newgrade = view.get_input() model.add_grade(student, newgrade) elif selection == '2': gpa = model.get_gpa(student) view.show_gpa(gpa) else: view.bad_input()
def receive(theModel, src, tag): weights = model.save(theModel) # will be overwritten for i in range(len(weights)): dist.recv(tensor=weights[i], src=src, tag=tag * 100 + i) theModel = model.load(weights, theModel) print("Model received from", src) return theModel
def create_account(): first_name = view.get_f_name() last_name = view.get_l_name() pin = view.get_pin() confirm_pin = view.pin_confirm(pin) if confirm_pin != pin: print("wrong pin") create_account() else: account_number = "N4321" + str(random.randint(100, 1000)) model.new_account(account_number, first_name, last_name, confirm_pin) model.save() view.new_account(account_number) view.welcome_menu() main_menu()
def mainmenu(student): #basically a while loop while True: #This loop doesnt terminate on its own. The break keyword stops the loop view.show_menu(student) #Print students name @ top of menu selection = view.get_input() print(selection) if selection == '4': model.save() #save before exiting return #exits the function. could also use /br elif selection == '1': new_grade = view.get_grade() model.add_grade(student, new_grade) elif selection == '2': pass elif selection == '3': pass else: view.bad_input()
def mainmenu(): while True: view.main_menu() selection = view.get_input() print(selection) if selection == '3': model.save() return elif selection == '1': new_account = view.create_account() model.gen_account() model.add_account() view.account_creation() elif selection == '2': viewm.log_in() else: view.bad_input()
def train_model_scenario_1(n, model_fname, training_examples_fname, m=0, alpha=0.001, iterations=10000): debug = False (W, b) = model.initialize_weights(n) ex = training.read_training_examples(m, fname=training_examples_fname) X = ex['X'] # assert X.shape == (9, 500) Y = ex['Y'] # assert Y.shape == (9, 500) # L is a number of NN layers # (L = 3 for a model 9x18x18x9) L = len(n) - 1 assert len(W) == L for i in range(0, iterations): # (dW, db) = model.calcGradients(W, b, X, Y) (dW, db, _) = model.back_propagation(n, W, b, X, Y) model.update_weights(W, dW, b, db, alpha) if i % 300 == 0: print('iteration ' + str(i)) (aL, _) = model.forward_propagation(W, b, X) cost = model.cost_function(Y, aL) # cost = model.costFunction(Y, A[L]) # print('alpha') # print(alpha) print('cost') print(cost) if debug: if i > 0 and i % 3000 == 0: is_back_prop_correct = model.check_back_propagation(n, W, b, X, Y) if not is_back_prop_correct: print("BP is not correct") exit() print('------ end -------') model.save(n, W, b, model_fname)
def main(): # Training data consits of 60000 images and 60000 labels # Testing data consists of 10000 images and 10000 labels # Each image consits of 784 (28x28) pixels each of which contains a value from # 0 to 255.0 which corresponds to its darkness or lightness. # Each input needs to be a list of numpy arrays to be valid. # Load all of the data print "Loading data..." test_images = data.load_data(LIMITED) train_images = data.load_data(LIMITED, "train-images.idx3-ubyte", "train-labels.idx1-ubyte") print "Normalizing data..." X_train, Y_train = data.convert_image_data(train_images) X_test, Y_test = data.convert_image_data(test_images) X_train = np.array(X_train) Y_train = np.array(Y_train) X_test = np.array(X_test) Y_test = np.array(Y_test) if LOAD == False: print "Building the model..." _model = model.build() else: print "Loading the model..." elements = os.listdir("model") if len(elements) == 0: print "No models to load." else: _model = model.load(elements[len(elements)-1]) if TRAIN == True: print "Training the model..." model.train(_model, X_train, Y_train, X_test, Y_test) if VISUALIZE: model.visualize(_model, test_images, VISUALIZE_TO_FILE) if TRAIN == True: print "Saving the model..." model.save(_model)
def _test_train_generator(self): d = model.MyDriver() (X_train, y_train) = d.load_example_training_data( examples=10, width=8, height=8) # //d.load_traing_data("../data/driving_log.csv")) model = d.simple_network(8, 8) history_object = model.fit_generator(d.train_generator_example( zip(X_train, y_train), 10), steps_per_epoch=10, epochs=10, verbose=1) if 0: model.save('test_train.md5') model = keras.models.load_model('test_train.md5') y = model.predict(X_train) y_index = np.argmax(y, 1) print(y_index) for i in range(0, 10): assert (y_index[i] == i) return
def menu(account_number): while True: first_name = model.get_f_name(account_number) last_name = model.get_l_name(account_number) view.main_menu(account_number, first_name, last_name) selection = view.choice() if selection == "1": model.get_balance(account_number) elif selection == "2": witdraw = float(view.withdraw()) model.withdraw_money(account_number, witdraw) money_left = model.balance(account_number) model.save() if money_left < 0: view.insuf() elif selection == "3": depo = float(view.deposit()) model.add_amount(account_number, depo) model.save() elif selection == "4": model.save() return
def save_email(): form = EmailForm() feed_back = '' global receiver if form.validate_on_submit(): # 提交内容合理则进行的操作 address = form.address.data form.address.data = '' if model.save(address) is True: feed_back = '存入完毕,并且已经发送一个测试邮件,请查收 \n 可能会被误认为垃圾邮件' one_email.test_email(address) else: feed_back = '当前 %s 已存在数据库' % address receiver = address return render_template('save_email.html', form=form, feedback=feed_back)
model.summary() # 打印出模型概况 callbacks = [ModelCheckpoint(MODEL_WEIGHTS_FILE, monitor='val_acc', save_best_only=True)] t0 = time.time() history = model.fit(X_train, train_label, batch_size=BATCH_SIZE, verbose=1, validation_split=VALIDATION_SPLIT, # (X_test, test_label) callbacks=callbacks, nb_epoch=NB_EPOCHS) t1 = time.time() print("Minutes elapsed: %f" % ((t1 - t0) / 60.)) # 将模型和权重保存到指定路径 model.save(model_path) # 加载权重到当前模型 # model = load_model(model_path) # Print best validation accuracy and epoch in valid_set max_val_acc, idx = max((val, idx) for (idx, val) in enumerate(history.history['val_acc'])) print('Maximum accuracy at epoch', '{:d}'.format(idx + 1), '=', '{:.4f}'.format(max_val_acc)) # plot the result import matplotlib.pyplot as plt plt.figure() plt.plot(history.epoch, history.history['acc'], label="acc") plt.plot(history.epoch, history.history['val_acc'], label="val_acc") plt.scatter(history.epoch, history.history['acc'], marker='*') plt.scatter(history.epoch, history.history['val_acc'])
else: model.store_transition_in_replay_memory(state, action, reward, next_state, episode_ends) loss = model.replay_experience() if loss == -1.0: pass # burn out else: sum_loss += loss if bootstrapped is False: exploration_rate = model.decrease_exploration_rate() if episode_ends is True: break if episode % dump_freq == 0 and episode != 0: if bootstrapped: print "episode:", episode, "reward:", sum_reward / float(dump_freq * env.max_episodes), "loss:", sum_loss / float(dump_freq), "optimal:", num_optimal_episodes else: print "episode:", episode, "reward:", sum_reward / float(dump_freq * env.max_episodes), "loss:", sum_loss / float(dump_freq), "eps:", "%.4f" % exploration_rate, "optimal:", num_optimal_episodes sum_reward = 0 sum_loss = 0 if episode % save_freq == 0 and episode != 0: model.save() if episode_rewards == 10: num_optimal_episodes += 1 if num_optimal_episodes == 100: break print "num_optimal_episodes:", num_optimal_episodes print "total_time:", time.time() - start_time
def start_save_frame(self, save_frame_heading): assert self._current_save is None self._current_save = model.save() save_name = save_frame_heading[save_frame_heading.find('_')+1:] self._current_block[save_name] = self._current_save
def train(sess, model, hps, logdir, visualise): _print(hps) _print('Starting training. Logging to', logdir) _print('epoch n_processed n_images ips dtrain dtest dsample dtot train_results test_results msg') # Train sess.graph.finalize() n_processed = 0 n_images = 0 train_time = 0.0 test_loss_best = 999999 if hvd.rank() == 0: train_logger = ResultLogger(logdir + "train.txt", **hps.__dict__) test_logger = ResultLogger(logdir + "test.txt", **hps.__dict__) tcurr = time.time() for epoch in range(1, hps.epochs): t = time.time() train_results = [] for it in range(hps.train_its): # Set learning rate, linearly annealed from 0 in the first hps.epochs_warmup epochs. lr = hps.lr * min(1., n_processed / (hps.n_train * hps.epochs_warmup)) # Run a training step synchronously. _t = time.time() train_results += [model.train(lr)] if hps.verbose and hvd.rank() == 0: _print(n_processed, time.time()-_t, train_results[-1]) sys.stdout.flush() # Images seen wrt anchor resolution n_processed += hvd.size() * hps.n_batch_train # Actual images seen at current resolution n_images += hvd.size() * hps.local_batch_train train_results = np.mean(np.asarray(train_results), axis=0) dtrain = time.time() - t ips = (hps.train_its * hvd.size() * hps.local_batch_train) / dtrain train_time += dtrain if hvd.rank() == 0: train_logger.log(epoch=epoch, n_processed=n_processed, n_images=n_images, train_time=int( train_time), **process_results(train_results)) if epoch < 10 or (epoch < 50 and epoch % 10 == 0) or epoch % hps.epochs_full_valid == 0: test_results = [] msg = '' t = time.time() # model.polyak_swap() if epoch % hps.epochs_full_valid == 0: # Full validation run for it in range(hps.full_test_its): test_results += [model.test()] test_results = np.mean(np.asarray(test_results), axis=0) if hvd.rank() == 0: test_logger.log(epoch=epoch, n_processed=n_processed, n_images=n_images, **process_results(test_results)) # Save checkpoint if test_results[0] < test_loss_best: test_loss_best = test_results[0] model.save(logdir+"model_best_loss.ckpt") msg += ' *' dtest = time.time() - t # Sample t = time.time() if epoch == 1 or epoch == 10 or epoch % hps.epochs_full_sample == 0: visualise(epoch) dsample = time.time() - t if hvd.rank() == 0: dcurr = time.time() - tcurr tcurr = time.time() _print(epoch, n_processed, n_images, "{:.1f} {:.1f} {:.1f} {:.1f} {:.1f}".format( ips, dtrain, dtest, dsample, dcurr), train_results, test_results, msg) # model.polyak_swap() if hvd.rank() == 0: _print("Finished!")