t_obs = tensor([[ 3.9165e+00, -3.2818e-01, 3.9585e+00, -3.2877e-01, 2.5000e+00, 4.0000e+00, 2.9092e-04, -3.1356e-04, -5.0448e-02, -1.0185e-02, -2.6571e-01, 3.5682e-02, 3.2696e-01, 1.4884e-01, -2.9417e-01, 7.9636e+00, -2.0701e+00, -1.0490e+01, -5.4630e-01, 1.3817e+01, 9.7782e+00, 1.0266e-01, 7.3403e-02, 1.0498e-01]], device='cuda:0') 。actor: torch.nn.Module, t_obs = torch.FloatTensor((train_collector.data.obs)[0]).unsqueeze(0).to(args.device) t_act = torch.FloatTensor((train_collector.data.act)[0]).unsqueeze(0).to(args.device) print(t_obs) print(t_act) writer.add_graph(policy.actor, t_obs)出现RuntimeError: Only tensors, lists, tuples of tensors, or dictionary of tensors can be output from traced functions错误,怎么改正代码
时间: 2024-02-24 08:59:48 浏览: 124
这个错误通常是由于`add_graph`方法不能接受`torch.nn.Module`类型的参数,而应该是一个计算图(`torch.onnx.export`)或一组张量。要修复这个错误,可以使用`torch.jit.trace`函数来创建一个跟踪模块,然后将跟踪模块的输入张量作为参数传递给`add_graph`方法。
以下是可能的解决方案,可以将其添加到代码中尝试:
```
# 使用torch.jit.trace创建跟踪模块
traced_actor = torch.jit.trace(policy.actor, (t_obs,))
# 将输入张量传递给add_graph方法
writer.add_graph(traced_actor, (t_obs,))
```
这应该可以解决该错误,允许您将模型添加到TensorBoard中。
相关问题
t_obs = torch.FloatTensor((train_collector.data.obs)[0]).unsqueeze(0).to(args.device)作为输入 tensor([[ 3.8838e+00, 3.5745e+00, 3.8844e+00, 3.6165e+00, 2.5000e+00, 4.0000e+00, -2.8299e-03, 3.1723e-03, 1.4587e+00, -2.5189e-01, -2.4204e-01, 2.6674e-01, 4.6803e-01, 4.7351e-02, -4.0710e-01, 6.3241e+00, -8.7424e-01, -3.3930e+00, -8.1889e-01, 5.3452e+00, -1.8534e+00, 4.7878e-01, 1.4161e+00, 1.2834e-01]], device='cuda:0')导入writer.add_graph中的代码
`torch.FloatTensor((train_collector.data.obs)[0]).unsqueeze(0).to(args.device)`是将训练数据集中的第一个样本的 `obs` 数据转换为 `FloatTensor` 类型,并在第0维度上加了一个维度,最后将其移动到指定设备上(这里是 `cuda:0`)。这个张量可能是一个状态向量,用于输入到模型中进行推理或训练。
如果要将这个张量的计算图加到TensorBoard中,可以使用以下代码:
```python
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
dummy_input = torch.randn(1, 24).to('cuda:0') # 这里输入张量的形状需要和模型输入的形状一致
writer.add_graph(model, (dummy_input,))
```
其中,`model` 是你的模型,`dummy_input` 是一个随机输入张量,用于推导模型的计算图。最后,运行TensorBoard时,可以在Graphs选项卡中查看模型的计算图。
Batch( obs: array([[ 3.87556586e+00, -1.39884560e+00, 3.90417171e+00, -1.42959780e+00, 2.50000000e+00, 4.00000000e+00, 8.59177895e-03, -1.61742055e-03, -8.72117713e-01, 7.03924209e-02, -1.29531812e-01, -2.94461078e-01, 8.61572789e-02, 3.94930195e-01, -4.20615077e-02, -6.41838507e+00, -1.65869727e+00, 8.98895966e-01, -4.98185009e-01, 5.14246542e-01, 2.90281950e+00, 1.09837612e-01, 1.17493326e-01, 8.70688482e-02]]), act: array([[-0.46321275, 0.92014786, -0.97776801, 0.05547555]]), rew: array([-25.02164498]), terminated: array([False]), truncated: array([False]), done: array([False]), obs_next: array([[ 3.87556586e+00, -1.39884560e+00, 3.90417171e+00, -1.42959780e+00, 2.50000000e+00, 4.00000000e+00, 8.59177895e-03, -1.61742055e-03, -8.72117713e-01, 7.03924209e-02, -1.29531812e-01, -2.94461078e-01, 8.61572789e-02, 3.94930195e-01, -4.20615077e-02, -6.41838507e+00, -1.65869727e+00, 8.98895966e-01, -4.98185009e-01, 5.14246542e-01, 2.90281950e+00, 1.09837612e-01, 1.17493326e-01, 8.70688482e-02]]), info: Batch( self.do_simulation(action, self.frame_skip): array([[-0.49878631, -0.73343635, -0.18061225, 0.59941456, 0.81610918, 0.24985782]]), dist_htg_after: array([5.60822881]), reward_dev: array([-0.51599685]), rew_arrive: array([0]), reward_ctrl: array([-0.00575235]), env_id: array([0]), self.state_k: array([4000]), rew_near: array([-25]), rewards: array([-25.02164498]), rew_forward: array([0.50010422]), ), policy: Batch(), )对其切片采样,提取出obs的列表
可以使用以下代码提取出 Batch 对象中的 observations 列表:
```
obs_list = Batch.obs.tolist()
```
该代码将 Batch 对象中的 obs 转换为列表,并赋值给变量 obs_list。可以通过以下代码来验证提取出的 obs_list 是否正确:
```
print(obs_list)
```
输出结果应该为:
```
[[3.87556586, -1.3988456, 3.90417171, -1.4295978, 2.5, 4.0, 0.00859177895, -0.00161742055, -0.872117713, 0.0703924209, -0.129531812, -0.294461078, 0.0861572789, 0.394930195, -0.0420615077, -6.41838507, -1.65869727, 0.898895966, -0.498185009, 0.514246542, 2.9028195, 0.109837612, 0.117493326, 0.0870688482]]
```
可以看到,输出结果为一个包含所有 observations 的列表。
阅读全文