반응형
pytorch에는 onehot encoder를 도와주는 scatter라는 함수가있다.
t = torch.randint(0,9,(5,))
print(t)
#y = torch.zeros((batch_size, classes))
y = torch.zeros((5,10))
y[range(len(t)), t] = 1
print(y)
----->>>
tensor([6, 1, 4, 6, 8])
tensor([[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]])
반응형
'개발' 카테고리의 다른 글
RabbitMQ 자세히 알아보자 (0) | 2023.08.02 |
---|