PetrochukM / PyTorch-NLP

Basic Utilities for PyTorch Natural Language Processing (NLP)
https://pytorchnlp.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.21k stars 258 forks source link

Apply SRU layer: 'Variable' object has no attribute 'new_zeros' #45

Closed dhairyadalal closed 6 years ago

dhairyadalal commented 6 years ago

Unable to recreate the SRU example code provided in readme. Getting a Variable object has no attribute 'new_zeros'. Running pytorch .3.0.

Expected Behavior

RETURNS: ( output [torch.FloatTensor (6x3x20)], hidden_state [torch.FloatTensor (2x3x20)] )

Actual Behavior

`--------------------------------------------------------------------------- AttributeError Traceback (most recent call last)

in () ----> 1 sru(input_) /anaconda3/envs/nlp/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 355 result = self._slow_forward(*input, **kwargs) 356 else: --> 357 result = self.forward(*input, **kwargs) 358 for hook in self._forward_hooks.values(): 359 hook_result = hook(self, input, result) /anaconda3/envs/nlp/lib/python3.6/site-packages/torchnlp/nn/sru.py in forward(self, input_, c0) 509 dir_ = 2 if self.bidirectional else 1 510 if c0 is None: --> 511 zeros = input_.new_zeros(input_.size(1), self.hidden_size * dir_) 512 c0 = [zeros for i in range(self.num_layers)] 513 else: /anaconda3/envs/nlp/lib/python3.6/site-packages/torch/autograd/variable.py in __getattr__(self, name) 65 if name in self._fallthrough_methods: 66 return getattr(self.data, name) ---> 67 return object.__getattribute__(self, name) 68 69 def __getitem__(self, key): AttributeError: 'Variable' object has no attribute 'new_zeros' ` ## Steps to Reproduce the Problem `from torchnlp.nn import SRU import torch input_ = torch.autograd.Variable(torch.randn(6, 3, 10)) sru = SRU(10, 20) sru(input_) `
PetrochukM commented 6 years ago

Hi There!

new_zeros is a PyTorch 0.4 feature. I'll update the documentation concerning PyTorch support. But overall, <= PyTorch 0.3 is not supported due to the many breaking changes in PyTorch 0.4.

Thanks!

PetrochukM commented 6 years ago

Hi!

The dependency on PyTorch 0.4 has been updated in the README via this PR: https://github.com/PetrochukM/PyTorch-NLP/pull/46

Thanks!

dhairyadalal commented 6 years ago

you may also want to update the requirements.txt file to reflect pytorch 0.4 is required

PetrochukM commented 6 years ago

@dhairyadalal Done! Thank you!

https://github.com/PetrochukM/PyTorch-NLP/commit/94dc8dd28c6bfda2ff05b746a6dbfe4d3787017b