Skip to content

automatically assign attributes that are variable as parameters? #143

@glample

Description

@glample

Currently, when a variable is assigned to an attribute, the container will consider that this variable is a parameter of the model.

https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/module.py#L95

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions