- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这段代码的构建如下:我的机器人拍照,一些 tf 计算机视觉模型计算目标对象在图片中的哪个位置开始。此信息(x1 和 x2 坐标)被传递给 pytorch 模型。它应该学会预测正确的运动激活,以便更接近目标。运动执行后,机器人再次拍照,tf cv 模型应计算电机激活是否使机器人更接近所需状态(x1 在 10,x2 坐标在 at31)
但是,每次我运行代码 pytorch 都无法计算梯度。
我想知道这是一些数据类型问题还是更普遍的问题:如果不直接从 pytorch 网络的输出计算损失,是否无法计算梯度?
任何帮助和建议将不胜感激。
#define policy model (model to learn a policy for my robot)
import torch
import torch.nn as nn
import torch.nn.functional as F
class policy_gradient_model(nn.Module):
def __init__(self):
super(policy_gradient_model, self).__init__()
self.fc0 = nn.Linear(2, 2)
self.fc1 = nn.Linear(2, 32)
self.fc2 = nn.Linear(32, 64)
self.fc3 = nn.Linear(64,32)
self.fc4 = nn.Linear(32,32)
self.fc5 = nn.Linear(32, 2)
def forward(self,x):
x = self.fc0(x)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
x = F.relu(self.fc5(x))
return x
policy_model = policy_gradient_model().double()
print(policy_model)
optimizer = torch.optim.AdamW(policy_model.parameters(), lr=0.005, betas=(0.9,0.999), eps=1e-08, weight_decay=0.01, amsgrad=False)
#make robot move as predicted by pytorch network (not all code included)
def move(motor_controls):
#define curvature
# motor_controls[0] = sigmoid(motor_controls[0])
activation_left = 1+(motor_controls[0])*99
activation_right = 1+(1- motor_controls[0])*99
print("activation left:", activation_left, ". activation right:",activation_right, ". time:", motor_controls[1]*100)
#start movement
#main
import cv2
import numpy as np
import time
from torch.autograd import Variable
print("start training")
losses=[]
losses_end_of_epoch=[]
number_of_steps_each_epoch=[]
loss_function = nn.MSELoss(reduction='mean')
#each epoch
for epoch in range(2):
count=0
target_reached=False
while target_reached==False:
print("epoch: ", epoch, ". step:", count)
###process and take picture
indices = process_picture()
###binary_network(sliced)=indices as input for policy model
optimizer.zero_grad()
###output: 1 for curvature, 1 for duration of movement
motor_controls = policy_model(Variable(torch.from_numpy(indices))).detach().numpy()
print("NO TANH output for motor: 1)activation left, 2)time ", motor_controls)
motor_controls[0] = np.tanh(motor_controls[0])
motor_controls[1] = np.tanh(motor_controls[1])
print("TANH output for motor: 1)activation left, 2)time ", motor_controls)
###execute suggested action
move(motor_controls)
###take and process picture2 (after movement)
indices = (process_picture())
###loss=(binary_network(picture2) - desired
print("calculate loss")
print("idx", indices, type(torch.tensor(indices)))
# loss = 0
# loss = (indices[0]-10)**2+(indices[1]-31)**2
# loss = loss/2
print("shape of indices", indices.shape)
array=np.zeros((1,2))
array[0]=indices
print(array.shape, type(array))
array2 = torch.ones([1,2])
loss = loss_function(torch.tensor(array).double(), torch.tensor([[10.0,31.0]]).double()).float()
print("loss: ", loss, type(loss), loss.shape)
# array2[0] = loss_function(torch.tensor(array).double(),
torch.tensor([[10.0,31.0]]).double()).float()
losses.append(loss)
#start line causing the error-message (still part of main)
###calculate gradients
loss.backward()
#end line causing the error-message (still part of main)
###apply gradients
optimizer.step()
#Output (so far as intented) (not all included)
#calculate loss
idx [14. 15.] <class 'torch.Tensor'>
shape of indices (2,)
(1, 2) <class 'numpy.ndarray'>
loss: tensor(136.) <class 'torch.Tensor'> torch.Size([])
#Error Message:
Traceback (most recent call last):
File "/home/pi/Desktop/GradientPolicyLearning/PolicyModel.py", line 259, in <module>
array2.backward()
File "/home/pi/.local/lib/python3.7/site-packages/torch/tensor.py", line 134, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/home/pi/.local/lib/python3.7/site-packages/torch/autograd/__init__.py", line 99, in
backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
最佳答案
如果您调用.detach()
在预测中,这将删除梯度。由于您首先从模型中获取索引,然后尝试反向支持错误,因此我建议
prediction = policy_model(torch.from_numpy(indices))
motor_controls = prediction.clone().detach().numpy()
loss = loss_function(prediction, torch.tensor([[10.0,31.0]]).double()).float()
关于python - Pytorch 运行时错误 : element 0 of tensors does not require grad and does not have a grad_fn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61808965/
我试图将迁移学习应用于 InceptionV3。这是我的代码: inception_model = InceptionV3(weights='imagenet',include_top=False)
我正在尝试展示 GAN 网络在某些指定时期的结果。打印当前结果的功能以前与 TF 一起使用。我需要换成 pytorch。 def show_result(G_net, z_, num_epoch, s
我对孪生神经网络还很陌生,最近发现了 this example和 Colab notebook . 运行代码时出现以下错误: IndexError: invalid index of a 0-dim
我正在尝试使用在此 PR 中添加的“高级”、numpy 样式的切片,但是我遇到了 same issue as the user here : ValueError: Shape must be ran
我想在 TensorFlow 中做类似这段 Numpy 代码的事情: a = np.zeros([5, 2]) idx = np.random.randint(0, 2, (5,)) row_idx
我有以下特征张量: Eigen::Tensor m(3,10,10); 我想访问第一个矩阵。在 numpy 中我会这样做 m(0,:,:) 我如何在 Eigen 中做到这一点 最佳答案 您可以使用 .
1、问题 模型训练完后进行测试,报错 RuntimeError: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU
我正在对 TFRecords 进行配对,它为我提供了一个标签作为数值。但是我需要在读取原始记录时将此值转换为分类向量。我怎样才能做到这一点。这是读取原型(prototype)记录的代码片段: def
我正在对 TFRecords 进行配对,它为我提供了一个标签作为数值。但是我需要在读取原始记录时将此值转换为分类向量。我怎样才能做到这一点。这是读取原型(prototype)记录的代码片段: def
我应该如何从 Eigen::Tensor 创建一个 tensorflow::Tensor?我可以一个接一个地复制元素,但我希望有更好的方法。 最佳答案 没有公共(public) api 可以在不复制数
我正在尝试使用 Tensorflow(版本 0.9.0)以与 beginner's tutorial 非常相似的方式训练一个简单的二元逻辑回归分类器。并且在拟合模型时遇到以下错误: ValueErro
从 0.4.0 版本开始,可以使用 torch.tensor 和 torch.Tensor 有什么区别?提供这两个非常相似且令人困惑的替代方案的原因是什么? 最佳答案 在 PyTorch 中,torc
PyTorch0.4中,.data 仍保留,但建议使用 .detach(), 区别在于 .data 返回和 x 的相同数据 tensor, 但不会加入到x的计算历史里,且require s_grad
我有一个参差不齐的张量,在尝试创建模型并使用 model.fit() 时,出现错误:TypeError: Failed to convert object of type to Tensor. Co
我必须用生成器和判别器训练一个 GAN 网络。我的发电机网络如下。 def Generator(image_shape=(512,512,3): inputs = Input(image_shap
我正在使用 Flask 运行 Web 服务器,当我尝试使用 vgg16 时出现错误,vgg16 是 keras 的预训练 VGG16 模型的全局变量。我不知道为什么会出现这个错误,也不知道它是否与 T
我正在使用 keras 的预训练模型,并且在调用 ResNet50(weights='imagenet') 时出现错误。 我在 flask 服务器中有以下代码: def getVGG16Predict
执行以下代码时出现以下错误。 rnn.rnn() 返回张量列表。错误在 convert_to_tensor 行。 TypeError: List of Tensors when single Tens
我有一个fruit_train_net.py 文件,其中包含以下代码 import tensorflow as tf import numpy as np import time import os
我们可以使用 torch.Tensor([1., 2.], device='cuda') 在 GPU 上分配张量.使用这种方式而不是torch.cuda.Tensor([1., 2.])有什么不同吗?
我是一名优秀的程序员,十分优秀!