AttributeError: 'numpy.ndarray' object has no attribute 'log'

username: Jhone
cooked: <p>File “/home/ali/BioNet_project/pytorch_version/utils.py”, line 90, in train<br>
soft_score = soft_loss(y, x, beta=0.95)<br>
File “/home/ali/BioNet_project/pytorch_version/metrics.py”, line 23, in soft_loss<br>
cross_entropy = F.nll_loss(y_pred.log(), y_true, size_average=False)<br>
AttributeError: ‘numpy.ndarray’ object has no attribute ‘log’</p>

username: ptrblck
cooked: <p>It seems you are trying to pass a numpy array to <code>F.nll_loss</code>, while a PyTorch tensor is expected.<br>
I’m not sure how <code>y_pred</code> is calculated, but note that using numpy array would detach them from the computation graph, so you should stick to PyTorch tensors and operations, if possible.</p>

username: Jhone
cooked: <p>thanks for your reply. yes, I am trying to calculate the soft_loss but stuck in this problem and don’t know what to do.</p>
<h1>metrics</h1>
<pre><code>        x, y = output.detach().cpu().numpy(), y.detach().cpu().numpy()
        soft_score = soft_loss(y, x, beta=0.95)
</code></pre>
<h5></h5>
<p>def soft_loss(y_true, y_pred, beta=0.95):<br>
cross_entropy = F.nll_loss(y_pred.log(), y_true, size_average=False)<br>
soft_reed = -y_pred * torch.log(y_pred + 1e-8)<br>
return beta * cross_entropy + (1 - beta) * torch.sum(soft_reed)</p>

username: ptrblck
cooked: <p>Remove the <code>.numpy()</code> operations and pass the tensors to the <code>soft_loss</code> function.<br>
As described before, you won’t be able to call <code>backward()</code> on the loss and calculate gradients with it, since you are also explicitly detaching the tensors, but I assume that’s wanted.</p>

username: Jhone
cooked: <p>thanks for your suggestion and i have followed your words but facing now this issue.<br>
###<span class="hashtag">#modification</span>######<br>
# # loss<br>
# l = criterion(output, y)<br>
# # tot_loss += l.item()<br>
# l.backward()<br>
# optimizer.step()</p>
<pre><code>        # metrics
        x, y = output.detach().cpu(), y.detach().cpu()

        # iou_score = iou(y, x)
        # dice_score = dice_coef(y, x)
        soft_score = soft_loss(y, x)
        hard_score = hard_loss(y, x)
</code></pre>
<p>#####<span class="hashtag">#metrics</span>###########<br>
import numpy as np<br>
import torch.nn.functional as F</p>
<p>def soft_loss(y_true, y_pred, beta=0.95):<br>
cross_entropy = F.nll_loss(y_pred.log(), y_true, size_average=False)<br>
soft_reed = -y_pred * torch.log(y_pred + 1e-8)<br>
return beta * cross_entropy + (1 - beta) * torch.sum(soft_reed)</p>
<p>####<span class="hashtag">#errorrr</span>####<br>
File “/home/ali/BioNet_project/pytorch_version/utils.py”, line 90, in train<br>
soft_score = soft_loss(y, x)<br>
File “/home/ali/BioNet_project/pytorch_version/metrics.py”, line 23, in soft_loss<br>
cross_entropy = F.nll_loss(y_pred.log(), y_true, size_average=False)<br>
File “/home/ali/miniconda3/envs/bio/lib/python3.6/site-packages/torch/nn/functional.py”, line 2117, in nll_loss<br>
ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4</p>

username: Jhone
cooked: <p>This is the shape of my data ‘‘data shape: (240, 512, 512, 3) (240, 512, 512, 1)’’.</p>