Example #1
0
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
Example #2
0
 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))
Example #3
0
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)
Example #4
0
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)
Example #5
0
 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
Example #6
0
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!")
Example #7
0
		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
Example #8
0
	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'])