Pytorch中autograd.Variable使用过程中的问题

y.grad_fn.saved_variables
AttributeError                            Traceback (most recent call last)
 in 
      1 #使用retain_graph来保存这些buffer
----> 2 y.grad_fn.saved_Variables

AttributeError: 'MulBackward0' object has no attribute 'saved_Variables'

原因确实是版本问题,PyTorch0.3 中把许多python的操作转移到了C++中,saved_variables 现在是一个c++的对象,无法通过python访问。(https://github.com/chenyuntc/pytorch-book/issues/7)

解决方法:
省略上面那不操作,直接进行下一步

#使用retain_graph来保存buffer
z.backward(retain_graph=True)
w.grad
def f(x):
    result = 1
    for ii in x:
        if ii.data[0]>0: result = ii*result
    return result
x = V(t.arange(-2, 4), requires_grad = True)
y = f(x)
y.backward()
x.grad
RuntimeError: Only Tensors of floating point dtype can require gradients

解决方法:

x = V(t.arange(-2, 4).float(),requires_grad=True) #这里如果不使用.float()会报错
def f(x):
    result = 1
    for ii in x:
        if ii.data[0]>0: result = ii*result
    return result
x = V(t.arange(-2, 4).float(), requires_grad = True)
y = f(x)
y.backward()
x.grad
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

解决方法:

x = V(t.arange(-2, 4).float(),requires_grad=True) #这里如果不使用.float()会报错
问题:
x.volatile, w.volatile, y.volatile

x.volatile, w.volatile, y.volatile

 with torch.no_grad():
                        ^
SyntaxError: invalid character in identifier

解决方法:

#x.volatile, w.volatile, y.volatile
x = t.zeros(1, requires_grad=True)
with t.no_grad():
    y = x * 2
y.requires_grad
False

小试牛刀:用Variable实现线性回归
问题:

#随机初始化参数
w = V(t.rand(1, 1), requires_grad = True)
b =V(t.zeros(1, 1), requires_grad = True)

lr = 0.001#学习率

for ii in range(8000):
    x, y = get_fake_data()
    x, y = V(x), V(y)
    
    #forward:计算loss
    y_pred = x.mm(w) + b.expand_as(y)
    loss = 0.5*(y_pred - y)**2
    loss = loss.sum()
    
    #backward: 自动计算梯度
    loss.backward()
    
    #更新参数
    w.data.sub_(lr*w.grad.data)
    b.data.sub_(lr*b.grad.data)
    
    #梯度清零
    w.grad.data.zero_()
    b.grad.data.zero_()
    
    if ii%1000 ==0:
        #画图
        display.clear_output(wait=True)
        x = t.arange(0, 20).view(-1, 1)
        y = x.mm(w.data) + b.data.expand_as(x)
        plt.plot(x.numpy(), y.numpy())#predicted
        
        x2, y2 = get_fake_data(batch_size=20)
        plt.scatter(x2.numpy(), y2.numpy())#true data
        
        plt.xlim(0, 20)
        plt.xlim(0, 41)
        plt.show()#输出如图
        plt.pause(0.5)
        
print(w.data.squeeze()[0], b.data.squeeze()[0])
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'mat2'

解决方法:

 #画图
        display.clear_output(wait=True)
        x = t.arange(0, 20).float().view(-1, 1)
        y = x.mm(w.data) + b.data.expand_as(x)
        plt.plot(x.numpy(), y.numpy())#predicted
        
        x2, y2 = get_fake_data(batch_size=20)
        plt.scatter(x2.numpy(), y2.numpy())#true data
        
        plt.xlim(0, 20)
        plt.xlim(0, 41)
        plt.show()#输出如图
        plt.pause(0.5)
        
print(w.squeeze().item(), b.squeeze().item())

你可能感兴趣的:(PyTorch)