How to output weight

I need to know all the weight values,How can I output the weight of the training process?

criterion = nn.CrossEntropyLoss().cuda()

optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)

if args.evaluate:
validate(val_loader, model, criterion)

for epoch in range(args.start_epoch, args.epochs):
adjust_learning_rate(optimizer, epoch)

    # train for one epoch
train(train_loader, model, criterion, optimizer, epoch)
    # evaluate on validation set
prec1 = validate(val_loader, model, criterion)
    # remember best prec@1 and save checkpoint
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
save_checkpoint({
    'epoch': epoch + 1,
    'state_dict': model.state_dict(),
    'best_prec1': best_prec1,
}, is_best, filename='/home/zl/models/checkpoint_{}.pth.tar'.format(epoch))
1 Like
for param in model.parameters():
  print(param.data)
16 Likes

Thank you very much!
If I want to output the output value(x) after the first layer convolution,not parameters ,what should I do?

You can solve it with the method register_forward_hook

Very grateful, but the function of the official website is very simple to introduce, there is no example, I tried it or did not use it。Can you give me an example?

look at Forward and Backward Function Hooks

you can use:
model.layername.weight
or
model.layername.weight.data

in second one you can also change the weights :slight_smile:

Sorry, I wanted to revive and chip in:
How to print dimensions of particular layer, that is length of weight array in each axis?
torch.nn.parameter.Parameter

You can print the shape of the weight parameter via print(model.layername.weight.shape).

1 Like