One-hot kodlanmış etiketler oluşturma
One-hot kodlama, tek bir tamsayı etiketi, N elemanlı bir vektöre dönüştürür; burada N, sınıf sayısıdır. Bu vektör, doğru pozisyonda bir tane bir ve geri kalan yerlerde sıfırlar içerir.
Bu egzersizde, önce y için one-hot kodlanmış bir vektörü elle oluşturacak, ardından süreci basitleştirmek için PyTorch kullanacaksın. Veri kümen üç sınıfa sahip (0, 1, 2).
numpy (np), torch.nn.functional (F) ve torch senin için zaten içe aktarıldı.
Bu egzersiz
PyTorch ile Deep Learning'e Giriş
kursunun bir parçasıdırEgzersiz talimatları
- Verilen NumPy dizisini kullanarak gerçek etiket
yiçin one-hot kodlamayı elle yap veone_hot_numpyolarak kaydet. - PyTorch kullanarak
yiçin one-hot kodlama yap veone_hot_pytorcholarak kaydet.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
y = 1
num_classes = 3
# Create the one-hot encoded vector using NumPy
one_hot_numpy = np.array([____, ____, ____])
# Create the one-hot encoded vector using PyTorch
one_hot_pytorch = F.____(torch.tensor(y), num_classes=____)
print("One-hot vector using NumPy:", one_hot_numpy)
print("One-hot vector using PyTorch:", one_hot_pytorch)