Exemplo n.º 1
0
def test_sequential_4():
    with pytest.raises(RuntimeError,
                       match='illegal parameter type of <h_neurons>'):
        SequentialLinear(10, 5, h_neurons=('NaN', ))

    with pytest.raises(
            RuntimeError,
            match='number of parameter not consistent with number of layers'):
        SequentialLinear(10, 5, h_neurons=(4, ), h_dropouts=(1, 5))

    m = SequentialLinear(10, 1, h_neurons=(3, 4))
    tmp = (1, 2)
    assert id(m._check_input(tmp)) == id(tmp)
Exemplo n.º 2
0
def test_persist_save_checkpoints(data):
    class _Trainer(BaseRunner):
        def __init__(self):
            super().__init__()
            self.model = SequentialLinear(50, 2)

        def predict(self, x_, y_):  # noqa
            return x_, y_

    cp_1 = Trainer.checkpoint_tuple(
        id='cp_1',
        iterations=111,
        model_state=SequentialLinear(50, 2).state_dict(),
    )
    cp_2 = Trainer.checkpoint_tuple(
        id='cp_2',
        iterations=111,
        model_state=SequentialLinear(50, 2).state_dict(),
    )

    # save checkpoint
    p = Persist('test_model_1', increment=False, only_best_states=False)
    p.before_proc(trainer=_Trainer())
    p.on_checkpoint(cp_1)
    p.on_checkpoint(cp_2)
    assert (Path('.').resolve() / 'test_model_1' / 'checkpoints' /
            'cp_1.pth.s').exists()
    assert (Path('.').resolve() / 'test_model_1' / 'checkpoints' /
            'cp_2.pth.s').exists()

    # reduced save checkpoint
    p = Persist('test_model_2', increment=False, only_best_states=True)
    p.before_proc(trainer=_Trainer())
    p.on_checkpoint(cp_1)
    p.on_checkpoint(cp_2)
    assert (Path('.').resolve() / 'test_model_2' / 'checkpoints' /
            'cp.pth.s').exists()
    assert not (Path('.').resolve() / 'test_model_2' / 'checkpoints' /
                'cp_1.pth.s').exists()
    assert not (Path('.').resolve() / 'test_model_2' / 'checkpoints' /
                'cp_2.pth.s').exists()

    # no checkpoint will be saved
    p = Persist('test_model_3', increment=False, only_best_states=True)
    p.before_proc(trainer=_Trainer())
    p.on_checkpoint(cp_2)
    assert not (Path('.').resolve() / 'test_model_3' / 'checkpoints' /
                'cp.pth.s').exists()
    assert not (Path('.').resolve() / 'test_model_3' / 'checkpoints' /
                'cp_2.pth.s').exists()
Exemplo n.º 3
0
def test_sequential_1(data):
    m = SequentialLinear(10, 5)
    assert isinstance(m, torch.nn.Module)
    assert m.output.in_features == 10
    assert m.output.out_features == 5

    x = m(data[0])
    assert x.size() == (10, 5)
Exemplo n.º 4
0
def test_sequential_3():
    m = SequentialLinear(10, 1, bias=False, h_neurons=(0.7, 0.5))
    assert m.layer_0.linear.in_features == 10
    assert m.layer_0.linear.out_features == 7
    assert m.layer_0.linear.bias is None
    assert m.layer_1.linear.in_features == 7
    assert m.layer_1.linear.out_features == 5
    assert m.layer_1.linear.bias is not None
    assert m.output.in_features == 5
    assert m.output.out_features == 1
    assert m.output.bias is not None
Exemplo n.º 5
0
def test_sequential_2(data):
    m = SequentialLinear(10, 1, bias=False, h_neurons=(7, 4))
    assert isinstance(m.layer_0, LinearLayer)
    assert isinstance(m.layer_1, LinearLayer)
    assert m.layer_0.linear.in_features == 10
    assert m.layer_0.linear.out_features == 7
    assert m.layer_0.linear.bias is None
    assert m.layer_1.linear.in_features == 7
    assert m.layer_1.linear.out_features == 4
    assert m.layer_1.linear.bias is not None
    assert m.output.in_features == 4
    assert m.output.out_features == 1
    assert m.output.bias is not None

    x = m(data[0])
    assert x.size() == (10, 1)
Exemplo n.º 6
0
def data():
    # prepare path
    model1 = nn.Sequential(
        nn.Sequential(OrderedDict(layer=nn.Linear(10, 7), act_func=nn.ReLU())),
        nn.Sequential(OrderedDict(layer=nn.Linear(7, 4), act_func=nn.ReLU())),
        nn.Sequential(OrderedDict(layer=nn.Linear(4, 1))),
    )
    model2 = SequentialLinear(10, 1, h_neurons=(7, 4))
    descriptor1 = np.random.rand(10, 10)
    descriptor2 = np.random.rand(20, 10)
    index = ['id_' + str(i) for i in range(10)]
    df = pd.DataFrame(descriptor1, index=index)

    # ignore numpy warning
    import warnings
    print('ignore NumPy RuntimeWarning\n')
    warnings.filterwarnings("ignore", message="numpy.dtype size changed")
    warnings.filterwarnings("ignore", message="numpy.ndarray size changed")

    yield model1, descriptor1, df, index, descriptor2, model2

    print('test over')
Exemplo n.º 7
0
 def __init__(self):
     super().__init__()
     self.model = SequentialLinear(50, 2)