Exemplo n.º 1
0
    def test_test_suite(self):
        torch.manual_seed(1729)
        torchtest.setup()

        in_features=10
        out_features=1
        inputs = Variable(torch.randn(20, in_features))
        targets = Variable(torch.randn(20, out_features))
        batch = [inputs, targets]
        model = MLP(in_features=in_features, out_features=out_features, n_hidden=32, num_layers=5)

        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

        torchtest.test_suite(
            model=model,
            loss_fn=F.mse_loss,
            optim=torch.optim.Adam(model.parameters()),
            batch=batch,
            output_range=None,
            train_vars=None,
            non_train_vars=None,
            test_output_range=False,
            test_vars_change=True,
            test_nan_vals=True,
            test_inf_vals=True,
            test_gpu_available=False,
            device=device
            )
Exemplo n.º 2
0
    def test_test_suite(self):
        torch.manual_seed(1729)
        torchtest.setup()
        in_features=1
        out_features=1
        inputs = Variable(torch.randn(100, in_features, 20))
        targets = Variable(torch.randn(100, out_features, 20))
        batch = [inputs, targets]
        model = TCN(
            in_channels=in_features,
            channels=[10,out_features],
            kernel_size=2,
            )

        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

        torchtest.test_suite(
            model=model,
            loss_fn=F.mse_loss,
            optim=torch.optim.Adam(model.parameters()),
            batch=batch,
            output_range=None,
            train_vars=None,
            non_train_vars=None,
            test_output_range=False,
            test_vars_change=False,
            test_nan_vals=True,
            test_inf_vals=True,
            test_gpu_available=False,
            device=device
            )
Exemplo n.º 3
0
def test_nan_vals(model, loss_fn, batch, optim):
    """

    :param model:
    :param loss_fn:
    :param batch:
    :param optim:
    """
    test_suite(model, loss_fn, optim, batch, test_nan_vals=True)
Exemplo n.º 4
0
def test_forward(image_size, model_builder):
    x = torch.rand(4, 3, image_size, image_size)
    y = torch.randint(99, (4, ), dtype=torch.long)
    model = model_builder(image_size=image_size)
    optimiser = torch.optim.Adam(model.parameters())
    torchtest.test_suite(model=model,
                         loss_fn=F.cross_entropy,
                         optim=optimiser,
                         batch=[x, y],
                         test_inf_vals=True,
                         test_nan_vals=True,
                         test_vars_change=True)
def test_forward(image_size):
    x = torch.rand(4, 16, image_size, image_size)
    y = torch.randint(8, (4, ), dtype=torch.long)
    scl = ScatteringCompositionalLearner(image_size=image_size)
    optimiser = torch.optim.Adam(scl.parameters())
    torchtest.test_suite(model=scl,
                         loss_fn=F.cross_entropy,
                         optim=optimiser,
                         batch=[x, y],
                         test_inf_vals=True,
                         test_nan_vals=True,
                         test_vars_change=True)
 def test(self, device='cpu'):
     self.eval()
     input = torch.randn(10, self.input_dim, requires_grad=False)
     targets = torch.rand(10, self.output_dim, requires_grad=False)
     torchtest.test_suite(self,
                          F.mse_loss,
                          torch.optim.Adam(self.parameters()),
                          batch=[input, targets],
                          test_vars_change=True,
                          test_inf_vals=True,
                          test_nan_vals=True,
                          device=device)
     print('All tests passed')
Exemplo n.º 7
0
                    loss_fn=F.cross_entropy,
                    optim=torch.optim.Adam(params_to_train),
                    batch=batch,
                    params=[('bias', model.bias)])
# it does? good. let's move on
"""
[3] Output Range

"""
# we are keeping the bias fixed for a reason
optim = torch.optim.Adam(params_to_train)
loss_fn = F.cross_entropy

tt.test_suite(model,
              loss_fn,
              optim,
              batch,
              output_range=(-2, 2),
              test_output_range=True)

# seems to work
#  let's tweak the model to fail the test
model.bias = nn.Parameter(2 + torch.randn(2, ))
"""FAILURE
tt.test_suite(
    model,
    loss_fn, optim, batch, 
    output_range=(-1, 1),
    test_output_range=True
    )
"""
Exemplo n.º 8
0
        # testing model
        print('==> Testing model and train process...')

        torchtest.assert_vars_change(
            model=net,
            loss_fn=criterion,
            optim=optimizer,
            batch=test_ds,
            device=device)

        torchtest.test_suite(
            model=net,
            loss_fn=criterion,
            optim=optimizer,
            batch=test_ds,
            device=device,
            test_nan_vals=True,
            test_vars_change=True,
            # non_train_vars=None,
            test_inf_vals=True
        )

        overfit_test()

        print('==> All test are passed! Let is train whole network.')

    if args.train or args.resume:
        print('==> Let is TRAIN begin!')
        best_acc = 0  # best test accuracy
        for epoch in range(start_epoch, start_epoch + args.epochs):
            train(epoch, trainloader)