Creating tensor TypeError: can't convert np.ndarray of type numpy.object_

I am trying to impement this code federated-ml/network-threat-detection-using-federated-learning.ipynb at master · tuhinsharma121/federated-ml · GitHub

while creating these pytorch sensor

X = final_df.values
y = df['Target'].values

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.4, random_state=42)
train_inputs = torch.tensor(X_train,dtype=torch.float).tag("#iot", "#network","#data","#train")
train_labels = torch.tensor(y_train).tag("#iot", "#network","#target","#train")
test_inputs = torch.tensor(X_test,dtype=torch.float).tag("#iot", "#network","#data","#test")
test_labels = torch.tensor(y_test).tag("#iot", "#network","#target","#test")

i got these errors current_tensor = hook_self.torch.native_tensor(*args, **kwargs) TypeError: can’t convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, int64, int32, int16, int8, uint8, and bool. how to resolve this

I tried this code by reading other stack questions

train_inputs = torch.tensor(X_train.astype(np.float32)).tag("#iot", “#network”,"#data","#train")
train_labels = torch.tensor(y_train).tag("#iot", “#network”,"#target","#train")
test_inputs = torch.tensor(X_test.astype(np.float32)).tag("#iot", “#network”,"#data","#test")
test_labels = torch.tensor(y_test).tag("#iot", “#network”,"#target","#test"
but then it gives error ValueError: could not convert string to float: ‘Total Fwd Packets’ Total Fwd Packets is my first feature name. Any recommendation

I also followed other threads

train_inputs = torch.tensor(X_train.to_numpy()).tag("#iot", “#network”,"#data","#train")
but got this error

train_inputs = torch.tensor(X_train.to_numpy()).tag("#iot", “#network”,"#data","#train")

but got this error

train_inputs = torch.tensor(X_train.to_numpy()).tag("#iot", “#network”,"#data","#train") AttributeError: ‘numpy.ndarray’ object has no attribute ‘to_numpy’

numpy.object_ is often referring to a mixed data type used in numpy or a collection of arrays having a different shape, which is not supported in PyTorch.
Here is a small example:

a = np.array([np.random.randn(1), np.random.randn(1, 1)])
a
> array([[-0.8689455135513591],
         [0.6826695103629262]], dtype=object)

x = torch.from_numpy(a)
> TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

Make sure the numpy array contains values in the same dtype in the same shape.

1 Like