torch.empty_like¶
- torch.empty_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) Tensor ¶
傳回一個未初始化的 tensor,其大小與
input
相同。torch.empty_like(input)
等效於torch.empty(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)
。注意
如果
torch.use_deterministic_algorithms()
和torch.utils.deterministic.fill_uninitialized_memory
都設置為True
,則會初始化輸出 tensor,以防止因將資料用作操作的輸入而產生的任何可能的非確定性行為。浮點數和複數 tensors 會填充 NaN,而整數 tensors 會填充最大值。- 參數
input (Tensor) –
input
的大小將決定輸出 tensor 的大小。- 關鍵字參數
dtype (
torch.dtype
, optional) – 傳回 Tensor 的所需資料類型。預設值:如果None
,則預設為input
的 dtype。layout (
torch.layout
, optional) – 傳回 tensor 的所需佈局。預設值:如果None
,則預設為input
的佈局。device (
torch.device
, optional) – 傳回 tensor 的所需裝置。預設值:如果None
,則預設為input
的裝置。requires_grad (bool, optional) – 如果 autograd 應該記錄傳回 tensor 上的操作。預設值:
False
。memory_format (
torch.memory_format
, optional) – 傳回 Tensor 的所需記憶體格式。預設值:torch.preserve_format
。
範例
>>> a=torch.empty((2,3), dtype=torch.int32, device = 'cuda') >>> torch.empty_like(a) tensor([[0, 0, 0], [0, 0, 0]], device='cuda:0', dtype=torch.int32)