本文簡要介紹python語言中 torch.Tensor.requires_grad_
的用法。
用法:
Tensor.requires_grad_(requires_grad=True) → Tensor
requires_grad(bool) -如果 autograd 應該記錄在這個張量上的操作。默認值:
True
。更改 autograd 是否應記錄此張量上的操作:就地設置此張量的
requires_grad
屬性。返回此張量。requires_grad_()
的主要用例是告訴 autograd 開始記錄張量tensor
上的操作。如果tensor
具有requires_grad=False
(因為它是通過 DataLoader 獲取的,或者需要預處理或初始化),tensor.requires_grad_()
會創建它,以便 autograd 將開始記錄tensor
上的操作。例子:
>>> # Let's say we want to preprocess some saved weights and use >>> # the result as new weights. >>> saved_weights = [0.1, 0.2, 0.3, 0.25] >>> loaded_weights = torch.tensor(saved_weights) >>> weights = preprocess(loaded_weights) # some function >>> weights tensor([-0.5503, 0.4926, -2.1158, -0.8303]) >>> # Now, start to record operations done to weights >>> weights.requires_grad_() >>> out = weights.pow(2).sum() >>> out.backward() >>> weights.grad tensor([-1.1007, 0.9853, -4.2316, -1.6606])
參數:
相關用法
- Python PyTorch Tensor.register_hook用法及代碼示例
- Python PyTorch Tensor.real用法及代碼示例
- Python PyTorch Tensor.refine_names用法及代碼示例
- Python PyTorch Tensor.rename用法及代碼示例
- Python PyTorch Tensor.repeat用法及代碼示例
- Python PyTorch Tensor.resize_用法及代碼示例
- Python PyTorch Tensor.unflatten用法及代碼示例
- Python PyTorch Tensor.storage_offset用法及代碼示例
- Python PyTorch Tensor.to用法及代碼示例
- Python PyTorch Tensor.sparse_mask用法及代碼示例
- Python PyTorch Tensor.is_leaf用法及代碼示例
- Python PyTorch Tensor.imag用法及代碼示例
- Python PyTorch Tensor.unfold用法及代碼示例
- Python PyTorch Tensor.view用法及代碼示例
- Python PyTorch Tensor.new_empty用法及代碼示例
- Python PyTorch Tensor.index_copy_用法及代碼示例
- Python PyTorch Tensor.new_tensor用法及代碼示例
- Python PyTorch Tensor.scatter_用法及代碼示例
- Python PyTorch Tensor.fill_diagonal_用法及代碼示例
- Python PyTorch Tensor.item用法及代碼示例
- Python PyTorch Tensor.tolist用法及代碼示例
- Python PyTorch Tensor.put_用法及代碼示例
- Python PyTorch Tensor.map_用法及代碼示例
- Python PyTorch Tensor.stride用法及代碼示例
- Python PyTorch Tensor.index_fill_用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.Tensor.requires_grad_。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。