pythonpytorchtensor

How can I plot pytorch tensor?


I would like to plot pytorch gpu tensor:

input= torch.randn(100).to(device)
output = torch.where(input>=0, input, -input)

input = input.('cpu').detach().numpy().copy()
output = output.('cpu').detach().numpy().copy()

plt.plot(input,out)

However I try to convert those tensors into cpu, numpy, it does not work. How can I plot the tensors ?


Solution

  • Does this work?

    plt.plot(input.cpu().numpy(),output.cpu().numpy())
    

    Alternatively you can try,

    plt.plot(input.to('cpu').numpy(),output.to('cpu').numpy())