本文簡要介紹python語言中 torch.nn.functional.cross_entropy 的用法。
用法:
torch.nn.functional.cross_entropy(input, target, weight=None, size_average=None, ignore_index=- 100, reduce=None, reduction='mean', label_smoothing=0.0)input(Tensor) - 其中
C = number of classes或 在 2D 損失的情況下,或 其中 在 K-dimensional 損失的情況下。input預計包含非標準化分數(通常稱為 logits)。target(Tensor) -如果包含類索引,形狀 ,其中每個值是 ,或 與 在 K-dimensional 損失的情況下。如果包含類概率,則與輸入的形狀相同。
weight(Tensor,可選的) -給每個類一個手動重新調整的權重。如果給定,則必須是大小為
C的張量size_average(bool,可選的) -已棄用(請參閱
reduction)。默認情況下,損失是批次中每個損失元素的平均值。請注意,對於某些損失,每個樣本有多個元素。如果字段size_average設置為False,則會對每個小批量的損失求和。當 reduce 為False時忽略。默認值:Trueignore_index(int,可選的) -指定一個被忽略且不影響輸入梯度的目標值。當
size_average為True時,損失是在非忽略目標上的平均值。請注意,ignore_index僅在目標包含類索引時適用。默認值:-100reduce(bool,可選的) -已棄用(請參閱
reduction)。默認情況下,根據size_average對每個小批量的觀察結果進行平均或求和。當reduce是False時,返回每個批次元素的損失並忽略size_average。默認值:Truereduction(string,可選的) -指定要應用於輸出的縮減:
'none'|'mean'|'sum'。'none':不應用減少,'mean':輸出的總和將除以輸出中的元素數,'sum':輸出將被求和。注意:size_average和reduce正在被棄用,同時,指定這兩個參數中的任何一個都將覆蓋reduction。默認值:'mean'label_smoothing(float,可選的) -[0.0, 1.0] 中的浮點數。指定計算損失時的平滑量,其中 0.0 表示不平滑。目標成為原始基本事實和均勻分布的混合體,如 Rethinking the Inception Architecture for Computer Vision 中所述。默認值:。
該標準計算輸入和目標之間的交叉熵損失。
有關詳細信息,請參閱
CrossEntropyLoss。例子:
>>> # Example of target with class indices >>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randint(5, (3,), dtype=torch.int64) >>> loss = F.cross_entropy(input, target) >>> loss.backward() >>> >>> # Example of target with class probabilities >>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randn(3, 5).softmax(dim=1) >>> loss = F.cross_entropy(input, target) >>> loss.backward()
參數:
相關用法
- Python PyTorch cross用法及代碼示例
- Python PyTorch criteo_terabyte用法及代碼示例
- Python PyTorch criteo_kaggle用法及代碼示例
- Python PyTorch create_feature_extractor用法及代碼示例
- Python PyTorch cholesky用法及代碼示例
- Python PyTorch column_stack用法及代碼示例
- Python PyTorch cumprod用法及代碼示例
- Python PyTorch calculate_gain用法及代碼示例
- Python PyTorch cov用法及代碼示例
- Python PyTorch cos用法及代碼示例
- Python PyTorch compute_deltas用法及代碼示例
- Python PyTorch conv_transpose3d用法及代碼示例
- Python PyTorch combinations用法及代碼示例
- Python PyTorch conv2d用法及代碼示例
- Python PyTorch cummax用法及代碼示例
- Python PyTorch custom_from_mask用法及代碼示例
- Python PyTorch collect_all用法及代碼示例
- Python PyTorch chunk用法及代碼示例
- Python PyTorch convert用法及代碼示例
- Python PyTorch conv1d用法及代碼示例
- Python PyTorch chain_matmul用法及代碼示例
- Python PyTorch cat用法及代碼示例
- Python PyTorch constant_用法及代碼示例
- Python PyTorch context用法及代碼示例
- Python PyTorch count_nonzero用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.nn.functional.cross_entropy。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
