Exemple #1
0
def train_new(path_model):
    gpu_id = (1 if torch.cuda.is_available() else -1)
    x, y = get_data(gpu_id)
    model = Model(
        nn_class='test_fnet_model.DummyModel',
        nn_kwargs={'some_param': SOME_PARAM_TEST_VAL},
    )
    model.to_gpu(gpu_id)
    for idx in range(4):
        _ = model.train_on_batch(x, y)
    model.save(path_model)
Exemple #2
0
def train_new(path_model):
    gpu_id = 1 if torch.cuda.is_available() else -1
    x, y = get_data(gpu_id)
    model = Model(
        nn_class="fnet.nn_modules.dummy.DummyModel",
        nn_kwargs={"some_param": SOME_PARAM_TEST_VAL},
    )
    model.to_gpu(gpu_id)
    for idx in range(4):
        _ = model.train_on_batch(x, y)
    model.save(path_model)
Exemple #3
0
def test_train_on_batch():
    model = Model(nn_class='tests.data.nn_test.Net', lr=0.01)
    shape_item = (1, 2, 4, 8)
    batch_size = 9
    shape_batch = (batch_size, ) + shape_item
    x_batch = torch.rand(shape_batch)
    y_batch = x_batch * 0.666 + 0.42
    cost_prev = float('inf')
    for _ in range(8):
        cost = model.train_on_batch(x_batch, y_batch)
        assert cost < cost_prev
        cost_prev = cost

    # Test target weight maps
    model = Model(nn_class='tests.data.nn_test.Net', lr=0.)  # disable learning
    cost_norm = model.train_on_batch(x_batch, y_batch)
    # Test uniform weight map
    weight_map_batch = ((torch.ones(shape_item) /
                         np.prod(shape_item)).expand(shape_batch))
    cost_weighted = model.train_on_batch(x_batch, y_batch, weight_map_batch)
    npt.assert_approx_equal(cost_weighted, cost_norm)
    # Test all-zero weight map
    cost_weighted = model.train_on_batch(x_batch, y_batch,
                                         torch.zeros(x_batch.size()))
    npt.assert_approx_equal(cost_weighted, 0.)
    # Random weights with first and last examples having zero weight
    weight_map_batch = torch.rand(shape_batch)
    weight_map_batch[[0, -1]] = 0.
    cost_weighted = model.train_on_batch(x_batch, y_batch, weight_map_batch)
    cost_exp = (
        model.train_on_batch(x_batch[1:-1], y_batch[1:-1],
                             weight_map_batch[1:-1]) * (batch_size - 2) /
        batch_size  # account for change in batch size
    )
    npt.assert_approx_equal(cost_weighted, cost_exp)