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

I am trying to impement this code https://github.com/tuhinsharma121/federated-ml/blob/master/notebooks/network-threat-detection-using-federated-learning.ipynb

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.

5 Likes

How can we convert this array of type objects to something that can be used in from_numpy() method

You won’t be able to directly convert it as the object type contains arbitrary or mixed data.
Transform your numpy object to an array first and call torch.from_numpy afterwards.