# Converting torch Variable/Tesnor to python data type

**URL:** <https://discuss.pytorch.org/t/converting-torch-variable-tesnor-to-python-data-type/1930>\
**Category:** Uncategorized\
**Created:** [April 17, 2017, 5:27pm UTC](https://discuss.pytorch.org/t/converting-torch-variable-tesnor-to-python-data-type/1930 "2017-04-17T17:27:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scientist1642](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/scientist1642/32/346_2.png) [@scientist1642](https://discuss.pytorch.org/u/scientist1642)\
**Post date:** [April 17, 2017, 5:27pm UTC](https://discuss.pytorch.org/t/converting-torch-variable-tesnor-to-python-data-type/1930/1 "2017-04-17T17:27:15Z")

</div>

Suppose I have 1 x 1 torch variable and I want to convert it into a float, is there a better way than doing this?  
`x.data.numpy()[0, 0]`  
(Couldn’t find it in docs).

---

<div class="post-metadata">

**Author:** ![jekbradbury](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jekbradbury/32/14_2.png) [@jekbradbury](https://discuss.pytorch.org/u/jekbradbury)\
**Post date:** [April 17, 2017, 5:42pm UTC](https://discuss.pytorch.org/t/converting-torch-variable-tesnor-to-python-data-type/1930/2 "2017-04-17T17:42:56Z")

</div>

You don’t need the `.numpy()` part.

---

<div class="post-metadata">

**Author:** ![FrancoisFleuret](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/francoisfleuret/32/5770_2.png) [@FrancoisFleuret](https://discuss.pytorch.org/u/FrancoisFleuret)\
**Post date:** [July 3, 2017, 5:51am UTC](https://discuss.pytorch.org/t/converting-torch-variable-tesnor-to-python-data-type/1930/3 "2017-07-03T05:51:58Z")

</div>

Regarding Variables, note that you cannot convert one directly to a numpy array, nor one of its coefficients to a scalar type. Since Variables only “wrap” around Tensors, to keep the autograd semantic working, if x is a Variable, x[i] is a 1d Variable of size 1.

To access the values of [the Tensor of] a Variable, you have to do it through its .data attribute, which is a Tensor.
