Tensors have attribute squeeze in a for loop, but not in a list comprehension

Using PyTorch 0.4.1 on Ubuntu 16.04
Riddle me this:

In my model, I have a list of convolutional filters that I want to train in parallel.

In my model’s forward function, I want to run my embedded input data through each convolutional layer, followed by a relu, followed by squeezing out the last dimension, which is just size 1.

So, I try to do this with a list comprehension, like so:

convs = [F.relu(conv_layer(embedded)).sqeeze(-1) for conv_layer in self.conv_layers]

This results in the following error message:

Traceback (most recent call last):
  File "main.py", line 230, in <module>
    main()
  File "main.py", line 178, in main
    loss = train_epoch(discriminator, dis_data_iter, dis_criterion, dis_optimizer)
  File "main.py", line 79, in train_epoch
    pred = model.forward(data_var)
  File "/home/bgenchel/SeqGAN/Curious-Musical-SeqGAN/src/models/baseline/discriminator.py", line 38, in forward
    convs = [F.relu(conv(embedded)).sqeeze(-1) for conv in self.conv_layers]
  File "/home/bgenchel/SeqGAN/Curious-Musical-SeqGAN/src/models/baseline/discriminator.py", line 38, in <listcomp>
    convs = [F.relu(conv(embedded)).sqeeze(-1) for conv in self.conv_layers]
AttributeError: 'Tensor' object has no attribute 'sqeeze'

HOWEVER, if I do the exact same thing using a for loop:

convs = []
for conv_layer in self.conv_layers:
    convs.append(F.relu(conv_layer(embedded)).squeeze(-1))

Then everything is fine. No error message; code proceeds as usual.
???
The way I figured this out was to use pdb, and then first run the list comprehension without the squeeze function, then try to use the squeeze method on each individual element of the resulting list. This worked fine, and I got no complaints about the squeeze method not existing.

your spelling of squeeze is wrong, you write it as sqeeze

2 Likes

LOL thank you very much