问题描述
我目前正在尝试通过使用Pytorch上的神经网络来预测串行机器人的偏移量。方法如下:
- 随机采样一个joint_angles的向量。
- 使用神经网络来预测相应的偏移量。
- 计算FK(joint_angles + offsets)以获得与估计的关节状态相对应的估计的effector_position。
- 使用关节角度移动机器人并测量real_effector_position,其中包含有关真实偏移的信息
- 计算MSE(real_effector_position,estimated_effector_position),然后计算反向传播错误。 网络应输出偏移量,以最大程度地减少训练中的误差。
我正式尝试解决以下优化问题: arg min(offsets)|| FK(joint_angles + offsets)-real_effector_position ||
我正在使用pytroch来实现此目的,但网络无法学习,并且损失保持不变。这是我的代码
robot = BraccioRobot()
loss = 0
model = OffsetMLP(3,64)
opti = optim.Adam(net.parameters(),lr=3e-4)
for i in range(10000):
desired_joints_state = torch.FloatTensor(np.random.randint(0,180,(10,3))) # Get joint_angles
offset = model(desired_joints_state/180) * 15 # Predict offset
angles = desired_joints_state + offset # Effective angles
x,y = robot.forward_kinematic(((angles-90)*np.pi)/180) # Compute FK with offsets
x_t,y_t = robot.offset_forward_kinematic(((desired_joints_state-90)*np.pi)/180) # Real joints_states
loss = F.mse_loss(x,x_t) + F.mse_loss(y,y_t) # Compute loss
print(loss)
opti.zero_grad()
loss.backward()
opti.step()
机器人课程:
class BraccioRobot():
def __init__(self,) :
self.proportions = np.array([0.17,0.29,0.285,0.25])
self.joints_offsets = np.array([10,9,8]) * (np.pi/180.0)
def forward_kinematic(self,joint_states):
# Compute standard FK
parent_angle = 0
x = torch.zeros(joint_states.shape[0])
y = torch.zeros(joint_states.shape[0])
for i in range(joint_states.shape[1]):
x += self.proportions[i+1] * torch.sin(joint_states[:,i] + parent_angle)
y += self.proportions[i+1] * torch.cos(joint_states[:,i] + parent_angle)
parent_angle += joint_states[:,i]
y += self.proportions[0]
return x,y # effector position
def offset_forward_kinematic(self,joint_states):
# Compute FK with added offsets
parent_angle = 0
x = torch.zeros(joint_states.shape[0])
y = torch.zeros(joint_states.shape[0])
for i in range(joint_states.shape[1]):
x += self.proportions[i+1] * torch.sin(joint_states[:,i] + parent_angle + self.joints_offsets[i])
y += self.proportions[i+1] * torch.cos(joint_states[:,i] + parent_angle + self.joints_offsets[i])
parent_angle += joint_states[:,y # effector position
和模型:
class OffsetMLP(nn.Module):
def __init__(self,n_joints,n_hidden):
super(OffsetMLP,self).__init__()
self.fc1 = nn.Linear(n_joints,n_hidden)
self.fc2 = nn.Linear(n_hidden,n_joints)
self.outputAct = nn.Tanh()
def forward(self,desired_joints_state):
offset = self.fc1(desired_joints_state)
offset = F.relu(offset)
offset = self.fc2(offset)
offset = self.outputAct(offset)
return offset
offset_forward_kinematic函数用于模拟真实的机器人运动。它只是添加了网络必须学习的偏移量。在这种情况下,它是3个简单常数。我猜该错误是在forward_kinematic函数中的某个地方,渐变可能未按预期传播,但我似乎找不到解决方法。预先感谢您进行任何修复。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)