I'd like to get an index of the tensor array, but it doesn't work, so please let me know. I'd like to extract only the following shape and the maximum element 1 is present.
tensor([[1., 0., 0., 0., 0.], → 0 in this case.
[1., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0.],
...,
[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.]]) → 2 in this case
Code
import numpy as np
import torch
npzfile=np.load("C:/Users/namae/Desktop/myo-python-1.0.4/myo-armband-nn-master/data/train_set.npz")
x = npzfile ['x']
y=npzfile['y']
x = x.astype (np.float32)
x=torch.from_numpy(x).clone()
x = torch.tensor(x)
x = x.clone().detach()
y = y.astype(np.float32)
y=torch.from_numpy(y).clone()
print(y)
x=torch.tensor([[1., 0, 0, 0, 0, 0.],
[1., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.]])
x.max(dim=1)[1]
# US>tensor([0,0,0,2,2,2])
Use argmax()
.
x=torch.tensor([[1., 0, 0, 0, 0, 0.],
[1., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.]])
x.argmax(1)
# US>tensor([0,0,0,2,2,2])
© 2024 OneMinuteCode. All rights reserved.