创建操作#
用于创建与可微分仿真兼容的 Genesis tensors 的函数。
概述#
在使用可微分仿真时,使用这些函数创建与梯度系统正确集成的 tensors。
创建 Tensors#
从 Python 值创建#
import genesis as gs
import torch
gs.init()
# 在正确的设备上创建 tensor
tensor = torch.tensor([1.0, 2.0, 3.0], device=gs.device, dtype=gs.tc_float)
# 启用梯度追踪
tensor = torch.tensor(
[1.0, 2.0, 3.0],
device=gs.device,
dtype=gs.tc_float,
requires_grad=True,
)
零/一 Tensors#
# 创建零 tensor
zeros = torch.zeros(10, device=gs.device, dtype=gs.tc_float)
# 创建一 tensor
ones = torch.ones(10, device=gs.device, dtype=gs.tc_float)
# 启用梯度追踪
zeros_grad = torch.zeros(10, device=gs.device, dtype=gs.tc_float, requires_grad=True)
随机 Tensors#
# 随机均匀分布 [0, 1)
rand = torch.rand(10, device=gs.device, dtype=gs.tc_float)
# 随机正态分布
randn = torch.randn(10, device=gs.device, dtype=gs.tc_float)
转换为 Genesis Tensors#
标准 PyTorch tensors 在与 scene state 结合时会变成 Genesis tensors:
# 标准 PyTorch tensor
external = torch.tensor([1.0, 2.0, 3.0], device=gs.device, requires_grad=True)
# 与 scene state 结合 -> Genesis tensor
pos = robot.get_pos()
combined = pos + external # 结果是 Genesis tensor
API 参考#
- genesis.grad.creation_ops.torch_op_wrapper(torch_op, *args, dtype=None, requires_grad=False, scene=None, **kwargs)[源代码]#
- genesis.grad.creation_ops.from_torch(torch_tensor, dtype=None, requires_grad=False, detach=True, scene=None)[源代码]#
By default, detach is True, meaning that this function returns a new leaf tensor which is not connected to torch_tensor’s computation gragh.
- genesis.grad.creation_ops.arange(*args, **kwargs)#
This is the genesis wrapper of torch.arange().
- genesis.grad.creation_ops.as_strided(*args, **kwargs)#
This is the genesis wrapper of torch.as_strided().
- genesis.grad.creation_ops.as_tensor(*args, **kwargs)#
This is the genesis wrapper of torch.as_tensor().
- genesis.grad.creation_ops.asarray(*args, **kwargs)#
This is the genesis wrapper of torch.asarray().
- genesis.grad.creation_ops.empty(*args, **kwargs)#
This is the genesis wrapper of torch.empty().
- genesis.grad.creation_ops.empty_like(*args, **kwargs)#
This is the genesis wrapper of torch.empty_like().
- genesis.grad.creation_ops.empty_strided(*args, **kwargs)#
This is the genesis wrapper of torch.empty_strided().
- genesis.grad.creation_ops.eye(*args, **kwargs)#
This is the genesis wrapper of torch.eye().
- genesis.grad.creation_ops.from_numpy(*args, **kwargs)#
This is the genesis wrapper of torch.from_numpy().
- genesis.grad.creation_ops.full(*args, **kwargs)#
This is the genesis wrapper of torch.full().
- genesis.grad.creation_ops.full_like(*args, **kwargs)#
This is the genesis wrapper of torch.full_like().
- genesis.grad.creation_ops.linspace(*args, **kwargs)#
This is the genesis wrapper of torch.linspace().
- genesis.grad.creation_ops.logspace(*args, **kwargs)#
This is the genesis wrapper of torch.logspace().
- genesis.grad.creation_ops.ones(*args, **kwargs)#
This is the genesis wrapper of torch.ones().
- genesis.grad.creation_ops.ones_like(*args, **kwargs)#
This is the genesis wrapper of torch.ones_like().
- genesis.grad.creation_ops.rand(*args, **kwargs)#
This is the genesis wrapper of torch.rand().
- genesis.grad.creation_ops.rand_like(*args, **kwargs)#
This is the genesis wrapper of torch.rand_like().
- genesis.grad.creation_ops.randint(*args, **kwargs)#
This is the genesis wrapper of torch.randint().
- genesis.grad.creation_ops.randint_like(*args, **kwargs)#
This is the genesis wrapper of torch.randint_like().
- genesis.grad.creation_ops.randn(*args, **kwargs)#
This is the genesis wrapper of torch.randn().
- genesis.grad.creation_ops.randn_like(*args, **kwargs)#
This is the genesis wrapper of torch.randn_like().
- genesis.grad.creation_ops.randperm(*args, **kwargs)#
This is the genesis wrapper of torch.randperm().
- genesis.grad.creation_ops.range(*args, **kwargs)#
This is the genesis wrapper of torch.range().
- genesis.grad.creation_ops.tensor(*args, **kwargs)#
This is the genesis wrapper of torch.tensor().
- genesis.grad.creation_ops.zeros(*args, **kwargs)#
This is the genesis wrapper of torch.zeros().
- genesis.grad.creation_ops.zeros_like(*args, **kwargs)#
This is the genesis wrapper of torch.zeros_like().