<jupyter_start><jupyter_text>第8章 torchパッケージ  8.4 数学演算<jupyter_code>import torch
import numpy as np<jupyter_output><empty_output><jupyter_text>torch.abs<jupyter_code>#要素単位の絶対値を計算する
x = torch.abs(torch.tensor([-1, -2, 3]))
x<jupyter_output><empty_output><jupyter_text>torch.add<jupyter_code>#要素単位でスカラー値を加算する
x = torch.randn(4)
x
x = torch.add(x, 20)
x<jupyter_output><empty_output><jupyter_text>torch.clamp<jupyter_code>#Tensorの値を指定範囲に収める
x = torch.randn(4)
x
torch.clamp(x, min=-0.5, max=0.5)
x = torch.randn(4)
x
x = torch.clamp(x, max=0.5)
x<jupyter_output><empty_output><jupyter_text>torch.div<jupyter_code>x = torch.randn(5)
x
#入力の各要素をスカラー値で除算する
torch.div(x, 0.5)
x1 = torch.randn(4, 4)
x1
x2 = torch.randn(4)
x2
torch.div(x1, x2)<jupyter_output><empty_output><jupyter_text>torch.exp<jupyter_code>import math
#入力の要素に指数関数を計算する
torch.exp(torch.tensor([0, math.log(2)]))<jupyter_output><empty_output><jupyter_text>torch.log<jupyter_code>#自然対数を計算する
torch.log(torch.randn(5))<jupyter_output><empty_output><jupyter_text>torch.mul<jupyter_code>x = torch.randn(3)
x
#各要素で乗算する
torch.mul(x, 100)
x1 = torch.randn(4, 1)
x1
x2 = torch.randn(1, 4)
x2
torch.mul(x1, x2)<jupyter_output><empty_output><jupyter_text>torch.round<jupyter_code>x = torch.randn(4)
x
#最も近い整数に丸める
torch.round(x)<jupyter_output><empty_output><jupyter_text>torch.sigmoid<jupyter_code>x = torch.randn(4)
x
#sigmoidを計算する
torch.sigmoid(x)<jupyter_output><empty_output><jupyter_text>torch.sign<jupyter_code>x = torch.randn(4)
x
#符号を得る
torch.sign(x)<jupyter_output><empty_output><jupyter_text>torch.sqrt<jupyter_code>x = torch.randn(4)
x
#平方根を計算する
torch.sqrt(x)<jupyter_output><empty_output><jupyter_text>torch.tanh<jupyter_code>x = torch.randn(4)
x
#tanhを計算する
torch.tanh(x)<jupyter_output><empty_output><jupyter_text>torch.bmm<jupyter_code>batch1 = torch.randn(10, 3, 4)
batch2 = torch.randn(10, 4, 5)
#バッチを含んだTensor同士の行列積を計算する
res = torch.bmm(batch1, batch2)
res.size()<jupyter_output><empty_output><jupyter_text>torch.dot<jupyter_code>#１階Tensor同士の内積を計算する
torch.dot(torch.tensor([2, 3]), torch.tensor([2, 1]))<jupyter_output><empty_output><jupyter_text>torch.mm<jupyter_code>mat1 = torch.randn(2, 3)
mat2 = torch.randn(3, 3)
#行列Tensor同士の行列積を計算する
torch.mm(mat1, mat2)<jupyter_output><empty_output><jupyter_text>torch.matmul<jupyter_code># ベクトル x ベクトル
tensor1 = torch.randn(3)
tensor2 = torch.randn(3)
#色々なTensor同士の行列積を計算する
torch.matmul(tensor1, tensor2).size()
# 行列 x ベクトル
tensor1 = torch.randn(3, 4)
tensor2 = torch.randn(4)
torch.matmul(tensor1, tensor2).size()
# バッチ行列 x ブロードキャストされたベクトル
tensor1 = torch.randn(10, 3, 4)
tensor2 = torch.randn(4)
torch.matmul(tensor1, tensor2).size()
# バッチ行列 x バッチ行列
tensor1 = torch.randn(10, 3, 4)
tensor2 = torch.randn(10, 4, 5)
torch.matmul(tensor1, tensor2).size()
#バッチ処理された行列 x ブロードキャストされた行列
tensor1 = torch.randn(10, 3, 4)
tensor2 = torch.randn(4, 5)
torch.matmul(tensor1, tensor2).size()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15608919382504288, 'fraction_numerical': 0.0411663807890223, 'mean_word_length': 9.02061855670103, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '386720', 'license': 'MIT', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1569, 'n_words': 168, 'path': 'chapter8/section8_4.ipynb', 'repo_name': '146790g/pytorch_handbook', 'size': '2915'}