How to solve this problem?

username: wsy
cooked: <p>X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)<br>
TypeError: float() argument must be a string or a number, not ‘builtin_function_or_method’</p>

username: KFrank
cooked: <p>Hi wsy!</p>
<aside class="quote no-group" data-username="wsy" data-post="1" data-topic="78522" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/5e9695/48.png" class="avatar"> wsy:</div>
<blockquote>
<p>X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)<br>
TypeError: float() argument must be a string or a number, not ‘builtin_function_or_method’</p>
</blockquote>
</aside>
<p>This works for me with pytorch version 0.3.0.</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
import numpy as np

torch.__version__

d = 10
X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)
X_grid
</code></pre>
<p>And the output:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt;
&gt;&gt;&gt; torch.__version__
'0.3.0b0+591e73e'
&gt;&gt;&gt;
&gt;&gt;&gt; d = 10
&gt;&gt;&gt; X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)
&gt;&gt;&gt; X_grid

 0.0000  0.0204  0.0408  0.0612  0.0816  0.1020  0.1224  0.1429  0.1633  0.1837
 0.2041  0.2245  0.2449  0.2653  0.2857  0.3061  0.3265  0.3469  0.3673  0.3878
 0.4082  0.4286  0.4490  0.4694  0.4898  0.5102  0.5306  0.5510  0.5714  0.5918
 0.6122  0.6327  0.6531  0.6735  0.6939  0.7143  0.7347  0.7551  0.7755  0.7959
 0.8163  0.8367  0.8571  0.8776  0.8980  0.9184  0.9388  0.9592  0.9796  1.0000
[torch.FloatTensor of size 5x10]
</code></pre>
<p>The only thing I can think of is perhaps the <code>d</code> you are using isn’t<br>
what you think it is.</p>
<p>Best.</p>
<p>K. Frank</p>

username: wsy
cooked: <p>I checked what you said, but I still couldn’t solve the problem.</p>

username: KFrank
cooked: <p>Hi wsy!</p>
<aside class="quote no-group" data-username="wsy" data-post="3" data-topic="78522" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/5e9695/48.png" class="avatar"> wsy:</div>
<blockquote>
<p>I checked what you said, but I still couldn’t solve the problem.</p>
</blockquote>
</aside>
<p>Could you run the complete, runnable script I posted in a fresh<br>
python session, and post the complete output?</p>
<p>Best.</p>
<p>K. Frank</p>

username: wsy
cooked: <p>import torch<br>
import torch.nn as nn<br>
import numpy as np<br>
import matplotlib.pyplot as plt<br>
momentum = 0.9<br>
step_size=0.05<br>
n_epochs = 6000<br>
n_hidden_1=32<br>
n_hidden_2=32<br>
d_out=1<br>
mse_loss_fn=nn.MSELoss()<br>
neural_network=nn.Sequential(<br>
nn.Linear(d,n_hidden_1),<br>
nn.Tanh(),<br>
nn.Linear(n_hidden_1,n_hidden_2),<br>
nn.Tanh(),<br>
nn.Linear(n_hidden_2,d_out)<br>
)<br>
optim = torch.optim.SGD(neural_network.parameters(), lr=step_size,momentum=momentum)<span class="hashtag">#可以直接调用SGD</span><br>
for i in range(n_epochs):<br>
y_hat=neural_network(X)<br>
loss = mse_loss_fn(y_hat, y)<br>
optim.zero_grad()<br>
loss.backward()<br>
optim.step()<br>
if i % (n_epochs // 10) == 0:<br>
print(’{},\t{:.2f}’.format(i, loss.item()))</p>
<p>X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)<br>
y_hat = neural_network(X_grid)<br>
plt.scatter(X.numpy(),y.numpy())<br>
plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)<br>
plt.title(‘plot of $f(x)$ and $\hat{f}(x)$’)<br>
plt.xlabel(’$x$’)<br>
plt.ylabel(’$y$’)<br>
plt.show()<br>
This is my whole code. I checked that when the code&lt;plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)&gt; is commented out, there will be no error.But I can’t find the syntax error of this code.</p>

username: KFrank
cooked: <p>Hi wsy!</p>
<aside class="quote no-group" data-username="wsy" data-post="5" data-topic="78522" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/5e9695/48.png" class="avatar"> wsy:</div>
<blockquote>
<p>This is my whole code. I checked that when the code&lt;plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)&gt; is commented out, there will be no error.But I can’t find the syntax error of this code.</p>
</blockquote>
</aside>
<p>Before you said that your error occurred in:</p>
<pre data-code-wrap="python"><code class="lang-python">X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)
</code></pre>
<p>But now you are saying it occurs in</p>
<pre data-code-wrap="python"><code class="lang-python">plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)
</code></pre>
<p>Here, you are passing a function, <code>numpy</code>, to <code>plt.plot()</code>, rather than<br>
the numerical values obtained by calling that function, <code>numpy()</code>.</p>
<p>This line should be:</p>
<pre data-code-wrap="python"><code class="lang-python">plt.plot(X_grid.detach().numpy(),y_hat.detach().numpy(),‘r’)
</code></pre>
<p>Note the two <code>numpy()</code> (rather than <code>numpy</code>).</p>
<p>As an aside, if this is indeed your entire code, <code>d</code> is not defined<br>
anywhere.  But, given that, I would have expected you to get a<br>
“d not defined” error first at this line:</p>
<pre data-code-wrap="python"><code class="lang-python">nn.Linear(d,n_hidden_1)
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

username: wsy
cooked: <p>It’s settled. Thank you</p>