2D input to 3D expansion using "stack", getting "expected 3D tensor"

I have a 2D input [Observations x Features] that I am trying to expand to 3D using nonlinear transformations so that I can use various convolution functions and architectures on it.

I did some searching and found the “stack” function which seems to achieve this. In my “forward” function I have:

x = tc.stack([tc.atan(x),tc.exp(x),x], dim = 1)

This results in an output that looks like [Observations x Channels x Features]

However, when I try to run my code (pasted below) I get a “expected 3D tensor”. I tried using different “dim” positions and I still get the error.

What is the most correct way to go about expanding a 2D input into a 3D input for use inside the model?

# Setting up the net
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        
        self.elu = nn.ELU(alpha = 1.0, inplace = False)
        
        self.conv1 = nn.Conv2d(in_channels = 3, 
                               out_channels = 8, 
                               kernel_size = 1,
                               padding = 0)
        
        self.conv2 = nn.Conv2d(in_channels = 8, 
                               out_channels = 12, 
                               kernel_size = 3,
                               padding = 0)
        
        self.fc1 = nn.Linear(19*19*12, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 2)

    def forward(self, x):
        
        x = tc.stack([tc.atan(x),tc.exp(x),x], dim = 1)
        
        x = self.elu(self.conv1(x))
        
        x = self.elu(self.conv2(x))
        
        x = x.view(-1, 19*19*12)
        
        x = self.elu(self.fc1(x))
        
        x = self.elu(self.fc2(x))
        
        x = self.fc3(x)
        return x
    
net = Net()

# Loss function and optimizer

import torch.optim as optim

criterion = nn.CrossEntropyLoss()

optimizer = optim.Adam(net.parameters(), 
                       lr = 0.001, 
                       betas = (0.9, 0.99),
                       weight_decay = 1e-4)

# Training
for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i in range(10000):
        
        # Mini batch indices
        inx  = np.random.choice(a = list(range(len(train_out))),
                                     size = 32,
                                     replace = False)
        # get the inputs
        inputs, labels = train_in[tc.LongTensor(inx)],train_out[tc.LongTensor(inx)]

        # wrap them in Variable
        inputs, labels = Variable(inputs), Variable(labels)

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.data[0]
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')

Side note, I am using Spyder and no PyTorch variables seem to appear in the “Variable explorer”. Is there a quick guide on how to figure out dimentionality, types, memory size, etc. of torch arrays?

I just tried this in an interpreter:

import torch
a=torch.randn(10, 20)
torch.stack([torch.atan(a), torch.exp(a), a], dim=1)

It seems to have worked as expected.

What’s the full error and stack-trace that you see and what version of pytorch are you on? print(tc.__version__)

print(tc.version) #0.1.12_2

The command works outside of the model just fine, its giving me a problem when I try to use it inside of “forward”. Here is the full stack-trace:

Traceback (most recent call last):

  File "<ipython-input-145-6f218d24ab72>", line 76, in <module>
    outputs = net(inputs)

  File "/home/ml-comp-01/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)

  File "<ipython-input-145-6f218d24ab72>", line 29, in forward
    x = self.elu(self.conv1(x))

  File "/home/ml-comp-01/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)

  File "/home/ml-comp-01/anaconda3/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 237, in forward
    self.padding, self.dilation, self.groups)

  File "/home/ml-comp-01/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py", line 40, in conv2d
    return f(input, weight, bias)

RuntimeError: expected 3D tensor

from the stack-trace it looks like your problem is not stack.
You are sending 3D inputs to Conv2d which wants 4D inputs (mini-batch x channels x height x width).
You need to use Conv1d for your purposes. I’ve modified the definition that’ll work for you:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        self.elu = nn.ELU(alpha = 1.0, inplace = False)

        self.conv1 = nn.Conv1d(in_channels = 3,
                               out_channels = 8,
                               kernel_size = 1,
                               padding = 0)

        self.conv2 = nn.Conv1d(in_channels = 8,
                               out_channels = 12,
                               kernel_size = 3,
                               padding = 0)

        self.fc1 = nn.Linear(96, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 2)

    def forward(self, x):

        x = tc.stack([tc.atan(x),tc.exp(x),x], dim = 1)
        x = self.elu(self.conv1(x))
        x = self.elu(self.conv2(x))
        x = x.view(-1, 96)
        x = self.elu(self.fc1(x))
        x = self.elu(self.fc2(x))
        x = self.fc3(x)
        return x
1 Like

saying that, the error message you got was terrible, i’ll fix that on master.

2 Likes

Thank you for the quick reply. The model essentially is supposed to be something like:

[32 observations x 21 features] --> [32 Observations x 3 Non-linear expansions * 21 features] == Input --> layers…

What I’m trying to do with the second step is to get PyTorch to treat this like an image of depth 3 or deeper.

I am able to successfully run and modify code from the CIFAR-10 tutorial.

When I run your modified code from above I get a massive set of errors with the title “An error occurred while starting the kernel”:

*** Error in `/home/ml‑comp󈚥/anaconda3/bin/python’: free(): invalid pointer: 0x00007f17ac63dae0 ***
======= Backtrace: =========
/lib/x86_64‑linux‑gnu/libc.so.6(+0x7908b)[0x7f17e7afd08b]
/lib/x86_64‑linux‑gnu/libc.so.6(+0x826fa)[0x7f17e7b066fa]
/lib/x86_64‑linux‑gnu/libc.so.6(cfree+0x4c)[0x7f17e7b0a12c]

Here are some system details:

Processor: Intel® Core™ i7-6700K CPU @ 4.00GHz × 8
Graphics: GeForce GTX 970/PCIe/SSE2
OS: Ubuntu 17.04 64-bit

nvcc --version # Cuda compilation tools, release 8.0, V8.0.44
Python: 3.6.0
GCC: 4.4.7
Spyder IDE: 3.1.4

I think I figured it out. My target output data were formatted as a single dimensional DoubleTensor and the input data were also formatted as a 2D DoubleTensor.

Formatting the inputs as a FloatTensor and LongTensor for the outputs seems to have fixed the issue. I understand that this currently limits the precision to 32-bits.

If you’d like some more information on my error I can give you any relevant information outside of this post. Thanks for the above suggestion regarding convolutions!