Invalid index in scatter

username: pesci85
cooked: <p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
from torch.autograd import Variable</p>
<p>class Generator(nn.Module):<br>
‘’’<br>
Generator<br>
‘’’<br>
def <strong>init</strong>(self, z=100, channels=3, d=128):<br>
super().<strong>init</strong>()<br>
self.z = z<br>
self.channels = channels<br>
self.deconv1_1 = nn.ConvTranspose2d(self.z, d<em>2, 4, 1, 0)<br>
self.deconv1_1_bn = nn.BatchNorm2d(d</em>2)<br>
self.deconv1_2 = nn.ConvTranspose2d(20, d<em>2, 4, 1, 0)<br>
self.deconv1_2_bn = nn.BatchNorm2d(d</em>2)<br>
self.deconv2 = nn.ConvTranspose2d(d<em>4, d</em>2, 4, 2, 1)<br>
self.deconv2_bn = nn.BatchNorm2d(d<em>2)<br>
self.deconv3 = nn.ConvTranspose2d(d</em>2, d, 4, 2, 1)<br>
self.deconv3_bn = nn.BatchNorm2d(d)<br>
self.deconv4 = nn.ConvTranspose2d(d, self.channels, 4, 2, 1)</p>
<pre><code>def forward(self, input, label):
    label_oh = Variable(label.data.new(label.size(0), 20).float().zero_())
    label = label.unsqueeze(1)
    label_oh.scatter_(1, label, 1)
    label = label_oh.view(label_oh.size(0), label_oh.size(1), 1, 1)
    input = input.view(input.size(0), -1, 1, 1)
    x = F.relu(self.deconv1_1_bn(self.deconv1_1(input)))
    y = F.relu(self.deconv1_2_bn(self.deconv1_2(label)))
    x = torch.cat([x, y], 1)
    x = F.relu(self.deconv2_bn(self.deconv2(x)))
    x = F.relu(self.deconv3_bn(self.deconv3(x)))
    features = x
    x = torch.tanh(self.deconv4(x))
    return x, features
</code></pre>
<p>this is a piece of the generator where for the cifar100 in this line because I entered 20 as a number of classes because I am considering training taking only the coarse_label which are 20 instead of the labels, in which case in this piece of code the number of classes is 100.<br>
self.deconv1_2 = nn.ConvTranspose2d(100, d<em>2, 4, 1, 0)<br>
self.deconv1_2_bn = nn.BatchNorm2d(d</em>2)<br>
self.deconv2 = nn.ConvTranspose2d(d<em>4, d</em>2, 4, 2, 1)<br>
self.deconv2_bn = nn.BatchNorm2d(d<em>2)<br>
self.deconv3 = nn.ConvTranspose2d(d</em>2, d, 4, 2, 1)<br>
self.deconv3_bn = nn.BatchNorm2d(d)<br>
self.deconv4 = nn.ConvTranspose2d(d, self.channels, 4, 2, 1)</p>
<pre><code>def forward(self, input, label):
    label_oh = Variable(label.data.new(label.size(0), 100).float().zero_())
    label = label.unsqueeze(1)
    label_oh.scatter_(1, label, 1)
</code></pre>
<p>In output it gives me the following error and I can’t understand what I have to change.<br>
label_oh.scatter_ (1, label, 1)<br>
RuntimeError: Invalid index in scatter at C: \ w \ 1 \ s \ windows \ pytorch \ aten \ src \ TH / generic / THTensorEvenMoreMath.cpp: 151Mi by error</p>

username: ptrblck
cooked: <p>Could you post the content of <code>label</code> before passing it to <code>scatter_</code>?<br>
Based on the code I assume that <code>label_oh</code> has the shape <code>[20, 100]</code> and contains zeros?</p>

username: pesci85
cooked: <p>I created the dataset in this way and then passed it to the loader<br>
import os<br>
import sys<br>
import pickle</p>
<p>from skimage import io<br>
import matplotlib.pyplot as plt<br>
import numpy<br>
import torch<br>
from torch.utils.data import Dataset</p>
<p>class CIFAR100Train(Dataset):<br>
“”“cifar100 test dataset, derived from<br>
torch.utils.data.DataSet<br>
“””</p>
<pre><code>def __init__(self, path, transform=None):
    #if transform is given, we transoform data using
    with open(os.path.join(path, 'train'), 'rb') as cifar100:
        self.data = pickle.load(cifar100, encoding='bytes')
    self.transform = transform
    
def __len__(self):
    return len(self.data['coarse_labels'.encode()])

def __getitem__(self, index):
    label = self.data['coarse_labels'.encode()][index]
    print(label)
    r = self.data['data'.encode()][index, :1024].reshape(32, 32)
    g = self.data['data'.encode()][index, 1024:2048].reshape(32, 32)
    b = self.data['data'.encode()][index, 2048:].reshape(32, 32)
    image = numpy.dstack((r, g, b))

    if self.transform:
        image = self.transform(image)
    return label, image
</code></pre>
<p>this is the piece of the train</p>
<p>for  i ,(inputs,coarse_labels) in enumerate(tqdm(data_loader, desc=f’EPOCH: {epoch+1}/{args.epochs}’)):<br>
inputs=inputs.to(device)<br>
#coarse_label_names=coarse_label_names.to(device)<br>
<span class="hashtag">#print</span>(coarse_labecl_name)<br>
coarse_labels = coarse_labels.to(device)</p>
<pre><code>                # train GAN
                if split == 'train':
                    loss_gan_dict = gan_trainer.forward_batch(inputs, coarse_labels)
                    fake_images = loss_gan_dict['generated_img'] # 64 x 3 x 32 x 32

                #if split == 'test':
                   
                        



                step = (epoch * len(data_loader)) + i

                # log train
                if split == 'train':
                    saver.log_loss("Discriminator/Loss", loss_gan_dict['discriminator'], step)
                    saver.log_loss("Discriminator/Real_Right_Loss", loss_gan_dict['discriminator_real_right'], step)
                    saver.log_loss("Discriminator/Real_Wrong_Loss", loss_gan_dict['discriminator_real_wrong'], step)
                    saver.log_loss("Discriminator/Fake_Loss", loss_gan_dict['discriminator_fake'], step)
                    saver.log_loss("Generator/Loss", loss_gan_dict['generator'], step)
                    saver.log_loss("Generator/Loss Fake", loss_gan_dict['generator_loss_fake'], step)

                    
            #evaluate generation of images putting generator in eval mode
            g_net.eval()
            z_in = torch.randn(len(coarse_labels), 100).to(device)
            for i in range(0, 20):
                label_vector = torch.ones(80, dtype=torch.int64).to(device)
                label_vector = label_vector*i
                z_in = torch.randn(len(label_vector), 100).to(device)
                results, _ = g_net(z_in, label_vector)
                saver.log_images("Fake Images 80/"+classes[i], epoch, results)

            # Generate 8 samples for each class with fixed noise
            labels_20 = torch.arange(0,20).to(device)
            labels_20 = labels_100.repeat(8)
            z_fixed = torch.randn(len(labels_20), 100).to(device)
            samples_20, _ = g_net(z_in, labels_20)
            saver.log_images("Fake Images/eval_20_fixed_noise", epoch, samples_20)

            # reset net to train mode
            g_net.train()</code></pre>

username: ptrblck
cooked: <p>I tried to isolate the code snippet, which might cause the issue, but this works fine:</p>
<pre><code class="lang-python">
for i in range(0, 20):
    label_vector = torch.ones(80, dtype=torch.int64).to(device)
    label_vector = label_vector*i
    z_in = torch.randn(len(label_vector), 100).to(device)                
    label = label_vector
    
    label_oh = label.new(label.size(0), 100).float().zero_()
    label = label.unsqueeze(1)
    label_oh.scatter_(1, label, 1)
</code></pre>
<p>Note that your <code>Dataset</code> returns <code>label, image</code> while you are using <code>inputs, coards_labels</code> in your <code>DataLoader</code> loop.<br>
I don’t know, if this mismatch might be related to your issue, but feel free to post a code snippet to reproduce this issue in case you get stuck.</p>
<p>PS: You can post code snippets by wrapping them into three backticks ```, which makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>