How to index a tensor in a for loop?
I am getting an error that says my tensor has to be a long, byte, or bool tensor. Debugging showed me that it is not getting an index [i], but the actual row [i] of the tensor x. I need that index to grab another row of another tensor y, tho.
Any ideas how to solve this?
number_batches_in = int(len(dataset_in)/batch_size)
number_batches_out = int(len(dataset_out)/batch_size)

x = torch.empty(size=(number_batches_in, 800))
y = torch.empty(size=(number_batches_out,1200), dtype=torch.float64)

for index, (x1, x2) in enumerate(dataloader_in):
    batch = torch.cat((x1, x2), 0)
    x[index] = batch

for index, (y1, y2, y3) in enumerate(dataloader_out):
    batch = torch.cat((y1, y2, y3), 0)
    y[index] = batch


model = Network(800,1200,3,800,200)
SAVE_PATH = &quot;trained/model.dat&quot;
epochs = 5
learning_rate = 0.001
optimizer = optim.Adam(model.parameters(),lr=learning_rate, eps=1e-08)
hist_error = []
hist_loss = []
beta = 0.5

for epoch in range(epochs):
    epoch_error = []
    epoch_loss = []
    for i in x:
        optimizer.zero_grad()
        pred = model.forward(i)
        y_true = y[i]
        loss = torch.mean(torch.sum((pred - y_true)) ** 2)
        loss.backward()
        optimizer.step()
        error = torch.mean(torch.sqrt((pred - y[i]) ** 2)).detach().numpy()
        epoch_error.append(error)
        epoch_loss.append(loss.data.detach().numpy())
    hist_error.append(np.mean(epoch_error))
    hist_loss.append(np.mean(epoch_loss))
    print(&quot;Epoch %d -- loss %f, RMS error %f &quot; % (epoch+1, hist_loss[-1], hist_error[-1]))
torch.save(model.state_dict(), SAVE_PATH)
print(&quot;Model saved to %s&quot; % SAVE_PATH)

Error message:
Traceback (most recent call last):
  File &quot;/home/samim/miniconda3/envs/deep/lib/python3.6/site-packages/IPython/core/interactiveshell.py&quot;, line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File &quot;&lt;ipython-input-2-54864ad18480&gt;&quot;, line 1, in &lt;module&gt;
    runfile('/home/samim/Documents/train.py', wdir='/home/samim/Documents/')
  File &quot;/home/samim/.local/share/JetBrains/PyCharm2020.3/python/helpers/pydev/_pydev_bundle/pydev_umd.py&quot;, line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File &quot;/home/samim/.local/share/JetBrains/PyCharm2020.3/python/helpers/pydev/_pydev_imps/_pydev_execfile.py&quot;, line 18, in execfile
    exec(compile(contents+&quot;\n&quot;, file, 'exec'), glob, loc)
  File &quot;/home/samim/Documents/train.py&quot;, line 61, in &lt;module&gt;
    y_true = y[i]
IndexError: tensors used as indices must be long, byte or bool tensors


Either enumerate:
...
for j, row_x in enumerate(x):
    optimizer.zero_grad()
    pred = model.forward(row_x)
    y_true = y[j]
    loss = torch.mean(torch.sum((pred - y_true)) ** 2)
...

or better yet, zip:
...
for row_x, y_true in zip(x, y):
    optimizer.zero_grad()
    pred = model.forward(row_x)
    loss = torch.mean(torch.sum((pred - y_true)) ** 2)
...

