How to extract values from type built-in method numpy of Tensor object

username: Lighting_Up
cooked: <p>I am running this following code to do prediction for my model. After I am trying plot a graph of practiced values - unfortunately the output of my variable Y_pred is of the type that I cannot ready the values:<br>
def predict(model,testloader, X_test, Y_test,  epoch, n_epochs):<br>
model.eval()<br>
Y_pred = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<pre><code>for data, target in test_loader:
    if torch.cuda.is_available():
        data = data.cuda()
    output = model(data)
    print(output)
    Y_pred.append(output.numpy)


Y_pred = np.array(Y_pred)
plot_prediction(X_test, Y_test, Y_pred, epoch, n_epochs) 
</code></pre>
<p>When  plotting , I am getting the following<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/2/427c061dd49e5a743699252721611dd5c1ff20e3.png" data-download-href="https://discuss.pytorch.org/uploads/default/427c061dd49e5a743699252721611dd5c1ff20e3" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/2/427c061dd49e5a743699252721611dd5c1ff20e3.png" alt="image" data-base62-sha1="9u9erHOiwADJcbgZbbJvXXq8Yfh" width="690" height="126" data-dominant-color="E0DFDF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">871×160 22 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Aman_Singh
cooked: <p>Try   <code>Y_pred.append(output.numpy())</code>.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/1/31d0043af8445bd42faa048d3784f0963a57c938.jpeg" alt="torchnumpy" data-base62-sha1="76F4iMccPaNcnKhMkc0HyX86G2A" width="635" height="197"></p>

username: Lighting_Up
cooked: <aside class="quote no-group" data-username="Aman_Singh" data-post="2" data-topic="116182">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/aman_singh/48/30084_2.png" class="avatar"> Aman_Singh:</div>
<blockquote>
<p>output.numpy()</p>
</blockquote>
</aside>
<p>I have tried and getting this:<br>
RuntimeError: Can’t call numpy() on Variable that requires grad. Use var.detach().numpy() instead.</p>

username: Aman_Singh
cooked: <p>Try <code>Y_pred.append(output.detach().numpy())</code>.<br>
I overlooked this part. I think it’s coming because <code>Y_pred</code> is on cpu while <code>output</code> is on cuda. You have to transfer it to cpu before appending  it which is what <code>detach</code> does.</p>

username: Lighting_Up
cooked: <p>Thank so much Aman_Singh for leading as a newbie with pytorch <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":smile:">. I changed my code to:<br>
output = model(data)<br>
output=output.detach().numpy()<br>
print(output)<br>
Y_pred.append(output)<br>
print(Y_pred.shape)</p>
<p>And I am getting the values and printed the shape : (300, 1, 1) of my tensor.<br>
Do I  have to reshape it in order plot it ?  my X_test is of shape (300,)</p>
<p>Cheers</p>

username: Aman_Singh
cooked: <p>Seems like you’ll have to . It kind of depends upon what your <code>plot_prediction</code> actually does.</p>

username: Lighting_Up
cooked: <p>Here is my plot function<br>
def plot_prediction(X_test, Y_test, Y_pred, epoch, n_epochs):</p>
<pre><code>fig=plt.figure()
plt.clf()
ax = plt.subplot(1,1,1)
ax.set_ylim([-1,1])
plt.title("Epoch #"+str(epoch)+"/"+str(n_epochs))
plt.plot(X_test[:,0],Y_test,label="True")
print(Y_pred)
plt.plot(X_test[:,0],Y_pred,label="Predicted")
plt.legend()
plt.savefig('test.png')
plt.close(fig)
return</code></pre>

username: Aman_Singh
cooked: <aside class="quote no-group" data-username="Lighting_Up" data-post="7" data-topic="116182">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lighting_up/48/30398_2.png" class="avatar"> Lighting_Up:</div>
<blockquote>
<p><code>plt.plot(X_test[:,0],Y_test,label="True")</code></p>
</blockquote>
</aside>
<p>and</p>
<aside class="quote no-group" data-username="Lighting_Up" data-post="7" data-topic="116182">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lighting_up/48/30398_2.png" class="avatar"> Lighting_Up:</div>
<blockquote>
<p><code>plt.plot(X_test[:,0],Y_pred,label="Predicted")</code></p>
</blockquote>
</aside>
<p>These lines might throw error because indexing will fail on X_test.<br>
Also, <code>plt.plot()</code>can handle only 2D values afaik but <code>Y_pred</code> is of shape (300, 1, 1).</p>
<p>I’d suggest you to handle these issues by reshaping them appropriately and then checking whether the plot looks reasonable.</p>