-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Closed
Description
Currently, when a variable is assigned to an attribute, the container will consider that this variable is a parameter of the model.
This example will not work, because self.h is considered as a parameter, although it is an intermediate state:
class RNN(nn.Container):
def __init__(self, input_dim, hidden_dim):
super(RNN, self).__init__(
i2h=nn.Linear(input_dim, hidden_dim),
h2h=nn.Linear(hidden_dim, hidden_dim)
)
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.reset()
def forward(self, x):
last_h = self.h
if self.step == 0:
last_h = last_h.view(1, self.hidden_dim).expand(x.size(0), self.hidden_dim)
self.h = nn.Sigmoid()(self.i2h(x) + self.h2h(last_h))
self.step += 1
return self.h
def reset(self):
self.step = 0
self.h = Variable(torch.zeros(self.hidden_dim), requires_grad=False)
a = RNN(3, 4)
a.forward(Variable(torch.FloatTensor(2, 3).zero_()))
Maybe it would be safer to let the user explicitely say what is a parameter or not, with some add_parameter / remove_parameter methods?
Metadata
Metadata
Assignees
Labels
No labels