コード例 #1
0
ファイル: main.py プロジェクト: jolinxql/LSTM-Attention
def train(cost, error_rate, batch_size=100, num_epochs=150):
    # Setting Loggesetr
    timestr = time.strftime("%Y_%m_%d_at_%H_%M")
    save_path = 'results/memory_' + timestr
    log_path = os.path.join(save_path, 'log.txt')
    os.makedirs(save_path)
    fh = logging.FileHandler(filename=log_path)
    fh.setLevel(logging.DEBUG)
    logger.addHandler(fh)

    # Training
    blocks_model = Model(cost)
    all_params = blocks_model.parameters
    print "Number of found parameters:" + str(len(all_params))
    print all_params

    training_algorithm = GradientDescent(
        cost=cost, parameters=all_params,
        step_rule=Adam(learning_rate=0.001))

    # training_algorithm = GradientDescent(
    #     cost=cost, params=all_params,
    #     step_rule=Scale(learning_rate=model.default_lr))

    monitored_variables = [cost, error_rate]

    # the rest is for validation
    # train_data_stream, valid_data_stream = get_mnist_streams(
    #     50000, batch_size)
    train_data_stream, valid_data_stream = get_mnist_video_streams(batch_size)

    train_monitoring = TrainingDataMonitoring(
        variables=monitored_variables,
        prefix="train",
        after_epoch=True)

    valid_monitoring = DataStreamMonitoring(
        variables=monitored_variables,
        data_stream=valid_data_stream,
        prefix="valid",
        after_epoch=True)

    main_loop = MainLoop(
        algorithm=training_algorithm,
        data_stream=train_data_stream,
        model=blocks_model,
        extensions=[
            train_monitoring,
            valid_monitoring,
            FinishAfter(after_n_epochs=num_epochs),
	    SaveParams('valid_misclassificationrate_apply_error_rate', blocks_model, save_path),
            SaveLog(save_path, after_epoch=True),
	    ProgressBar(),
            Printing()])
    main_loop.run()
コード例 #2
0
def train(cost, error_rate, batch_size=100, num_epochs=150):
    # Setting Loggesetr
    timestr = time.strftime("%Y_%m_%d_at_%H_%M")
    save_path = 'results/memory_' + timestr
    log_path = os.path.join(save_path, 'log.txt')
    os.makedirs(save_path)
    fh = logging.FileHandler(filename=log_path)
    fh.setLevel(logging.DEBUG)
    logger.addHandler(fh)

    # Training
    blocks_model = Model(cost)
    all_params = blocks_model.parameters
    print "Number of found parameters:" + str(len(all_params))
    print all_params

    training_algorithm = GradientDescent(cost=cost,
                                         parameters=all_params,
                                         step_rule=Adam(learning_rate=0.001))

    # training_algorithm = GradientDescent(
    #     cost=cost, params=all_params,
    #     step_rule=Scale(learning_rate=model.default_lr))

    monitored_variables = [cost, error_rate]

    # the rest is for validation
    # train_data_stream, valid_data_stream = get_mnist_streams(
    #     50000, batch_size)
    train_data_stream, valid_data_stream = get_mnist_video_streams(batch_size)

    train_monitoring = TrainingDataMonitoring(variables=monitored_variables,
                                              prefix="train",
                                              after_epoch=True)

    valid_monitoring = DataStreamMonitoring(variables=monitored_variables,
                                            data_stream=valid_data_stream,
                                            prefix="valid",
                                            after_epoch=True)

    main_loop = MainLoop(
        algorithm=training_algorithm,
        data_stream=train_data_stream,
        model=blocks_model,
        extensions=[
            train_monitoring, valid_monitoring,
            FinishAfter(after_n_epochs=num_epochs),
            SaveParams('valid_misclassificationrate_apply_error_rate',
                       blocks_model, save_path),
            SaveLog(save_path, after_epoch=True),
            ProgressBar(),
            Printing()
        ])
    main_loop.run()
コード例 #3
0
hyperparameters["batched_window"] = True

cropper = LocallySoftRectangularCropper(
    patch_shape=patch_shape,
    hyperparameters=hyperparameters,
    kernel=Gaussian())

patch = cropper.apply(
    x.reshape((1, 1,) + image_shape),
    np.array([list(image_shape)]),
    location,
    scale)

f = theano.function([x, location, scale], patch, allow_input_downcast=True)

tds, vds = get_mnist_video_streams(1)
image = tds.get_epoch_iterator().next()[0][10, 0, :]

patch1 = f(image, [[60.0, 65.0]], [[1.0, 1.0]])[0, 0]
patch2 = f(image, [[60.0, 65.0]], [[2.0, 2.0]])[0, 0]
patch3 = f(image, [[50.0, 50.0]], [[0.28, 0.28]])[0, 0]

plt.imshow(
    image.reshape(image_shape),
    cmap=plt.gray(),
    interpolation='nearest')
plt.savefig('img0.png')

plt.imshow(
    patch1.reshape(patch_shape),
    cmap=plt.gray(),
コード例 #4
0
ファイル: main.py プロジェクト: mohammadpz/LSTM-Attention
def train(cost, monitorings, batch_size=100, num_epochs=500):
    # Setting Loggesetr
    timestr = time.strftime("%Y_%m_%d_at_%H_%M")
    save_path = 'results/test_' + timestr
    log_path = os.path.join(save_path, 'log.txt')
    os.makedirs(save_path)
    fh = logging.FileHandler(filename=log_path)
    fh.setLevel(logging.DEBUG)
    logger.addHandler(fh)

    # Training
    blocks_model = Model(cost)
    all_params = blocks_model.parameters
    print "Number of found parameters:" + str(len(all_params))
    print all_params

    # grads = T.grad(cost, all_params)
    # from blocks.graph import ComputationGraph
    # cg = ComputationGraph(cost)
    # f = theano.function(cg.inputs, grads)
    # tds, vds = get_mnist_video_streams(100)
    # data = tds.get_epoch_iterator().next()
    # res = f(data[1], data[0])
    # res_norm = [np.mean(np.abs(r)) for r in res]
    # params_dicts = blocks_model.get_parameter_dict()
    # for e1, e2 in zip(params_dicts, res_norm):
    #     print str(e1) + ": " + str(e2)
    # import ipdb; ipdb.set_trace()

    clipping = StepClipping(threshold=np.cast[floatX](20))
    adam = Adam(learning_rate=0.0001)
    step_rule = CompositeRule([adam, clipping])
    training_algorithm = GradientDescent(
        cost=cost, parameters=all_params,
        step_rule=step_rule)

    monitored_variables = [
        cost,
        aggregation.mean(training_algorithm.total_gradient_norm)] + monitorings

    blocks_model = Model(cost)
    params_dicts = blocks_model.get_parameter_dict()
    for name, param in params_dicts.iteritems():
        to_monitor = training_algorithm.gradients[param].norm(2)
        to_monitor.name = name + "_grad_norm"
        monitored_variables.append(to_monitor)
        to_monitor = param.norm(2)
        to_monitor.name = name + "_norm"
        monitored_variables.append(to_monitor)

    train_data_stream, valid_data_stream = get_mnist_video_streams(batch_size)

    train_monitoring = TrainingDataMonitoring(
        variables=monitored_variables,
        prefix="train",
        after_epoch=True)

    valid_monitoring = DataStreamMonitoring(
        variables=monitored_variables,
        data_stream=valid_data_stream,
        prefix="valid",
        after_epoch=True)

    main_loop = MainLoop(
        algorithm=training_algorithm,
        data_stream=train_data_stream,
        model=blocks_model,
        extensions=[
            train_monitoring,
            valid_monitoring,
            FinishAfter(after_n_epochs=num_epochs),
            SaveParams('valid_misclassificationrate_apply_error_rate',
                       blocks_model, save_path),
            SaveLog(save_path, after_epoch=True),
            ProgressBar(),
            Printing()])
    main_loop.run()
コード例 #5
0
hyperparameters["batched_window"] = True

cropper = LocallySoftRectangularCropper(
    patch_shape=patch_shape,
    hyperparameters=hyperparameters,
    kernel=Gaussian())

patch = cropper.apply(
    x.reshape((1, 1,) + image_shape),
    np.array([list(image_shape)]),
    location,
    scale)

f = theano.function([x, location, scale], patch, allow_input_downcast=True)

tds, vds = get_mnist_video_streams(1)
image = tds.get_epoch_iterator().next()[0][2, 0, :]

import ipdb; ipdb.set_trace()

location = [[60.0, 65.0]]
scale = [[1.0, 1.0]]

patch1 = f(image, location, scale)[0, 0]

plt.imshow(
    image.reshape(image_shape),
    cmap=plt.gray(),
    interpolation='nearest')
plt.savefig('img0.png')