コード例 #1
0
def test_training_data_monitoring_updates_algorithm():
    features = [
        numpy.array(f, dtype=theano.config.floatX)
        for f in [[1, 2], [3, 5], [5, 8]]
    ]
    targets = numpy.array([f.sum() for f in features])
    dataset = IterableDataset(dict(features=features, targets=targets))

    x = tensor.vector('features')
    y = tensor.scalar('targets')
    m = x.mean().copy(name='features_mean')
    t = y.sum().copy(name='targets_sum')

    main_loop = MainLoop(
        model=None,
        data_stream=dataset.get_example_stream(),
        algorithm=UpdatesAlgorithm(),
        extensions=[
            TrainingDataMonitoring([m, t], prefix="train1", after_batch=True)
        ],
    )
    main_loop.extensions[0].main_loop = main_loop
    assert len(main_loop.algorithm.updates) == 0
    main_loop.extensions[0].do('before_training')
    assert len(main_loop.algorithm.updates) > 0
コード例 #2
0
def test_updates_algorithm():
    n = shared_floatx(1)
    algorithm = UpdatesAlgorithm(updates=[(n, n + 1)])
    algorithm.initialize()
    algorithm.process_batch({})
    assert_allclose(n.get_value(), 2)
    algorithm.process_batch({})
    assert_allclose(n.get_value(), 3)
コード例 #3
0
def test_updates_algorithm_data():
    n = shared_floatx(1)
    m = tensor.scalar('m')
    algorithm = UpdatesAlgorithm(updates=[(n, m + 1)])
    algorithm.initialize()
    algorithm.process_batch({'m': 5})
    assert_allclose(n.get_value(), 6)
    algorithm.process_batch({'m': 3})
    assert_allclose(n.get_value(), 4)
コード例 #4
0
def test_updates_algorithm_add_updates():
    n = shared_floatx(1)
    m = shared_floatx(0)
    algorithm = UpdatesAlgorithm(updates=[(n, n + 1)])
    algorithm.add_updates([(m, n % 2)])
    assert len(algorithm.updates) == 2
    algorithm.initialize()
    algorithm.process_batch({})
    assert_allclose(n.get_value(), 2)
    assert_allclose(m.get_value(), 1)
    algorithm.process_batch({})
    assert_allclose(n.get_value(), 3)
    assert_allclose(m.get_value(), 0)