Hakim2000
Hakim2000

Reputation: 31

How I make pytorch read the numpy format?

I'm trying to create a code with PyTorch and Keras that uses the BERT algorithm to detect fake news, but I got an error tells me:

can't convert np.ndarray of type numpy.bool_.
The only supported types are: double, float, float16, int64, int32, and uint8.

Please access the code on my Google Codelab. The error can be seen in the last cell.

The only requirement for running it is downloading a CSV file for the training process.

Upvotes: 0

Views: 81

Answers (3)

Graylien
Graylien

Reputation: 134

I can't confirm but I believe your problem will be solved by changing:

train_y = np.array(train_labels) == 'fake'
test_y = np.array(test_labels) == 'fake'

to:

train_y = (np.array(train_labels) == 'fake').astype(int)
test_y = (np.array(test_labels) == 'fake').astype(int)

The train_y data is currently an array of type Bool (True or False) and the tensor needs and int (0 or 1).

Upvotes: 3

Toyo
Toyo

Reputation: 751

You should convert first from bool to uint8 and then use torch.from_numpy() to get train_y as a torch's Tensor.

Upvotes: 0

Xhuliano Brace
Xhuliano Brace

Reputation: 119

Check the dtype of the numpy array.

I suspect you have to convert this to Change it's dtype

torch.from_numpy(np.array(someVar, dtype=np.uint8))

Upvotes: 1

Related Questions