Trying to load model and switch to eval

In my script the model successfully trains and I save it at the end. When I try to load and evaluate it this is what happens:

Traceback (most recent call last):
  File ".\GRU.py", line 139, in <module>
    gru_outputs, targets, gru_sMAPE = evaluate(gmodel, X_test, Y_test, label_scalars)
  File ".\GRU.py", line 115, in evaluate
    model.eval()
AttributeError: 'NoneType' object has no attribute 'eval'

This is the last portion of the code:

def evaluate(model, X_test, Y_test, label_scalars):
    model.eval()
    outputs = []
    targets = []
    start_time = time.clock()
    for i in X_test.keys():
        inp = torch.from_numpy(np.array(X_test[i]))
        labs = torch.from_numpy(np.array(Y_test[i]))
        h = model.init_hidden(inp.shape[0])
        out, h = model(inp.to(device).float(), h)
        outputs.append(label_scalars[i].inverse_transform(out.cpu().detach().numpy()).reshape(-1))
        targets.append(label_scalars[i].inverse_transform(labs.numpy()).reshape(-1))
    print("Evaluation Time: {}".format(str(time.clock()-start_time)))
    sMAPE = 0
    for i in range(len(outputs)):
        sMAPE += np.mean(abs(outputs[i]-targets[i])/(targets[i]+outputs[i])/2)/len(outputs)
    print("sMAPE: {}%".format(sMAPE*100))
    return outputs, targets, sMAPE

lr = 0.002

#Gru_model = train(train_loader, lr)

gmodel = torch.load('./grunet.pkl')

gru_outputs, targets, gru_sMAPE = evaluate(gmodel, X_test, Y_test, label_scalars)

What does print(gmodel) return after the torch.load call?
Based on the error it seems it’s None, which indicates the loading fails somehow.

1 Like