How can I make a FloatTensor with requires_grad=True from a numpy array using PyTorch 0.4.0? : <p>Pytorch 0.4.0 introduced the merging on the Tensor and Variable classes. </p>

<p>Before this version, when I wanted to create a <code>Variable</code> with autograd from a numpy array I would do the following (where <code>x</code> is a numpy array):</p>

<pre><code>x = Variable(torch.from_numpy(x).float(), requires_grad=True)
</code></pre>

<p>With PyTorch version 0.4.0, <a href="http://pytorch.org/2018/04/22/0_4_0-migration-guide.html" rel="noreferrer">the migration guide</a> shows how we can create Tensors with autograd enabled, examples show you can do things such as </p>

<pre><code>x = torch.ones(3, 4, requires_grad=True) 
</code></pre>

<p>and also set <code>requires_grad</code> to existing tensors</p>

<pre><code>existing_tensor.requires_grad_()
</code></pre>

<p>I have tried the following three things to try and create a Tensor with <code>requires_grad=True</code> which give errors (where <code>x</code> is a numpy array):</p>

<p>The first is </p>

<pre><code>x = FloatTensor(x, requires_grad=True)
</code></pre>

<p>which gives the error</p>

<pre><code>TypeError: new() received an invalid combination of arguments - got 
(numpy.ndarray, requires_grad=bool), but expected one of:
 * (torch.device device)
 * (tuple of ints size, torch.device device)
      didn't match because some of the keywords were incorrect: 
requires_grad
 * (torch.Storage storage)
 * (Tensor other)
 * (object data, torch.device device)
      didn't match because some of the keywords were incorrect: 
requires_grad
</code></pre>

<p>The second is to do</p>

<pre><code>x = FloatTensor(x)
x.requires_grad()
</code></pre>

<p>And the third is </p>

<pre><code>x = torch.from_numpy(x).single()
x.requires_grad()
</code></pre>

<p>Which both throw the following error on the second line:</p>

<pre><code>TypeError: 'bool' object is not callable
</code></pre>

<p>These errors give me little hint at what I'm doing wrong, and since the latest version is so new its hard to find content online to help. How can I make a <code>FloatTensor</code> with <code>requires_grad=True</code> from a numpy array using PyTorch 0.4.0, preferably in a single line?</p>
