Plot a 1d array from a 3d torch tensor

import torch
import matplotlib.pyplot as plt

def get_number(swnew):
    temp = torch.div(1, swnew)
    temp[temp == float("Inf")] = 200
    return temp

if __name__ == '__main__':

    swnew     = torch.zeros(10, 1, 1, requires_grad=True, dtype=torch.float64)    
    y= get_number(swnew)
    plt.plot(swnew[:,1,1], y[:,1,1])


Hey, sorry this one is pretty easy, but I don’t know why it shows
IndexError: index 1 is out of bounds for dimension 1 with size 1

Thank you

You intialized swnew with shape (10, 1, 1) and while plotting you are trying to access an axis which is not there. .i.e. you only have one axis and you are accessing 2nd axis.
remember in python indexing starts from 0.

you should try something like swnew[: ,0, 0]

1 Like