I’m a starter learning pytorch based on python 3.7. As I tried to implete ‘fizzbuzz’ game(details about this little game is in the code) using nn, I found the loss decreased quite slowly. And I also noticed that the dimension of y_pre and batchY is different. Is this the reason why the model seems to be unefficient? How can I change my code?
### FuzzBuzz game. If the given number can be divided by 15, print 'fizzbuzz'; 5, print 'buzz'; 3, print 'fizz', else print itself
def fizz_buzz_encode(i):
if i % 15 == 0: return 0
elif i % 5 == 0: return 1
elif i % 3 == 0: return 2
else: return 3
def fizz_buzz_decode(i,prediction):
return ['fizzbuzz','buzz','fizz',i][prediction]
for i in range(50):
print(fizz_buzz_decode(i,fizz_buzz_encode(i)))
### impletment using nn
import numpy as np
import torch
NUM_DIGITS = 10
def binary_encode(i,num_digits):
return np.array([i>>d&1 for d in range(num_digits)][::-1])
trX = torch.Tensor([binary_encode(i,NUM_DIGITS) for i in range(101,2**NUM_DIGITS)])
trY = torch.LongTensor([fizz_buzz_encode(i) for i in range(101,2**NUM_DIGITS)])
NUM_HIDDEN = 100
model = torch.nn.Sequential(torch.nn.Linear(NUM_DIGITS,NUM_HIDDEN),
torch.nn.ReLU(),
torch.nn.Linear(NUM_HIDDEN,4))
device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model=model.to(device)
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(),lr = 0.01)
BATCH_SIZE = 256
for epoch in range(10000): # 进行10000次epoch
for start in range(0,len(trX),BATCH_SIZE):
end = start + BATCH_SIZE # 每隔128个数分段
batchX =trX[start:end].to(device)
batchY =trY[start:end].to(device)
y_pre = model(batchX)
loss = loss_fn(y_pre,batchY)
print('Epoch',epoch,loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
The output are as flow:
Epoch 0 1.33250093460083
Epoch 0 1.3155981302261353
Epoch 0 1.3044551610946655
Epoch 0 1.2906633615493774
Epoch 1 1.3085103034973145
Epoch 1 1.290393352508545
Epoch 1 1.2784980535507202
Epoch 1 1.2634493112564087
Epoch 2 1.2881089448928833
Epoch 2 1.2691847085952759
Epoch 2 1.256813645362854
Epoch 2 1.2410774230957031
…
Epoch 9998 0.3821529746055603
Epoch 9998 0.38773271441459656
Epoch 9998 0.44764772057533264
Epoch 9998 0.35775092244148254
Epoch 9999 0.38201063871383667
Epoch 9999 0.3875814378261566
Epoch 9999 0.4476431608200073
Epoch 9999 0.357889324426651
I also get confused that my CPU utilization is 100% but my GPU’s is 0, but I already transfered the whole process to cuda. What’s wrong with my code? Thanks a lot.