你可以如何在PyTorch中使用优化器来更新模型的参数?

huangapple go评论83阅读模式
英文:

How can I update parameters of my model with optimiser in pytorch?

问题

感谢您的关注。我只会提供代码的翻译,以下是您要翻译的内容:

  1. for k in range(100):
  2. c_train = cluster.forward(context)
  3. loss_Cluster = cluster_L(train_num, args['lambda_c'], scalar_f, c_train)
  4. optimiser_c.zero_grad()
  5. loss_Cluster.backward()
  6. grad_norm = th.nn.utils.clip_grad_norm_(c_param, 10)
  7. optimiser_c.step()

一些配置和cluster模型:

  1. args = {'c_emb': 16, 'use_cuda': False, 'ally_type': [1, 0, 0, 0, 0, 0, 0, 0, 0],
  2. 'enemy_type': [1, 0, 0, 0, 0, 0, 0, 0, 0], 'n_classes': 5, 'lambda_c': 2}
  3. cluster = CLUSTER_L(args)
  4. context = th.tensor(args['ally_type']+args['enemy_type'], dtype=th.float)
  5. train_num = args['n_classes']*args['lambda_c']
  6. scalar_f = (args['n_classes']*(train_num-1))/(args['lambda_c']*args['lambda_c'])
  7. c_param = list(cluster.parameters())
  8. optimiser_c = RMSprop(c_param, lr=0.1)
  1. import torch.nn as nn
  2. import torch.nn.functional as F
  3. import torch
  4. import numpy as np
  5. class CLUSTER_L(nn.Module):
  6. def __init__(self, args):
  7. super(CLUSTER_L, self).__init__()
  8. self.args = args
  9. self.embed_dim = self.args['c_emb']
  10. if self.args['use_cuda']:
  11. torch.cuda.set_device(torch.device('cuda:0'))
  12. self.input_size = len(self.args['ally_type']) + len(self.args['enemy_type'])
  13. self.n = self.args['n_classes']
  14. self.lbd = self.args['lambda_c']
  15. self.cl = nn.Sequential(nn.Linear(self.input_size, self.embed_dim),
  16. nn.ReLU(),
  17. nn.Linear(self.embed_dim, self.embed_dim),
  18. nn.ReLU(),
  19. nn.Linear(self.embed_dim, self.n*self.lbd*self.input_size)
  20. )
  21. self.af = nn.ReLU()
  22. def forward(self, inputs):
  23. d_outputs = self.cl(inputs).view(self.n*self.lbd, self.input_size)
  24. outputs = ((self.af(d_outputs)+0.1)*10).round()
  25. outputs2 = outputs*inputs
  26. return outputs2

我尝试将损失函数更改为nn.functional中的一些标准函数,例如cross_entropy,但问题仍然存在。能否有人告诉我如何更新模型(cluster)的参数?我感激您的帮助。

英文:

Thanks for your attention. I just can't update the parameters of my model. It stays the same although I backward my loss and step the optimiser.
The codes are below.

  1. for k in range(100):
  2. c_train = cluster.forward(context)
  3. # pdb.set_trace() # debug here
  4. loss_Cluster = cluster_L(train_num, args['lambda_c'], scalar_f, c_train)
  5. #loss_Cluster = F.cross_entropy(c_train, test) # just for testing, proving that loss_Cluster is right
  6. optimiser_c.zero_grad()
  7. loss_Cluster.backward()
  8. grad_norm = th.nn.utils.clip_grad_norm_(c_param, 10)
  9. # for para in cluster.parameters():
  10. # print(para)
  11. optimiser_c.step()

some configs and the cluster model:

  1. args = {'c_emb':16, 'use_cuda':False, 'ally_type':[1,0,0,0,0,0,0,0,0],
  2. 'enemy_type':[1,0,0,0,0,0,0,0,0], 'n_classes':5, 'lambda_c':2}
  3. cluster = CLUSTER_L(args)
  4. context = th.tensor(args['ally_type']+args['enemy_type'], dtype=th.float)
  5. train_num = args['n_classes']*args['lambda_c']
  6. scalar_f = (args['n_classes']*(train_num-1))/(args['lambda_c']*args['lambda_c'])
  7. c_param = list(cluster.parameters())
  8. optimiser_c = RMSprop(c_param, lr = 0.1)
  1. import torch.nn as nn
  2. import torch.nn.functional as F
  3. import torch
  4. import numpy as np
  5. class CLUSTER_L(nn.Module):
  6. def __init__(self, args):
  7. super(CLUSTER_L, self).__init__()
  8. self.args = args
  9. self.embed_dim = self.args['c_emb']
  10. if self.args['use_cuda']:
  11. torch.cuda.set_device(torch.device('cuda:0'))
  12. self.input_size = len(self.args['ally_type'])+len(self.args['enemy_type'])
  13. self.n = self.args['n_classes']
  14. self.lbd = self.args['lambda_c']
  15. # the input of cl should be 1*2T , output should be 1*(2T*self.n*self.lbd)
  16. self.cl = nn.Sequential(nn.Linear(self.input_size, self.embed_dim),
  17. nn.ReLU(),
  18. nn.Linear(self.embed_dim, self.embed_dim),
  19. nn.ReLU(),
  20. nn.Linear(self.embed_dim, self.n*self.lbd*self.input_size)
  21. )
  22. self.af = nn.ReLU()
  23. # inputs should be 1*2T size
  24. def forward(self, inputs):
  25. d_outputs = self.cl(inputs).view(self.n*self.lbd, self.input_size)
  26. outputs = ((self.af(d_outputs)+0.1)*10).round() # make sure the outputs are positive and >=1
  27. print(outputs)
  28. outputs2 = outputs*inputs # mask and output
  29. return outputs2

I've tried changing the loss function to some standard functions in nn.functional such as cross_entropy but the issue remains the same.

Could anyone tell me how can I update the parameters of the model(cluster)? I appreciate your help.

答案1

得分: 0

只移除 round() 并修改视图以保留批量维度。

英文:

Just remove the round() and modify the view to keep the batch dim.

huangapple
  • 本文由 发表于 2023年4月4日 11:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定