Bounded¶
- class torchrl.data.Bounded(*args, **kwargs)[source]¶
一種有界的張量規範。
Bounded規範本身不會直接出現,而是會根據其資料型別(dtype)被子類化為BoundedContinuous或BoundedDiscrete(浮點型別的資料型別將產生BoundedContinuous例項,所有其他型別將產生BoundedDiscrete例項)。- 引數:
low (np.ndarray, torch.Tensor 或 number) – 盒子的下界。
high (np.ndarray, torch.Tensor 或 number) – 盒子的上界。
shape (torch.Size) –
Bounded規範的形狀。必須指定形狀。輸入low、high和shape必須可廣播 (broadcastable)。device (str, int 或 torch.device, optional) – 張量的裝置。
dtype (str 或 torch.dtype, optional) – 張量的資料型別(dtype)。
domain (str) – “continuous” 或 “discrete”。可用於覆蓋自動型別分配。
示例
>>> spec = Bounded(low=-1, high=1, shape=(), dtype=torch.float) >>> spec BoundedContinuous( shape=torch.Size([]), space=ContinuousBox( low=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, contiguous=True), high=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, contiguous=True)), device=cpu, dtype=torch.float32, domain=continuous) >>> spec = Bounded(low=-1, high=1, shape=(), dtype=torch.int) >>> spec BoundedDiscrete( shape=torch.Size([]), space=ContinuousBox( low=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, contiguous=True), high=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, contiguous=True)), device=cpu, dtype=torch.int32, domain=discrete) >>> spec.to(torch.float) BoundedContinuous( shape=torch.Size([]), space=ContinuousBox( low=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, contiguous=True), high=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.float32, contiguous=True)), device=cpu, dtype=torch.float32, domain=continuous) >>> spec = Bounded(low=-1, high=1, shape=(), dtype=torch.int, domain="continuous") >>> spec BoundedContinuous( shape=torch.Size([]), space=ContinuousBox( low=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, contiguous=True), high=Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, contiguous=True)), device=cpu, dtype=torch.int32, domain=continuous)
- assert_is_in(value: Tensor) None¶
斷言張量是否屬於盒子,否則引發異常。
- 引數:
value (torch.Tensor) – 要檢查的值。
- contains(item: torch.Tensor | TensorDictBase) bool¶
如果值
val可以由TensorSpec生成,則返回True,否則返回False。有關更多資訊,請參閱
is_in()。
- cpu()¶
將 TensorSpec 轉換為 'cpu' 裝置。
- cuda(device=None)¶
將 TensorSpec 轉換為 'cuda' 裝置。
- encode(val: np.ndarray | torch.Tensor | TensorDictBase, *, ignore_device: bool = False) torch.Tensor | TensorDictBase¶
根據指定的規範編碼值,並返回相應的張量。
此方法用於那些返回可以輕鬆對映到 TorchRL 所需域的值(例如,numpy 陣列)的環境。如果該值已經是張量,則規範不會改變其值並按原樣返回。
- 引數:
val (np.ndarray 或 torch.Tensor) – 要編碼為張量的值。
- 關鍵字引數:
ignore_device (bool, optional) – 如果為
True,將忽略規範裝置。這用於在呼叫TensorDict(..., device="cuda")時對張量轉換進行分組,這樣更快。- 返回值:
與所需張量規範匹配的 torch.Tensor。
- expand(*shape)[source]¶
返回具有擴充套件形狀的新 Spec。
- 引數:
*shape (tuple 或 int 可迭代物件) – Spec 的新形狀。必須與當前形狀可廣播:其長度必須至少與當前形狀長度一樣長,並且其最後一個值也必須一致;即,只有當前維度是單例時,它們才能與當前形狀不同。
- classmethod implements_for_spec(torch_function: Callable) Callable¶
為 TensorSpec 註冊 torch 函式重寫。
- index(index: INDEX_TYPING, tensor_to_index: torch.Tensor | TensorDictBase) torch.Tensor | TensorDictBase[source]¶
對輸入張量進行索引。
此方法用於編碼一個或多個類別變數(例如
OneHot或Categorical)的規範,這樣就可以在使用樣本對張量進行索引時,不必關心索引的實際表示形式。- 引數:
index (int, torch.Tensor, slice 或 list) – 張量的索引
tensor_to_index – 要索引的張量
- 返回值:
索引後的張量
- 示例
>>> from torchrl.data import OneHot >>> import torch >>> >>> one_hot = OneHot(n=100) >>> categ = one_hot.to_categorical_spec() >>> idx_one_hot = torch.zeros((100,), dtype=torch.bool) >>> idx_one_hot[50] = 1 >>> print(one_hot.index(idx_one_hot, torch.arange(100))) tensor(50) >>> idx_categ = one_hot.to_categorical(idx_one_hot) >>> print(categ.index(idx_categ, torch.arange(100))) tensor(50)
- is_in(val: Tensor) bool[source]¶
如果值
val可以由TensorSpec生成,則返回True,否則返回False。更準確地說,
is_in方法檢查值val是否在由space屬性(即盒子)定義的限制範圍內,並且檢查dtype、device、shape以及可能的其他元資料是否與規範的匹配。如果任何檢查失敗,is_in方法將返回False。- 引數:
val (torch.Tensor) – 要檢查的值。
- 返回值:
布林值,指示值是否屬於 TensorSpec 盒子。
- make_neg_dim(dim: int) T¶
將特定維度轉換為
-1。
- property ndim: int¶
規範形狀的維度數量。
len(spec.shape)的快捷方式。
- ndimension() int¶
規範形狀的維度數量。
len(spec.shape)的快捷方式。
- one(shape: torch.Size = None) torch.Tensor | TensorDictBase¶
返回盒子裡填充了 1 的張量。
注意
即使不能保證
1屬於規範域,當違反此條件時,此方法也不會引發異常。one的主要用例是生成空資料緩衝區,而不是有意義的資料。- 引數:
shape (torch.Size) – 填充 1 的張量的形狀
- 返回值:
在 TensorSpec 盒子中取樣的填充了 1 的張量。
- ones(shape: torch.Size = None) torch.Tensor | TensorDictBase¶
one()的代理。
- project(val: torch.Tensor | TensorDictBase) torch.Tensor | TensorDictBase¶
如果輸入張量不在 TensorSpec 盒子中,它會根據一些定義的啟發式方法將其映射回盒子。
- 引數:
val (torch.Tensor) – 要對映到盒子的張量。
- 返回值:
屬於 TensorSpec 盒子的 torch.Tensor。
- rand(shape: Optional[Size] = None) Tensor[source]¶
在由規範定義的空間中返回一個隨機張量。
取樣將在空間上均勻進行,除非盒子是無界的,在這種情況下將抽取正態分佈的值。
- 引數:
shape (torch.Size) – 隨機張量的形狀
- 返回值:
在 TensorSpec 盒子中取樣的隨機張量。
- sample(shape: torch.Size = None) torch.Tensor | TensorDictBase¶
在由規範定義的空間中返回一個隨機張量。
有關詳細資訊,請參閱
rand()。
- squeeze(dim: int | None = None)[source]¶
返回一個新 Spec,其中所有大小為
1的維度都被移除。當給定
dim時,僅在該維度執行 squeeze 操作。- 引數:
dim (int 或 None) – 應用 squeeze 操作的維度
- to(dest: Union[dtype, device, str, int]) Bounded[source]¶
將 TensorSpec 轉換為裝置或資料型別(dtype)。
如果未做更改,則返回相同的規範。
- to_numpy(val: torch.Tensor | TensorDictBase, safe: bool = None) np.ndarray | dict¶
返回輸入張量對應的
np.ndarray。這旨在成為
encode()的逆操作。- 引數:
val (torch.Tensor) – 要轉換為 numpy 的張量。
safe (bool) – 布林值,指示是否應針對規範域對值執行檢查。預設為
CHECK_SPEC_ENCODE環境變數的值。
- 返回值:
一個 np.ndarray。
- type_check(value: Tensor, key: Optional[NestedKey] = None) None¶
檢查輸入值的資料型別
dtype是否與TensorSpec的dtype匹配,如果不匹配則引發異常。- 引數:
value (torch.Tensor) – 需要檢查資料型別(dtype)的張量。
key (str, optional) – 如果 TensorSpec 有鍵,將根據指定的鍵指向的規範檢查值的資料型別(dtype)。
- unflatten(dim: int, sizes: Tuple[int]) T¶
將一個
TensorSpec展平。有關此方法的更多資訊,請查閱
unflatten()。
- unsqueeze(dim: int)[source]¶
返回一個新的 Spec,增加一個單例維度(在由
dim指示的位置)。- 引數:
dim (int or None) – 應用 unsqueeze 操作的維度。
- zero(shape: torch.Size = None) torch.Tensor | TensorDictBase¶
返回在此(規格定義的)範圍內的一個全零張量。
注意
儘管不能保證
0屬於規格域,但當違反此條件時,此方法不會引發異常。zero的主要用例是生成空資料緩衝區,而不是有意義的資料。- 引數:
shape (torch.Size) – 全零張量的形狀
- 返回值:
在此 TensorSpec 範圍內生成的一個全零張量。
- zeros(shape: torch.Size = None) torch.Tensor | TensorDictBase¶
zero()的別名。