# Operation between Tensor and Variable

**URL:** <https://discuss.pytorch.org/t/operation-between-tensor-and-variable/1286>\
**Category:** Uncategorized\
**Created:** [March 23, 2017, 6:34am UTC](https://discuss.pytorch.org/t/operation-between-tensor-and-variable/1286 "2017-03-23T06:34:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zafarullah\_Mahmood](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/z/c77e96/32.png) [@Zafarullah\_Mahmood](https://discuss.pytorch.org/u/Zafarullah_Mahmood)\
**Post date:** [March 23, 2017, 6:34am UTC](https://discuss.pytorch.org/t/operation-between-tensor-and-variable/1286/1 "2017-03-23T06:34:42Z")

</div>

Suppose I do this:

```python
x = Variable(torch.rand(3,5))
y = torch.Tensor(torch.rand(3,5))
print(x + y)

```

It gives error. I can use `Variable.data` if I want the output to be of `Tensor` type. But I want the sum to be a `Variable`. How can I do this?

1. This example is trivial. When both x and y are matrices, things get complicated (like doing operation between a Variable and a Tensor that has come through register\_buffer).
2. I am new to PyTorch. So is there a good reference that explain all these things?

---

<div class="post-metadata">

**Author:** ![JerryLin](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@JerryLin](https://discuss.pytorch.org/u/JerryLin)\
**Post date:** [March 23, 2017, 8:39am UTC](https://discuss.pytorch.org/t/operation-between-tensor-and-variable/1286/2 "2017-03-23T08:39:34Z")

</div>

Why not wrap your `y` into a `Variable` object?

---

<div class="post-metadata">

**Author:** ![Zafarullah\_Mahmood](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/z/c77e96/32.png) [@Zafarullah\_Mahmood](https://discuss.pytorch.org/u/Zafarullah_Mahmood)\
**Post date:** [March 23, 2017, 8:44am UTC](https://discuss.pytorch.org/t/operation-between-tensor-and-variable/1286/3 "2017-03-23T08:44:43Z")

</div>

Thanks. Do I have to wrap y every time I use it?  
Also, this requires gradient with respect to y to be calculated during `backward`. Since y is a tensor (like running\_var in BatchNorm`, I don’t want the gradient to flow through it.

---

<div class="post-metadata">

**Author:** ![vabh](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/898d66/32.png) [@vabh](https://discuss.pytorch.org/u/vabh)\
**Post date:** [March 23, 2017, 9:41am UTC](https://discuss.pytorch.org/t/operation-between-tensor-and-variable/1286/4 "2017-03-23T09:41:08Z")

</div>

The `requires_grad` attrribute of a Variable is `False` by default. This means that the gradient will not flow though `y`

```
In [2]: y = torch.autograd.Variable(torch.Tensor(4))
In [3]: y.requires_grad
Out[3]: False
```

---

<div class="post-metadata">

**Author:** ![maggie](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/maggie/32/4466_2.png) [@maggie](https://discuss.pytorch.org/u/maggie)\
**Post date:** [August 18, 2017, 2:56am UTC](https://discuss.pytorch.org/t/operation-between-tensor-and-variable/1286/5 "2017-08-18T02:56:20Z")

</div>

I also have question about this. Why is it designed like this?
