dsgiitr / d2l-pytorch

This project reproduces the book Dive Into Deep Learning (https://d2l.ai/), adapting the code from MXNet into PyTorch.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why use np.asscalar nor item() to get scalar value?

ciuji opened this issue · comments

commented

in CH_4_Data_manipulation, we use

We can transform the result into a scalar in Python using the asscalar function of numpy. In the following example, the $\ell_2$ norm of x yields a single element tensor. The final result is transformed into a scalar.
In[20]: np.asscalar(x.norm())
Out[20]: 22.494443893432617

Why don't just use x.norm().item() to get the scalar, which seems much easier? Are their any differences?