Repository: pytorch/pytorch

Issue:
Error in forward() docs for RNNCell
I think there is a mistake in the docs about the `RNNCell`. It says that the hidden argument in `forward` function may be omitted and will then be defaulted to `zeros` but this raises an error.
The same issue will happen for `LSTMCell` and `GRUCell`


```
rnn = nn.RNNCell(10, 20)
input = torch.randn(3,10)
h = rnn(input)
```

leads to 

`TypeError: forward() missing 1 required positional argument: 'hx'`

Error in forward() docs for RNNCell
I think there is a mistake in the docs about the `RNNCell`. It says that the hidden argument in `forward` function may be omitted and will then be defaulted to `zeros` but this raises an error.
The same issue will happen for `LSTMCell` and `GRUCell`


```
rnn = nn.RNNCell(10, 20)
input = torch.randn(3,10)
h = rnn(input)
```

leads to 

`TypeError: forward() missing 1 required positional argument: 'hx'`



Discussion:
Thanks for pointing that out, @valsworthen. We'll either fix the docs or add that functionality. It seems useful for the hidden state to default to 0 in my opinion so I'm leaning towards a solution involving adding the functionality, especially if it used to exist.
I agree with that second option, thanks for your answer! 
Thanks for pointing that out, @valsworthen. We'll either fix the docs or add that functionality. It seems useful for the hidden state to default to 0 in my opinion so I'm leaning towards a solution involving adding the functionality, especially if it used to exist.
I agree with that second option, thanks for your answer! 

Resolving patch:
diff --git a/torch/nn/modules/rnn.py b/torch/nn/modules/rnn.py
--- a/torch/nn/modules/rnn.py
+++ b/torch/nn/modules/rnn.py
@@ -603,8 +603,10 @@ def reset_parameters(self):
         for weight in self.parameters():
             weight.data.uniform_(-stdv, stdv)
 
-    def forward(self, input, hx):
+    def forward(self, input, hx=None):
         self.check_forward_input(input)
+        if hx is None:
+            hx = input.new_zeros(input.size(0), self.hidden_size, requires_grad=False)
         self.check_forward_hidden(input, hx)
         if self.nonlinearity == "tanh":
             func = self._backend.RNNTanhCell
@@ -698,8 +700,11 @@ def reset_parameters(self):
         for weight in self.parameters():
             weight.data.uniform_(-stdv, stdv)
 
-    def forward(self, input, hx):
+    def forward(self, input, hx=None):
         self.check_forward_input(input)
+        if hx is None:
+            hx = input.new_zeros(input.size(0), self.hidden_size, requires_grad=False)
+            hx = (hx, hx)
         self.check_forward_hidden(input, hx[0], '[0]')
         self.check_forward_hidden(input, hx[1], '[1]')
         return self._backend.LSTMCell(
@@ -778,8 +783,10 @@ def reset_parameters(self):
         for weight in self.parameters():
             weight.data.uniform_(-stdv, stdv)
 
-    def forward(self, input, hx):
+    def forward(self, input, hx=None):
         self.check_forward_input(input)
+        if hx is None:
+            hx = input.new_zeros(input.size(0), self.hidden_size, requires_grad=False)
         self.check_forward_hidden(input, hx)
         return self._backend.GRUCell(
             input, hx,


Test patch: