Only Tensors of floating point and complex dtype can require gradients问题解决方案

简介: Only Tensors of floating point and complex dtype can require gradients问题解决方案

一、问题原因

二、解决方案,改成float类型即可,运行结果没问题。

import torch as t
from torch.autograd import Variable as V
x = V(t.arange(0,3).float(),requires_grad=True) # 将inttensor改成floattensor
y = x**2 + x*2
y.backward(t.ones(y.size()))


相关文章
|
2月前
|
TensorFlow 算法框架/工具 Python
成功解决but is 0 and 2 (computed from start 0 and end 9223372 over shape with rank 2 and stride-1)
成功解决but is 0 and 2 (computed from start 0 and end 9223372 over shape with rank 2 and stride-1)
|
Python
Python内置函数--getattr&setattr&delattr&hasattr
Python内置函数--getattr&setattr&delattr&hasattr
93 0
|
并行计算 Python
TypeError: can‘t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory
运行程序,出现报错信息 TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.。
357 0
|
PyTorch 算法框架/工具 开发者
RuntimeError: Can‘t call numpy() on Variable that requires grad. Use var.detach().numpy()
出现这个现象的原因是:待转换类型的PyTorch Tensor变量带有梯度,直接将其转换为numpy数据将破坏计算图,因此numpy拒绝进行数据转换,实际上这是对开发者的一种提醒。如果自己在转换数据时不需要保留梯度信息,可以在变量转换之前添加detach()调用。
241 0
|
机器学习/深度学习 PyTorch 算法框架/工具
解决Pytorch中RuntimeError: expected scalar type Double but found Float
解决Pytorch中RuntimeError: expected scalar type Double but found Float
2841 0
|
PyTorch 算法框架/工具
pytorch报错 RuntimeError: The size of tensor a (25) must match the size of tensor b (50) at non-singleton dimension 1 怎么解决?
这个错误提示表明,在进行某个操作时,张量a和b在第1个非单例维(即除了1以外的维度)上的大小不一致。例如,如果a是一个形状为(5, 5)的张量,而b是一个形状为(5, 10)的张量,则在第二个维度上的大小不匹配。
4736 0
TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('M8[ns]')
TypeError: ufunc subtract cannot use operands with types dtype('O') and dtype('M8[ns]')
|
Python