<jupyter_start><jupyter_text>第9章 torch.nnパッケージ  9.11 損失関数<jupyter_code>import torch 
import torchvision
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
import torchvision.transforms as transforms
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')<jupyter_output><empty_output><jupyter_text>torch.nn.L1Loss<jupyter_code>loss = nn.L1Loss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5)
output = loss(input, target)
output.backward()
print(output)<jupyter_output>tensor(1.0741, grad_fn=<L1LossBackward>)<jupyter_text>torch.nn.MSELoss<jupyter_code>loss = nn.MSELoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5)
output = loss(input, target)
output.backward()
print(output)<jupyter_output>tensor(2.8663, grad_fn=<MseLossBackward>)<jupyter_text>torch.nn.CrossEntropyLoss<jupyter_code>loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()
print(output)<jupyter_output>tensor(2.1320, grad_fn=<NllLossBackward>)<jupyter_text>torch.nn.LogSoftmax<jupyter_code>m = nn.LogSoftmax()
loss = nn.NLLLoss()
# 入力はサイズN×C = 3×5
input = torch.randn(3, 5, requires_grad=True)
# ターゲット内の各要素は0 <= value <C
target = torch.tensor([1, 0, 4])
output = loss(m(input), target)
output.backward()<jupyter_output>/home/ubuntu/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:7: UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument.
  import sys<jupyter_text>torch.nn.BCELoss<jupyter_code>m = nn.Sigmoid()
loss = nn.BCELoss()
input = torch.randn(3, requires_grad=True)
target = torch.empty(3).random_(2)
output = loss(m(input), target)
output.backward()<jupyter_output><empty_output><jupyter_text>torch.nn.BCEWithLogitsLoss<jupyter_code>loss = nn.BCEWithLogitsLoss()
input = torch.randn(3, requires_grad=True)
target = torch.empty(3).random_(2)
output = loss(m(input), target)
output.backward()<jupyter_output><empty_output><jupyter_text>torch.nn.MultiLabelSoftMarginLoss<jupyter_code>loss = nn.MultiLabelSoftMarginLoss()
input = torch.randn(3, requires_grad=True)
target = torch.empty(3).random_(2)
output = loss(m(input), target)
output.backward()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13584288052373159, 'fraction_numerical': 0.022504091653027823, 'mean_word_length': 8.022140221402214, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '509767', 'license': 'MIT', 'n_tokens_mistral': 990, 'n_tokens_neox': 962, 'n_words': 157, 'path': 'chapter9/section9_11.ipynb', 'repo_name': '146790g/pytorch_handbook', 'size': '2444'}