How to solve this problem?

X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)
TypeError: float() argument must be a string or a number, not ‘builtin_function_or_method’

Hi wsy!

This works for me with pytorch version 0.3.0.

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

And the output:

>>> import torch
>>> import numpy as np
>>>
>>> torch.__version__
'0.3.0b0+591e73e'
>>>
>>> d = 10
>>> X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)
>>> 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]

The only thing I can think of is perhaps the d you are using isn’t
what you think it is.

Best.

K. Frank

I checked what you said, but I still couldn’t solve the problem.

Hi wsy!

Could you run the complete, runnable script I posted in a fresh
python session, and post the complete output?

Best.

K. Frank

import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
momentum = 0.9
step_size=0.05
n_epochs = 6000
n_hidden_1=32
n_hidden_2=32
d_out=1
mse_loss_fn=nn.MSELoss()
neural_network=nn.Sequential(
nn.Linear(d,n_hidden_1),
nn.Tanh(),
nn.Linear(n_hidden_1,n_hidden_2),
nn.Tanh(),
nn.Linear(n_hidden_2,d_out)
)
optim = torch.optim.SGD(neural_network.parameters(), lr=step_size,momentum=momentum)#可以直接调用SGD
for i in range(n_epochs):
y_hat=neural_network(X)
loss = mse_loss_fn(y_hat, y)
optim.zero_grad()
loss.backward()
optim.step()
if i % (n_epochs // 10) == 0:
print(’{},\t{:.2f}’.format(i, loss.item()))

X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)
y_hat = neural_network(X_grid)
plt.scatter(X.numpy(),y.numpy())
plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)
plt.title(‘plot of $f(x)$ and $\hat{f}(x)$’)
plt.xlabel(’$x$’)
plt.ylabel(’$y$’)
plt.show()
This is my whole code. I checked that when the code<plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)> is commented out, there will be no error.But I can’t find the syntax error of this code.

Hi wsy!

Before you said that your error occurred in:

X_grid = torch.from_numpy(np.linspace(0,1,50)).float().view(-1, d)

But now you are saying it occurs in

plt.plot(X_grid.detach().numpy,y_hat.detach().numpy,‘r’)

Here, you are passing a function, numpy, to plt.plot(), rather than
the numerical values obtained by calling that function, numpy().

This line should be:

plt.plot(X_grid.detach().numpy(),y_hat.detach().numpy(),‘r’)

Note the two numpy() (rather than numpy).

As an aside, if this is indeed your entire code, d is not defined
anywhere. But, given that, I would have expected you to get a
“d not defined” error first at this line:

nn.Linear(d,n_hidden_1)

Best.

K. Frank

It’s settled. Thank you