今天我们利用pytorch对二维数据进行求导并输出显示,主要用到pytorch中的Linear()、MSELoss()等函数,具体的求导过程详见下面代码:
import torch
#create tensors of shape(10,3)and (10,2)
x=torch.randn(10,3)
y=torch.randn(10,2)
#build a fully connected layer
linear=nn.Linear(3,2)
print('w:',linear.weight)
print('b:',linear.bias)
#build loss function and optimizer
criterion=nn.MSELoss()
optimizer=torch.optim.SGD(linear.parameters(),lr=0.01)
#forward pass
pred=linear(x)
#compute loss
loss=criterion(pred,y)
print('loss:',loss.item())
#backward pass
loss.backward()
#print out the gradients
print('dl/dw:',linear.weight.grad)
print('dl/db',linear.bias.grad)
#1-step gradient descent
optimizer.step()
输出结果显示:
另外一种方案:在1-step之后输出loss
import torch
#create tensors of shape(10,3)and (10,2)
x=torch.randn(10,3)
y=torch.randn(10,2)
#build a fully connected layer
linear=nn.Linear(3,2)
print('w:',linear.weight)
print('b:',linear.bias)
#build loss function and optimizer
criterion=nn.MSELoss()
optimizer=torch.optim.SGD(linear.parameters(),lr=0.01)
# #forward pass
# pred=linear(x)
#
# #compute loss
# loss=criterion(pred,y)
# print('loss:',loss.item())
#
# #backward pass
# loss.backward()
#
# #print out the gradients
# print('dl/dw:',linear.weight.grad)
# print('dl/db',linear.bias.grad)
#1-step gradient descent
optimizer.step()
pred=linear(x)
loss=criterion(pred,y)
print('loss after 1 step optimization:',loss.item())
输出结果显示:
对比: