【发布时间】:2020-04-20 23:23:50
【问题描述】:
我有一个 PyTorch 以前版本的代码,我收到 2 条关于它的第 3 行的警告:
import torch.nn.functional as F
def select_action(self, state):
probabilities = F.softmax(self.model(Variable(state, volatile = True))*100) # T=100
action = probs.multinomial(num_samples=1)
return action.data[0,0]
UserWarning: volatile 已被移除,现在无效。请改用
with torch.no_grad():。用户警告:softmax 的隐式维度选择已被弃用。更改调用以包含 dim=X > 作为参数。
我发现:
当您确定时,建议将 Volatile 用于纯推理模式 你甚至不会调用 .backward()。它比任何东西都更有效 其他 autograd 设置 - 它将使用绝对最小量 内存来评估模型。 volatile 也决定了 requires_grad 为 False。
我应该删除它是对的吗? 因为我想得到概率,所以我应该使用 dim=1 吗? 我的代码的第三行应该是这样的:
probabilities = F.softmax(self.model(Variable(state), dim=1)*100) # T=100
状态在此处创建:
def update(self, reward, new_signal):
new_state = torch.Tensor(new_signal).float().unsqueeze(0)
self.memory.push((self.last_state, new_state, torch.LongTensor([int(self.last_action)]), torch.Tensor([self.last_reward])))
action = self.select_action(new_state)
if len(self.memory.memory) > 100:
batch_state, batch_next_state, batch_action, batch_reward = self.memory.sample(100)
self.learn(batch_state, batch_next_state, batch_reward, batch_action)
self.last_action = action
self.last_state = new_state
self.last_reward = reward
self.reward_window.append(reward)
if len(self.reward_window) > 1000:
del self.reward_window[0]
return action
【问题讨论】:
标签: python-3.x pytorch