I'm trying to train a simple linear regression model with Pytorch and CUDA. PyTorch can detect CUDA, and there is GPU memory consumption. However, GPU is never used in my code. This is my code
class linearRegression(torch.nn.Module):
def __init__(self, inputSize, outputSize):
super(linearRegression, self).__init__()
self.linear = torch.nn.Linear(inputSize, outputSize)
def forward(self, x):
out = self.linear(x)
return out
def linear_demo():
# create dummy data for training
n_data = 1000000
x_values = n_data*np.random.normal(size=n_data)
x_train = np.array(x_values, dtype=np.float64)
x_train = x_train.reshape(-1, 1)
y_values = [2*i + 1 + (np.random.rand(1)-0.5)*5 for i in x_values]
y_train = np.array(y_values, dtype=np.float64)
y_train = y_train.reshape(-1, 1)
x_train /= np.abs(x_train).max()
y_train /= np.abs(y_train).max()
print(x_train.shape)
print(y_train.shape)
inputDim = 1 # takes variable 'x'
outputDim = 1 # takes variable 'y'
learningRate = 1e-2
epochs = 80
batch_size = 128*8
batch_size = n_data
model = linearRegression(inputDim, outputDim).to(torch.float64)
##### For GPU #######
if torch.cuda.is_available():
model.cuda()
criterion = torch.nn.MSELoss(reduction='mean').cuda()
optimizer = torch.optim.SGD(model.parameters(), lr=learningRate)
for epoch in range(epochs):
permutation = torch.randperm(n_data)
idx = permutation[:batch_size]
# Converting inputs and labels to Variable
if torch.cuda.is_available():
inputs = Variable(torch.from_numpy(x_train[idx]).cuda())
labels = Variable(torch.from_numpy(y_train[idx]).cuda())
else:
inputs = Variable(torch.from_numpy(x_train[idx]))
labels = Variable(torch.from_numpy(y_train[idx]))
# Clear gradient buffers because we don't want any gradient from the previous epoch to carry forward, don't want to accumulate gradients
optimizer.zero_grad()
# get output from the model, given the inputs
outputs = model(inputs)
# get loss for the predicted output
loss = criterion(outputs, labels)
if loss.item() > 1:
print(loss)
# get gradients w.r.t to parameters
loss.backward()
# update parameters
optimizer.step()
if (epoch % 10 == 0):
print('epoch {}, loss {}'.format(epoch, loss.item()))
return
This is a screenshot of GPU usage. Apparently, GPU is not used in computing. GPU can be detected.
If the computational burden is too low for GPU to display usage, I tried increasing n_data
to 1e8
, but the GPU usage is still 0%. How can I use GPU computing in this situation? If it's really due to low computational burden, is there any simple way to clearly show that GPU is working?
------update------
I tried top answer here, and the terminal returns the expected info. So I believe the installation is correct. screenshot for GPU info
I also tried device declaration like
inputs.to('cuda')
,inputs.to(torch.device('cuda'))
,inputs.cuda()
. They turn out to be equivalent.