AttributeError: 'numpy.ndarray' object has no attribute 'float'

in 0422_FFNetworksWithPytorch

i’m facing this error and i looked on stackoverflow but couldn’t solve this.

AttributeError                            Traceback (most recent call last)

<ipython-input-25-5ecc76e33fa6> in <module>()
     12 epochs = 10000
     13 
---> 14 X_train = X_train.float()
     15 Y_train = Y_train.long()
     16 

AttributeError: 'numpy.ndarray' object has no attribute 'float'

How do we convert ndarray to float. I tried .astype(float) but it resulted in other error while computing model function.

 19 
 20 for epoch in range(epochs):
---> 21   y_hat = model(X_train)
 22   loss = loss_fn(y_hat, Y_train)
 23   loss.backward()

<ipython-input-23-62d5caab50b6> in model(x)
      1 # now wo do the forward propagation.
      2 def model(x):
----> 3   a1 = torch.matmul(x, weights1) + bias1 # (N, 2) x (2, 2) -> (N, 2)
      4   h1 = a1.sigmoid() # (N, 2)
      5   a2 = torch.matmul(h1, weights2) + bias2 # (N, 2) x (2, 4) -> (N, 4)

TypeError: matmul(): argument 'input' (position 1) must be Tensor, not numpy.ndarray

Hi @Arshad_Ali
The given input should also be a torch tensor, and not numpy array.
Please convert X_train to a float tensor.
You can refer this thread for more info.

1 Like

Thank you.
It worked by doing this

X_train = torch.tensor(X_train).float()