In [2]:
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

from utils import forward_tracer, backward_tracer, Char2Vec, num_flat_features

import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import numpy as np

from tqdm import tqdm

from IPython.display import clear_output

In [3]:
torch.nn


Out[3]:
<module 'torch.nn' from '/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/__init__.py'>

In [33]:
?nn.GRU

In [25]:
rnn = nn.GRU(10, 20, 2)
input = Variable(torch.randn(5, 3, 10))
h0 = Variable(torch.randn(2, 3, 20))
output, hn = rnn(input, h0)
print(output.size())


torch.Size([5, 3, 20])

In [32]:
class GruRNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(GruRNN, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        
        self.encoder = nn.Linear(input_size, hidden_size)
        self.gru = nn.GRU(hidden_size, hidden_size, 1)
        self.decoder = nn.Linear(hidden_size, output_size)
        self.softmax = F.softmax
        
    def forward(self, x, hidden):
        embeded = self.encoder(x)
        gru_output, hidden = self.gru(embeded.view(-1, 1, self.input_size), hidden.view(1, 1, -1))
        output = self.decoder(gru_output.view(-1, self.hidden_size))
        return output, hidden

input_size = 101
hidden_size = 101
output_size = 101

gRNN = GruRNN(input_size, hidden_size, output_size)

gRNN(Variable(torch.FloatTensor(10000, 101)),
     Variable(torch.FloatTensor(101)))


Out[32]:
(Variable containing:
 -0.0958 -0.0411 -0.0050  ...   0.0697 -0.0073 -0.0698
 -0.0875 -0.0418 -0.0105  ...   0.0676 -0.0271 -0.0742
 -0.0798 -0.0431 -0.0150  ...   0.0665 -0.0374 -0.0760
           ...             ⋱             ...          
 -0.0676 -0.0447 -0.0241  ...   0.0642 -0.0454 -0.0773
 -0.0676 -0.0447 -0.0241  ...   0.0642 -0.0454 -0.0773
 -0.0676 -0.0447 -0.0241  ...   0.0642 -0.0454 -0.0773
 [torch.FloatTensor of size 10000x101], Variable containing:
 ( 0 ,.,.) = 
 
 Columns 0 to 8 
    0.1587 -0.0741  0.0693 -0.1123  0.1256  0.0301  0.0243  0.0200  0.0807
 
 Columns 9 to 17 
    0.0037 -0.0817  0.0118 -0.0359  0.0019  0.0554 -0.0608 -0.0108 -0.0539
 
 Columns 18 to 26 
   -0.0195  0.0433 -0.0090  0.1201 -0.1222  0.0183 -0.0192 -0.0266 -0.0782
 
 Columns 27 to 35 
    0.0480  0.0021 -0.0520 -0.0773 -0.1464 -0.0051  0.0316 -0.0374  0.0037
 
 Columns 36 to 44 
    0.0061  0.1132  0.0179 -0.0742 -0.0382 -0.0021 -0.0242 -0.0183 -0.0139
 
 Columns 45 to 53 
    0.1005 -0.0213 -0.0371  0.0428  0.0005  0.0425  0.1150 -0.0952 -0.0801
 
 Columns 54 to 62 
    0.0890  0.1392  0.0744  0.0631 -0.0649 -0.0503  0.0721  0.1257 -0.0899
 
 Columns 63 to 71 
   -0.0216 -0.0969  0.0408  0.0397  0.0022  0.0698  0.0525 -0.0118 -0.0350
 
 Columns 72 to 80 
   -0.1321  0.0301  0.0422  0.1623  0.0060  0.0482  0.1194  0.0037  0.0807
 
 Columns 81 to 89 
   -0.1701 -0.1468 -0.1598  0.0265  0.0615  0.0271  0.0798  0.0570  0.0425
 
 Columns 90 to 98 
   -0.0738  0.0095 -0.0225 -0.0784  0.0523 -0.0454  0.0561  0.1989 -0.0056
 
 Columns 99 to 100 
   -0.0936 -0.0985
 [torch.FloatTensor of size 1x1x101])

In [18]:
?nn.Linear

In [15]:
gRNN(Variable(torch.FloatTensor(10000, 101)),
     Variable(torch.FloatTensor(1, 10, 101)))


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-15-e8f9c0429e47> in <module>()
      1 gRNN(Variable(torch.FloatTensor(10000, 101)),
----> 2      Variable(torch.FloatTensor(1, 10, 101)))

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    200 
    201     def __call__(self, *input, **kwargs):
--> 202         result = self.forward(*input, **kwargs)
    203         for hook in self._forward_hooks.values():
    204             hook_result = hook(self, input, result)

<ipython-input-6-b3b5f9382efb> in forward(self, x, hidden)
     12     def forward(self, x, hidden):
     13         embeded = self.encoder(x)
---> 14         gru_output, hidden = self.gru(embeded.view(embeded.size()[0], 1, -1), hidden)
     15         output = self.decoder(gru_output)
     16         return output, hidden

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    200 
    201     def __call__(self, *input, **kwargs):
--> 202         result = self.forward(*input, **kwargs)
    203         for hook in self._forward_hooks.values():
    204             hook_result = hook(self, input, result)

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/modules/rnn.py in forward(self, input, hx)
     89             dropout_state=self.dropout_state
     90         )
---> 91         output, hidden = func(input, self.all_weights, hx)
     92         if is_packed:
     93             output = PackedSequence(output, batch_sizes)

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in forward(input, *fargs, **fkwargs)
    325         else:
    326             func = AutogradRNN(*args, **kwargs)
--> 327         return func(input, *fargs, **fkwargs)
    328 
    329     return forward

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in forward(input, weight, hidden)
    225             input = input.transpose(0, 1)
    226 
--> 227         nexth, output = func(input, hidden, weight)
    228 
    229         if batch_first and batch_sizes is None:

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in forward(input, hidden, weight)
     65                 l = i * num_directions + j
     66 
---> 67                 hy, output = inner(input, hidden[l], weight[l])
     68                 next_hidden.append(hy)
     69                 all_output.append(output)

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in forward(input, hidden, weight)
     94         steps = range(input.size(0) - 1, -1, -1) if reverse else range(input.size(0))
     95         for i in steps:
---> 96             hidden = inner(input[i], hidden, *weight)
     97             # hack to handle LSTM
     98             output.append(isinstance(hidden, tuple) and hidden[0] or hidden)

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/nn/_functions/rnn.py in GRUCell(input, hidden, w_ih, w_hh, b_ih, b_hh)
     40     h_r, h_i, h_n = gh.chunk(3, 1)
     41 
---> 42     resetgate = F.sigmoid(i_r + h_r)
     43     inputgate = F.sigmoid(i_i + h_i)
     44     newgate = F.tanh(i_n + resetgate * h_n)

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/autograd/variable.py in __add__(self, other)
    742 
    743     def __add__(self, other):
--> 744         return self.add(other)
    745     __radd__ = __add__
    746 

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/autograd/variable.py in add(self, other)
    291 
    292     def add(self, other):
--> 293         return self._add(other, False)
    294 
    295     def add_(self, other):

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/autograd/variable.py in _add(self, other, inplace)
    285     def _add(self, other, inplace):
    286         if isinstance(other, Variable):
--> 287             return Add(inplace)(self, other)
    288         else:
    289             assert not torch.is_tensor(other)

/home/ge/anaconda3/envs/deep-learning/lib/python3.6/site-packages/torch/autograd/_functions/basic_ops.py in forward(self, a, b)
     18             return a.add_(b)
     19         else:
---> 20             return a.add(b)
     21 
     22     def backward(self, grad_output):

RuntimeError: inconsistent tensor size at /data/users/soumith/miniconda2/conda-bld/pytorch-cuda80-0.1.10_1488758793045/work/torch/lib/TH/generic/THTensorMath.c:827

In [ ]: