有界限¶
- class torchrl.data.Bounded(*args, **kwargs)[來源]¶
有界限的張量規格。
Bounded
規格永遠不會以這種形式出現,並且總是會被子類化為BoundedContinuous
或BoundedDiscrete
,具體取決於它們的 dtype(浮點 dtype 將產生BoundedContinuous
實例,所有其他 dtype 則產生BoundedDiscrete
實例)。- 參數:
low (np.ndarray, torch.Tensor 或 數字) – 方框的下限。
high (np.ndarray, torch.Tensor 或 數字) – 方框的上限。
shape (torch.Size) –
Bounded
規格的形狀。必須指定形狀。輸入low
、high
和shape
必須是可廣播的。device (str, int 或 torch.device, 選用) – 張量的裝置。
dtype (str 或 torch.dtype, 選用) – 張量的 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 | tensordict.base.TensorDictBase) bool ¶
如果值
val
可能是由TensorSpec
所產生,則回傳True
,否則回傳False
。請參閱
is_in()
以取得更多資訊。
- cpu()¶
將 TensorSpec 轉換到 'cpu' 裝置。
- cuda(device=None)¶
將 TensorSpec 轉換到 'cuda' 裝置。
- encode(val: numpy.ndarray | torch.Tensor | tensordict.base.TensorDictBase, *, ignore_device: bool = False) torch.Tensor | tensordict.base.TensorDictBase ¶
根據指定的 spec 編碼一個值,並回傳對應的 tensor。
此方法用於環境中,該環境回傳一個可以輕鬆對應到 TorchRL 所需網域的值(例如,一個 numpy array)。如果該值已經是一個 tensor,則 spec 不會改變其值並將其原樣回傳。
- 參數:
val (np.ndarray 或 torch.Tensor) – 要編碼為 tensor 的值。
- 關鍵字參數:
ignore_device (bool, optional) – 如果
True
,則將忽略 spec 裝置。這用於將 tensor 轉換分組在TensorDict(..., device="cuda")
的呼叫中,這樣會更快。- 回傳:
與所需 tensor specs 相符的 torch.Tensor。
- expand(*shape)[來源]¶
回傳一個具有展開形狀的新 Spec。
- 參數:
*shape (tuple 或 int 的 iterable) – Spec 的新形狀。必須可以與目前的形狀進行廣播:它的長度必須至少與目前形狀的長度一樣長,並且其最後的值也必須相符;也就是說,只有在目前的維度是單例時,它們才能與之不同。
- classmethod implements_for_spec(torch_function: Callable) Callable ¶
為 TensorSpec 註冊一個 torch 函數覆寫。
- abstract index(index: Union[int, Tensor, ndarray, slice, List], tensor_to_index: torch.Tensor | tensordict.base.TensorDictBase) torch.Tensor | tensordict.base.TensorDictBase ¶
為輸入 tensor 建立索引。
- 參數:
index (int, torch.Tensor, slice 或 list) – 張量的索引
tensor_to_index – 要建立索引的張量
- 回傳:
已建立索引的張量
- is_in(val: Tensor) bool [原始碼]¶
如果值
val
可能是由TensorSpec
所產生,則回傳True
,否則回傳False
。更精確地說,
is_in
方法會檢查值val
是否在space
屬性(邊界框)定義的範圍內,以及dtype
、device
、shape
這些可能的中繼資料是否與 spec 的中繼資料相符。 如果這些檢查中的任何一項失敗,is_in
方法將傳回False
。- 參數:
val (torch.Tensor) – 要檢查的值。
- 回傳:
布林值,指示值是否屬於 TensorSpec 邊界框。
- make_neg_dim(dim: int) T ¶
將特定維度轉換為
-1
。
- property ndim: int¶
Spec 形狀的維度數量。
len(spec.shape)
的捷徑。
- ndimension() int ¶
Spec 形狀的維度數量。
len(spec.shape)
的捷徑。
- one(shape: Optional[Size] = None) torch.Tensor | tensordict.base.TensorDictBase ¶
傳回邊界框中填滿 1 的張量。
注意
即使無法保證
1
屬於 spec 網域,當違反此條件時,此方法也不會引發例外狀況。one
的主要用途是產生空的資料緩衝區,而不是有意義的資料。- 參數:
shape (torch.Size) – one-tensor 的形狀
- 回傳:
在 TensorSpec 邊界框中採樣的填滿 1 的張量。
- ones(shape: Optional[Size] = None) torch.Tensor | tensordict.base.TensorDictBase ¶
one()
的 Proxy。
- project(val: torch.Tensor | tensordict.base.TensorDictBase) torch.Tensor | tensordict.base.TensorDictBase ¶
如果輸入張量不在 TensorSpec 邊界框中,它會根據某些已定義的啟發式方法將其對應回邊界框。
- 參數:
val (torch.Tensor) – 要對應到邊界框的張量。
- 回傳:
屬於 TensorSpec 邊界框的 torch.Tensor。
- rand(shape: Optional[Size] = None) Tensor [原始碼]¶
回傳一個由 spec 定義的空間中的隨機張量。
除非 box 是無界的,在這種情況下會提取常態值,否則抽樣將在空間上均勻完成。
- 參數:
shape (torch.Size) – 隨機張量的形狀
- 回傳:
在 TensorSpec box 中抽樣的隨機張量。
- sample(shape: Optional[Size] = None) torch.Tensor | tensordict.base.TensorDictBase ¶
回傳一個由 spec 定義的空間中的隨機張量。
請參閱
rand()
以取得更多細節。
- squeeze(dim: Optional[int] = None)[原始碼]¶
回傳一個新的 Spec,其中移除了所有尺寸為
1
的維度。當給定
dim
時,只會在該維度上執行 squeeze 操作。- 參數:
dim (int or None) – 要對其應用 squeeze 操作的維度
- to(dest: Union[dtype, device, str, int]) Bounded [原始碼]¶
將 TensorSpec 轉換為 device 或 dtype。
如果沒有進行任何更改,則回傳相同的 spec。
- to_numpy(val: torch.Tensor | tensordict.base.TensorDictBase, safe: Optional[bool] = None) numpy.ndarray | dict ¶
回傳輸入張量對應的
np.ndarray
。這應該是
encode()
的反向操作。- 參數:
val (torch.Tensor) – 要轉換為 numpy 的張量。
safe (bool) – 布林值,指示是否應該根據 spec 的網域對該值執行檢查。預設為
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 具有鍵,則會根據指定鍵所指向的 spec 檢查值 dtype。
- unflatten(dim: int, sizes: Tuple[int]) T ¶
將
TensorSpec
展平。有關此方法的更多資訊,請參閱
unflatten()
。
- unsqueeze(dim: int)[原始碼]¶
返回一個新的 Spec,其中包含一個額外的單例維度(位於
dim
指示的位置)。- 參數:
dim (int 或 None) – 套用 unsqueeze 運算的維度。
- zero(shape: Optional[Size] = None) torch.Tensor | tensordict.base.TensorDictBase ¶
返回 box 中一個填滿零的張量。
注意
即使無法保證
0
屬於 spec 域,此方法在違反此條件時也不會拋出例外。zero
的主要用例是產生空的資料緩衝區,而不是有意義的資料。- 參數:
shape (torch.Size) – 零張量的形狀
- 回傳:
在 TensorSpec box 中取樣的填滿零的張量。
- zeros(shape: Optional[Size] = None) torch.Tensor | tensordict.base.TensorDictBase ¶
Proxy to
zero()
.