当前位置: 首页>>代码示例>>Python>>正文


Python Model.set_param_values方法代码示例

本文整理汇总了Python中blocks.model.Model.set_param_values方法的典型用法代码示例。如果您正苦于以下问题:Python Model.set_param_values方法的具体用法?Python Model.set_param_values怎么用?Python Model.set_param_values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在blocks.model.Model的用法示例。


在下文中一共展示了Model.set_param_values方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_model

# 需要导入模块: from blocks.model import Model [as 别名]
# 或者: from blocks.model.Model import set_param_values [as 别名]
def test_model():
    x = tensor.matrix("x")
    mlp1 = MLP([Tanh(), Tanh()], [10, 20, 30], name="mlp1")
    mlp2 = MLP([Tanh()], [30, 40], name="mlp2")
    h1 = mlp1.apply(x)
    h2 = mlp2.apply(h1)

    model = Model(h2.sum())
    assert model.get_top_bricks() == [mlp1, mlp2]
    # The order of parameters returned is deterministic but
    # not sensible.
    assert list(model.get_params().items()) == [
        ("/mlp2/linear_0.b", mlp2.linear_transformations[0].b),
        ("/mlp1/linear_1.b", mlp1.linear_transformations[1].b),
        ("/mlp1/linear_0.b", mlp1.linear_transformations[0].b),
        ("/mlp1/linear_0.W", mlp1.linear_transformations[0].W),
        ("/mlp1/linear_1.W", mlp1.linear_transformations[1].W),
        ("/mlp2/linear_0.W", mlp2.linear_transformations[0].W),
    ]

    # Test getting and setting parameter values
    mlp3 = MLP([Tanh()], [10, 10])
    mlp3.allocate()
    model3 = Model(mlp3.apply(x))
    param_values = {
        "/mlp/linear_0.W": 2 * numpy.ones((10, 10), dtype=theano.config.floatX),
        "/mlp/linear_0.b": 3 * numpy.ones(10, dtype=theano.config.floatX),
    }
    model3.set_param_values(param_values)
    assert numpy.all(mlp3.linear_transformations[0].params[0].get_value() == 2)
    assert numpy.all(mlp3.linear_transformations[0].params[1].get_value() == 3)
    got_param_values = model3.get_param_values()
    assert len(got_param_values) == len(param_values)
    for name, value in param_values.items():
        assert_allclose(value, got_param_values[name])

    # Test name conflict handling
    mlp4 = MLP([Tanh()], [10, 10])

    def helper():
        Model(mlp4.apply(mlp3.apply(x)))

    assert_raises(ValueError, helper)
开发者ID:gilbertoIglesias,项目名称:blocks,代码行数:45,代码来源:test_model.py

示例2: train_model

# 需要导入模块: from blocks.model import Model [as 别名]
# 或者: from blocks.model.Model import set_param_values [as 别名]
def train_model(cost, train_stream, valid_stream, valid_freq, valid_rare,
                load_location=None, save_location=None):
    cost.name = 'nll'
    perplexity = 2 ** (cost / tensor.log(2))
    perplexity.name = 'ppl'

    # Define the model
    model = Model(cost)

    # Load the parameters from a dumped model
    if load_location is not None:
        logger.info('Loading parameters...')
        model.set_param_values(load_parameter_values(load_location))

    cg = ComputationGraph(cost)
    algorithm = GradientDescent(cost=cost, step_rule=Scale(learning_rate=0.01),
                                params=cg.parameters)
    main_loop = MainLoop(
        model=model,
        data_stream=train_stream,
        algorithm=algorithm,
        extensions=[
            DataStreamMonitoring([cost, perplexity], valid_stream,
                                 prefix='valid_all', every_n_batches=5000),
            # Overfitting of rare words occurs between 3000 and 4000 iterations
            DataStreamMonitoring([cost, perplexity], valid_rare,
                                 prefix='valid_rare', every_n_batches=500),
            DataStreamMonitoring([cost, perplexity], valid_freq,
                                 prefix='valid_frequent',
                                 every_n_batches=5000),
            Printing(every_n_batches=500)
        ]
    )
    main_loop.run()

    # Save the main loop
    if save_location is not None:
        logger.info('Saving the main loop...')
        dump_manager = MainLoopDumpManager(save_location)
        dump_manager.dump(main_loop)
        logger.info('Saved')
开发者ID:bartvm,项目名称:variational_nlp,代码行数:43,代码来源:feedforward.py

示例3: train_model

# 需要导入模块: from blocks.model import Model [as 别名]
# 或者: from blocks.model.Model import set_param_values [as 别名]
def train_model(cost, error_rate, train_stream,
                load_location=None, save_location=None):

    cost.name = "Cross_entropy"
    error_rate.name = 'Error_rate'

    # Define the model
    model = Model(cost)

    # Load the parameters from a dumped model
    if load_location is not None:
        logger.info('Loading parameters...')
        model.set_param_values(load_parameter_values(load_location))

    cg = ComputationGraph(cost)
    step_rule = Momentum(learning_rate=0.1, momentum=0.9)
    algorithm = GradientDescent(cost=cost, step_rule=step_rule,
                                params=cg.parameters)
    main_loop = MainLoop(
        model=model,
        data_stream=train_stream,
        algorithm=algorithm,
        extensions=[
            # DataStreamMonitoring([cost], test_stream, prefix='test',
            #                      after_epoch=False, every_n_epochs=10),
            DataStreamMonitoring([cost], train_stream, prefix='train',
                                 after_epoch=True),
            Printing(after_epoch=True)
        ]
    )
    main_loop.run()

    # Save the main loop
    if save_location is not None:
        logger.info('Saving the main loop...')
        dump_manager = MainLoopDumpManager(save_location)
        dump_manager.dump(main_loop)
        logger.info('Saved')
开发者ID:Alexis211,项目名称:transpose_features,代码行数:40,代码来源:features_reduction.py

示例4: main

# 需要导入模块: from blocks.model import Model [as 别名]
# 或者: from blocks.model.Model import set_param_values [as 别名]

#.........这里部分代码省略.........
    else:
        generator.weights_init = OrthogonalGlorot()
    generator.biases_init = Constant(0)

    # Build the cost computation graph [steps, batch_size, 3]
    x = T.tensor3('features', dtype=floatX)
    if debug:
        x.tag.test_value = np.ones((max_length,batch_size,3)).astype(floatX)
    x = x[:max_length,:,:]  # has to be after setting test_value
    cost = generator.cost(x)
    cost.name = "sequence_log_likelihood"

    # Give an idea of what's going on
    model = Model(cost)
    params = model.get_params()
    logger.info("Parameters:\n" +
                pprint.pformat(
                    [(key, value.get_value().shape) for key, value
                     in params.items()],
                    width=120))
    model_size = 0
    for v in params.itervalues():
        s = v.get_value().shape
        model_size += s[0] * (s[1] if len(s) > 1 else 1)
    logger.info("Total number of parameters %d"%model_size)

    #------------------------------------------------------------
    extensions = []
    if old_model_name == 'continue':
        extensions.append(LoadFromDump(jobname))
    elif old_model_name:
        # or you can just load the weights without state using:
        old_params = LoadFromDump(old_model_name).manager.load_parameters()
        model.set_param_values(old_params)
    else:
        # Initialize parameters
        for brick in model.get_top_bricks():
            brick.initialize()

    if sample:
        assert old_model_name and old_model_name != 'continue'
        Sample(generator, steps=max_length, path=old_model_name).do(None)
        exit(0)

    #------------------------------------------------------------
    # Define the training algorithm.
    cg = ComputationGraph(cost)
    if dropout > 0.:
        from blocks.roles import INPUT, OUTPUT
        dropout_target = VariableFilter(roles=[OUTPUT],
                                        bricks=transitions,
                                        name_regex='states')(cg.variables)
        print('# dropout %d' % len(dropout_target))
        cg = apply_dropout(cg, dropout_target, dropout)
        opt_cost = cg.outputs[0]
    else:
        opt_cost = cost

    if step_method == 'adam':
        step_rule = Adam(learning_rate)
    elif step_method == 'rmsprop':
        step_rule = RMSProp(learning_rate, decay_rate=0.95)
    elif step_method == 'adagrad':
        step_rule = AdaGrad(learning_rate)
    elif step_method == 'adadelta':
        step_rule = AdaDelta()
开发者ID:dribnet,项目名称:sketch,代码行数:70,代码来源:sketch.py

示例5: VAModel

# 需要导入模块: from blocks.model import Model [as 别名]
# 或者: from blocks.model.Model import set_param_values [as 别名]
from fuel.schemes import ShuffledScheme
import theano
import logging
import numpy as np


logging.basicConfig()

m = VAModel()

# load parameters
model = Model(m.variational_cost)
print "loading params"
params = load_parameter_values(sys.argv[1])
model.set_param_values(params)

test_dataset = MNIST("test", sources=["features"])
test_scheme = ShuffledScheme(test_dataset.num_examples, 128)
test_stream = DataStream(test_dataset, iteration_scheme=test_scheme)

_func_sample = theano.function([m.Z], m.sampled)
# _func_noisy = theano.function([m.X], m.noisy)
# _func_produced = theano.function([m.X], m.produced)

# batch = test_stream.get_epoch_iterator().next()[0]
# out_noise = _func_noisy(batch)
# out_produced = _func_produced(batch)
import cv2

# out_sampled = _func_sample(np.random.normal(size=(20, m.n_hidden)).astype(theano.config.floatX))
开发者ID:caomw,项目名称:MLFun,代码行数:32,代码来源:animate_sample.py

示例6: main

# 需要导入模块: from blocks.model import Model [as 别名]
# 或者: from blocks.model.Model import set_param_values [as 别名]

#.........这里部分代码省略.........
            algorithm.total_step_norm, algorithm.total_gradient_norm]
        for name, param in params.items():
            observables.append(named_copy(
                param.norm(2), name + "_norm"))
            observables.append(named_copy(
                algorithm.gradients[param].norm(2), name + "_grad_norm"))

        # Construct the main loop and start training!
        average_monitoring = TrainingDataMonitoring(
            observables, prefix="average", every_n_batches=10)
        main_loop = MainLoop(
            model=model,
            data_stream=data_stream,
            algorithm=algorithm,
            extensions=[
                Timing(),
                TrainingDataMonitoring(observables, after_batch=True),
                average_monitoring,
                FinishAfter(after_n_batches=num_batches)
                # This shows a way to handle NaN emerging during
                # training: simply finish it.
                .add_condition("after_batch", _is_nan),
                # Saving the model and the log separately is convenient,
                # because loading the whole pickle takes quite some time.
                Checkpoint(save_path, every_n_batches=500,
                           save_separately=["model", "log"]),
                Printing(every_n_batches=1)])
        main_loop.run()
    elif mode == "sample" or mode == "beam_search":
        chars = tensor.lmatrix("input")
        generated = reverser.generate(chars)
        model = Model(generated)
        logger.info("Loading the model..")
        model.set_param_values(load_parameter_values(save_path))

        def generate(input_):
            """Generate output sequences for an input sequence.

            Incapsulates most of the difference between sampling and beam
            search.

            Returns
            -------
            outputs : list of lists
                Trimmed output sequences.
            costs : list
                The negative log-likelihood of generating the respective
                sequences.

            """
            if mode == "beam_search":
                samples, = VariableFilter(
                    bricks=[reverser.generator], name="outputs")(
                        ComputationGraph(generated[1]))
                # NOTE: this will recompile beam search functions
                # every time user presses Enter. Do not create
                # a new `BeamSearch` object every time if
                # speed is important for you.
                beam_search = BeamSearch(input_.shape[1], samples)
                outputs, costs = beam_search.search(
                    {chars: input_}, char2code['</S>'],
                    3 * input_.shape[0])
            else:
                _1, outputs, _2, _3, costs = (
                    model.get_theano_function()(input_))
                outputs = list(outputs.T)
开发者ID:Thrandis,项目名称:blocks-examples,代码行数:70,代码来源:__init__.py

示例7: main

# 需要导入模块: from blocks.model import Model [as 别名]
# 或者: from blocks.model.Model import set_param_values [as 别名]
def main(save, load, sample, path, **kwargs):
    input_dim = 784
    hidden_dim = 2
    batch_size = 100

    features = tensor.matrix('features')

    vae = VariationalAutoEncoder(input_dim, hidden_dim,
                                 weights_init=IsotropicGaussian(0.01),
                                 biases_init=Constant(0.))
    vae.initialize()

    mu, logsigma, x_hat = vae.apply(features)
    cost = vae.cost(features)
    cost.name = 'cost'
    regularization_cost = vae.regularization_cost(mu, logsigma).mean()
    regularization_cost.name = 'regularization_cost'
    reconstruction_cost = vae.reconstruction_cost(features, x_hat).mean()
    reconstruction_cost.name = 'reconstruction_cost'

    cg = ComputationGraph([cost, reconstruction_cost, regularization_cost])
    model = Model(cost)

    algorithm = GradientDescent(step_rule=RMSProp(1e-4), params=cg.parameters,
                                cost=cost)

    extensions = []
    if load:
        extensions.append(LoadFromDump(path))
    if save:
        extensions.append(Dump(path, after_epoch=True))
    extensions.append(FinishAfter(after_n_epochs=6001))

    train_dataset = MNIST('train', binary=False, sources=('features',))
    train_stream = DataStream(train_dataset,
                              iteration_scheme=ShuffledScheme(
                                  examples=train_dataset.num_examples,
                                  batch_size=batch_size))
    train_monitor = TrainingDataMonitoring(
        [cost, regularization_cost, reconstruction_cost],
        prefix='train', after_epoch=True)

    test_dataset = MNIST('test', binary=True, sources=('features',))
    test_stream = DataStream(test_dataset,
                             iteration_scheme=ShuffledScheme(
                                 examples=test_dataset.num_examples,
                                 batch_size=batch_size))
    test_monitor = DataStreamMonitoring([cost], test_stream, prefix='test')
    extensions.extend([train_monitor, test_monitor])
    extensions.extend([Timing(), Printing()])
    main_loop = MainLoop(model=model, algorithm=algorithm,
                         data_stream=train_stream,
                         extensions=extensions)
    if not sample:
        main_loop.run()
    else:
        parameters = load_parameter_values(path + '/params.npz')
        model.set_param_values(parameters)

        num_samples = 10
        samples = vae.sample(num_samples)
        samples = function([], samples)()
        z = tensor.matrix('z')
        decode_z = function([z], vae.decoder.apply(z))

        from matplotlib import pyplot as plt

        sample = numpy.zeros((28, 0))

        size = 40
        z_val = numpy.zeros((size ** 2, 2))
        for i in xrange(size):
            for j in xrange(size):
                z_val[i * size + j, :] = numpy.array(
                    [i / float(0.3 * size) - .5 / .3,
                     j / float(0.3 * size) - .5 / .3])
        samples = decode_z(z_val)
        samples = samples.reshape((size, size, 28, 28))
        samples = numpy.concatenate(samples, axis=1)
        samples = numpy.concatenate(samples, axis=1)
        plt.imshow(samples, cmap=plt.get_cmap('Greys'))
        plt.show()
        f = function([features], x_hat)
        for data in train_stream.get_epoch_iterator():
            data_hat = f(data[0])
            for image, image_hat in zip(data[0], data_hat):
                im = numpy.concatenate([image_hat.reshape((28, 28)),
                                        image.reshape((28, 28))])
                plt.imshow(im, cmap=plt.get_cmap('Greys'))
                plt.show()
开发者ID:dmitriy-serdyuk,项目名称:variational_rnn,代码行数:92,代码来源:vae.py


注:本文中的blocks.model.Model.set_param_values方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。