TheMody / No-learning-rates-needed-Introducing-SALSA-Stable-Armijo-Line-Search-Adaptation

SaLSa Optimizer implementation (No learning rates needed)
MIT License
28 stars 0 forks source link

None error #1

Closed fedeloper closed 2 months ago

fedeloper commented 2 months ago

Hi, I'm trying to use your package: I got this error

  File "/home/fred/.local/lib/python3.10/site-packages/salsa/SaLSA.py", line 112, in step
    pp_norm = self.get_pp_norm(grad_current)
  File "/home/fred/.local/lib/python3.10/site-packages/salsa/SaLSA.py", line 142, in get_pp_norm
    layer_norm = ((g_i**2) * pv_i).sum()
TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'

here is my training loop:

        if type(optimizer) is SaLSA and training:
            def closure(backwards = False):
                y_pred = model(inputs)
                if type(y_pred) is list:
                    y_pred = y_pred[0]
                loss = criterion(y_pred, target)
                if backwards: 
                    loss.backward()
                return loss
            optimizer.zero_grad()
            loss = optimizer.step(closure = closure)
            prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
            losses.update(loss.data.item(), inputs.size(0))
            top1.update(prec1.item(), inputs.size(0))
            top5.update(prec5.item(), inputs.size(0))
        else:
            output = model(inputs)
            loss = criterion(output, target)

            if type(output) is list:
                output = output[0]

            #* measure accuracy and record loss
            prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
            losses.update(loss.data.item(), inputs.size(0))
            top1.update(prec1.item(), inputs.size(0))
            top5.update(prec5.item(), inputs.size(0))

            if training:
                #* back-propagation
                    optimizer.zero_grad()
                    loss.backward()
                    optimizer.step()

        #* measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

and that's how defined the optimizer optimizer = SaLSA(model.parameters())

Everything works when I'm not using SaLSa.

TheMody commented 2 months ago

Thanks for the feedback!

My working guess is that your model has parameters which are without a gradient. I added a check in the Optimizer for this. Please try the updated version which i just now pushed.