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
bille_du
(jin du)
May 11, 2017, 9:10am
5
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
bille_du
(jin du)
May 12, 2017, 7:01am
7
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?
you can use:
model.layername.weight
or
model.layername.weight.data
in second one you can also change the weights
guen_gn
(nguyen gn)
October 11, 2021, 7:51pm
10
anirvirnn_mishra:
model.layername.weight
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
ptrblck
October 13, 2021, 6:43am
11
You can print the shape of the weight
parameter via print(model.layername.weight.shape)
.
1 Like
why this command gives me the initial value every time?