Pytorch torch リファレンスマニュアル

PyTorch ソフトウェアパッケージには、多次元テンソル用のデータ構造が含まれており、これらのテンソル上で実行される数学演算が定義されています。さらに、テンソルや任意の型のデータを効率的にシリアライズするための多くのユーティリティや、その他の便利なツールも提供しています。

さらに、CUDA 版もあり、計算能力 >= 3.0 の NVIDIA GPU 上でテンソル計算を実行することができます。

PyTorch torch API リファレンス

テンソル型の判定

関数 説明
torch.is_tensor(obj) 確認objPyTorchテンソルかどうかを確認します。
torch.is_storage(obj) 確認objPyTorchストレージオブジェクトかどうかを確認します。
torch.is_complex(input) 確認inputデータ型が複素数データ型かどうかを確認します。
torch.is_conj(input) 確認input共役テンソルかどうかを確認します。
torch.is_floating_point(input) 確認inputデータ型が浮動小数点データ型かどうかを確認します。
torch.is_nonzero(input) 確認input非ゼロの単一要素テンソルかどうかを確認します。
torch.set_default_dtype(d) デフォルトの浮動小数点データ型を設定します。d。
torch.get_default_dtype() 現在のデフォルトの浮動小数点データ型を取得します。torch.dtype。
torch.set_default_device(device) デフォルトを設定します。torch.Tensor割り当てるデバイスを設定します。device。
torch.get_default_device() デフォルトを取得します。torch.Tensor割り当てられるデバイス。
torch.set_default_tensor_type(tensor_type) デフォルトのテンソル型を設定します。tensor_type。
torch.numel(input) 返します。inputテンソル内の要素の総数。
torch.set_printoptions(...) テンソルのプリントオプションを設定します。

テンソルの作成

関数 説明
torch.tensor(data, dtype, device, requires_grad) データからテンソルを作成し、データをコピーします。自動勾配の履歴はありません。
torch.as_tensor(data, dtype, device) データをテンソルに変換し、データを共有して自動勾配の履歴を保持します。
torch.asarray(data, dtype, device) データをテンソル配列に変換します。
torch.from_numpy(ndarray) NumPy配列からテンソルを作成します(メモリを共有)。
torch.from_dlpack(ext_tensor) dlpackテンソルからPyTorchテンソルを作成します。
torch.frombuffer(buffer, dtype, count, offset) バッファから1次元テンソルを作成します。
torch.zeros(*size, dtype, device, requires_grad) すべてゼロのテンソルを作成します。
torch.zeros_like(input, dtype, device, requires_grad) 入力と同じ形状のすべてゼロのテンソルを作成します。
torch.ones(*size, dtype, device, requires_grad) すべて1のテンソルを作成します。
torch.ones_like(input, dtype, device, requires_grad) 入力と同じ形状のすべて1のテンソルを作成します。
torch.empty(*size, dtype, device, requires_grad) 未初期化のテンソルを作成します。
torch.empty_like(input, dtype, device, requires_grad) 入力と同じ形状の未初期化テンソルを作成します。
torch.empty_strided(size, stride, dtype, device) 指定されたストライドを持つ未初期化テンソルを作成します。
torch.arange(start, end, step, dtype, device, requires_grad) 等差数列のテンソルを作成します。
torch.range(start, end, step, dtype, device, requires_grad) end値を含む等差数列テンソルを作成します。
torch.linspace(start, end, steps, dtype, device, requires_grad) 等間隔の数列テンソルを作成します。
torch.logspace(start, end, steps, base, dtype, device, requires_grad) 対数間隔の数列テンソルを作成します。
torch.eye(n, m, dtype, device, requires_grad) 単位行列を作成します。
torch.full(size, fill_value, dtype, device, requires_grad) 指定された値で満たされたテンソルを作成します。
torch.full_like(input, fill_value, dtype, device, requires_grad) 入力と同じ形状の、値が満たされたテンソルを作成します。
torch.rand(*size, dtype, device, requires_grad) 一様分布の乱数テンソルを作成します(範囲 [0, 1))。
torch.rand_like(input, dtype, device, requires_grad) 入力と同じ形状の一様分布乱数テンソルを作成します。
torch.randn(*size, dtype, device, requires_grad) 標準正規分布の乱数テンソルを作成します。
torch.randn_like(input, dtype, device, requires_grad) 入力と同じ形状の標準正規分布乱数テンソルを作成します。
torch.randint(low, high, size, dtype, device, requires_grad) 整数の乱数テンソルを作成します。
torch.randint_like(input, low, high, dtype, device, requires_grad) 入力と同じ形状の整数乱数テンソルを作成します。
torch.randperm(n, dtype, device, requires_grad) 0からn-1までのランダムな順列を作成します。
torch.sparse_coo_tensor(indices, values, size, dtype, device, requires_grad) 指定されたindices位置にスパースCOOテンソルを構築します。
torch.sparse_csr_tensor(crow_indices, col_indices, values, size, dtype, device) スパースCSRテンソルを構築します。
torch.sparse_csc_tensor(ccol_indices, row_indices, values, size, dtype, device) スパースCSCテンソルを構築します。
torch.quantize_per_tensor(input, scale, zero_point, dtype) 量子化テンソルを作成します(per-tensor)。
torch.quantize_per_channel(input, scales, zero_points, axis, dtype) 量子化テンソルを作成します(per-channel)。
torch.dequantize(input) テンソルを逆量子化します。
torch.complex(real, imag) 実部と虚部から複素数テンソルを作成します。
torch.polar(abs, angle) 極座標から複素数テンソルを作成します。
torch.heaviside(input, values) ヘヴィサイドの階段関数を計算します。

インデックス、スライス、結合、変更操作

関数 説明
torch.cat(tensors, dim, out) 指定した次元に沿ってテンソルを連結します。
torch.concat(tensors, dim, out) 指定した次元に沿ってテンソルを連結します(catと同じ)。
torch.concatenate(tensors, dim, out) 指定した次元に沿ってテンソルを連結します(catと同じ)。
torch.stack(tensors, dim, out) 新しい次元に沿ってテンソルをスタックします。
torch.split(tensor, split_size, dim) 指定した次元に沿ってテンソルを分割します。
torch.chunk(tensor, chunks, dim) 指定した次元に沿ってテンソルをチャンク分割します。
torch.reshape(input, shape) テンソルの形状を変更します。
torch.transpose(input, dim0, dim1) テンソルの2つの次元を入れ替えます。
torch.t(input) 2次元テンソルを転置します。
torch.squeeze(input, dim) サイズ1の次元を削除します。
torch.unsqueeze(input, dim) 指定した位置にサイズ1の次元を挿入します。
torch.permute(input, dims) テンソルの次元を並べ替えます。
torch.movedim(input, source, destination) テンソルの次元を新しい位置に移動します。
torch.moveaxis(input, source, destination) テンソルの軸を新しい位置に移動します。
torch.narrow(input, dim, start, length) テンソルのスライスを返します。
torch.narrow_copy(input, dim, start, length) テンソルのスライスのコピーを返します。
torch.select(input, dim, index) 指定した次元でインデックスに対応するスライスを選択します。
torch.slice_scatter(input, src, dim, start, end) srcをinputのスライスに散布します。
torch.select_scatter(input, src, dim, index) srcを指定されたインデックス位置に散布します。
torch.diagonal_scatter(input, src, offset, dim1, dim2) 値を対角線上の位置に散布します。
torch.expand(input, size) テンソルのサイズを拡張します(ビュー)。
torch.expand_as(input, other) テンソルをotherと同じサイズに拡張します。
torch.masked_select(input, mask) ブールマスクに基づいて要素を選択します。
torch.index_select(input, dim, index) 指定した次元でインデックスに対応する要素を選択します。
torch.gather(input, dim, index, sparse_grad) 指定した次元で指定されたインデックスの要素を収集します。
torch.scatter(input, dim, index, src, reduce) 将 srcの値を散布します。inputの指定された位置に。
torch.scatter_add(input, dim, index, src) srcの値を指定された位置に加算します。
torch.scatter_reduce(input, dim, index, src, reduce, include_self) srcの値を指定された方法で指定された位置に集約します。
torch.index_add(input, dim, index, source, alpha) sourceをindexで指定された位置に加算します。
torch.index_copy(input, dim, index, source) sourceをindexで指定された位置にコピーします。
torch.index_reduce(input, dim, index, source, reduce, include_self) sourceを指定された方法でindexで指定された位置に集約します。
torch.take(input, index) 指定されたインデックス位置の要素を取得します。
torch.take_along_dim(input, indices, dim) 指定した次元でインデックス位置の要素を取得します。
torch.nonzero(input) ゼロでない要素のインデックスを返します。
torch.argwhere(input) 条件を満たす要素のインデックスを返します。
torch.where(condition, input, other) 条件に基づいて要素を返します。
torch.unbind(tensor, dim) 指定した次元に沿ってタプルに分割します。
torch.split_with_sizes(tensor, split_sizes, dim) サイズに基づいてテンソルを分割します。
torch.tensor_split(tensor, indices_or_sections, dim) インデックスまたはセグメント数に基づいてテンソルを分割します。
torch.hsplit(tensor, indices_or_sections) テンソルを水平方向に分割します。
torch.vsplit(tensor, indices_or_sections) テンソルを垂直方向に分割します。
torch.dsplit(tensor, indices_or_sections) テンソルを深さ方向に分割します。
torch.hstack(tensors, dim, out) テンソルを水平方向にスタックします。
torch.vstack(tensors, out) テンソルを垂直方向にスタックします。
torch.dstack(tensors, out) テンソルを深さ方向にスタックします。
torch.column_stack(tensors, out) テンソルを列方向にスタックします。
torch.row_stack(tensors, out) テンソルを行方向にスタックします(vstackと同じ)。
torch.tile(input, dims) テンソルを複数回繰り返します。
torch.repeat_interleave(input, repeats, dim) 指定した次元に沿って要素を繰り返します。
torch.flip(input, dims) 指定した次元に沿ってテンソルを反転します。
torch.fliplr(input) テンソルを左右に反転します。
torch.flipud(input) テンソルを上下に反転します。
torch.rot90(input, k, dims) テンソルを90度回転します。
torch.linalg.matrix_transpose(input) 行列を転置します。
torch.adjoint(input) テンソルの随伴行列を返します。
torch.resolve_conj(input) 共役テンソルを解決します。
torch.resolve_neg(input) 負のテンソルを解決します。
torch.view_as_real(input) 複素数テンソルを実数テンソルとして扱います。
torch.view_as_complex(input) 実数テンソルを複素数テンソルとして扱います。
torch.unravel_index(indices, shape) フラット化されたインデックスを多次元インデックスに変換します。

乱数生成

関数 説明
torch.manual_seed(seed) 乱数シードを設定します(CPU)。
torch.seed() 乱数シードを設定し、新しいシード値を返します。
torch.initial_seed() 現在の乱数シードを返します。
torch.get_rng_state() 乱数生成器の状態を返します。
torch.set_rng_state(state) 乱数生成器の状態を設定します。
torch.rand(*size, dtype, device, requires_grad) 一様分布の乱数テンソルを作成します(範囲 [0, 1))。
torch.rand_like(input, dtype, device, requires_grad) 入力と同じ形状の一様分布乱数テンソルを作成します。
torch.randn(*size, dtype, device, requires_grad) 標準正規分布の乱数テンソルを作成します。
torch.randn_like(input, dtype, device, requires_grad) 入力と同じ形状の標準正規分布乱数テンソルを作成します。
torch.randint(low, high, size, dtype, device, requires_grad) 整数の乱数テンソルを作成します。
torch.randint_like(input, low, high, dtype, device, requires_grad) 入力と同じ形状の整数乱数テンソルを作成します。
torch.randperm(n, dtype, device, requires_grad) 0からn-1までのランダムな順列を作成します。
torch.bernoulli(input, *, generator) ベルヌーイ分布から乱数を生成します。
torch.multinomial(input, num_samples, replacement, generator) 多項分布のサンプリングを行います。
torch.normal(mean, std, out) 正規分布から乱数を生成します。
torch.poisson(input, generator) ポアソン分布から乱数を生成します。

シリアライズ

関数 説明
torch.save(obj, f, pickle_module, pickle_protocol) オブジェクトをファイルに保存します。
torch.load(f, map_location, pickle_module, weights_only) ファイルからオブジェクトを読み込みます。

勾配制御

関数 説明
torch.no_grad() コンテキストマネージャ。勾配計算を無効にします。
torch.enable_grad() コンテキストマネージャ。勾配計算を有効にします。
torch.set_grad_enabled(grad) 勾配計算を有効にするかどうかを設定します。
torch.is_grad_enabled() 勾配計算が有効かどうかを確認します。
torch.inference_mode() コンテキストマネージャ。推論モード(勾配とautogradを無効にします)。
torch.is_inference_mode_enabled() 推論モードが有効かどうかを確認します。

数学演算 - 点操作

関数 説明
torch.abs(input, out) 要素ごとの絶対値。
torch.absolute(input, out) 要素ごとの絶対値(abs と同じ)。
torch.acos(input, out) 要素ごとの逆余弦。
torch.arccos(input, out) 要素ごとの逆余弦(acos と同じ)。
torch.acosh(input, out) 要素ごとの逆双曲線余弦。
torch.arccosh(input, out) 要素ごとの逆双曲線余弦(acosh と同じ)。
torch.add(input, other, alpha, out) 要素ごとの加算(alpha スケーリングを指定可能)。
torch.addcdiv(input, tensor1, tensor2, value, out) input + value * (tensor1 / tensor2) を実行します。
torch.addcmul(input, tensor1, tensor2, value, out) input + value * (tensor1 * tensor2) を実行します。
torch.angle(input, out) 複素数テンソルの位相角を返します。
torch.asin(input, out) 要素ごとの逆正弦。
torch.arcsin(input, out) 要素ごとの逆正弦(asin と同じ)。
torch.asinh(input, out) 要素ごとの逆双曲線正弦。
torch.arcsinh(input, out) 要素ごとの逆双曲線正弦(asinh と同じ)。
torch.atan(input, out) 要素ごとの逆正接。
torch.arctan(input, out) 要素ごとの逆正接(atan と同じ)。
torch.atan2(input, other, out) 要素ごとの 2 次元逆正接。
torch.arctan2(input, other, out) 要素ごとの 2 次元逆正接(atan2 と同じ)。
torch.atanh(input, out) 要素ごとの逆双曲線正接。
torch.arctanh(input, out) 要素ごとの逆双曲線正接(atanh と同じ)。
torch.bitwise_not(input, out) 要素ごとのビット単位の否定。
torch.bitwise_and(input, other, out) 要素ごとのビット単位の論理積(AND)。
torch.bitwise_or(input, other, out) 要素ごとのビット単位の論理和(OR)。
torch.bitwise_xor(input, other, out) 要素ごとのビット単位の排他的論理和(XOR)。
torch.bitwise_left_shift(input, other, out) 要素ごとの左シフト。
torch.bitwise_right_shift(input, other, out) 要素ごとの右シフト。
torch.ceil(input, out) 要素ごとの切り上げ。
torch.clamp(input, min, max, out) テンソルの値を指定された範囲内に制限します。
torch.clip(input, min, max, out) テンソルの値を指定された範囲内に制限します(clamp と同じ)。
torch.conj_physical(input, out) 要素ごとの物理共役を計算します。
torch.copysign(input, other, out) 要素ごとに符号をコピーします。
torch.cos(input, out) 要素ごとの余弦。
torch.cosh(input, out) 要素ごとの双曲線余弦。
torch.deg2rad(input, out) 角度をラジアンに変換します。
torch.div(input, other, rounding_mode, out) 要素ごとの除算。
torch.divide(input, other, rounding_mode, out) 要素ごとの除算(div と同じ)。
torch.digamma(input, out) 要素ごとの psi 関数(対数導関数)を計算します。
torch.erf(input, out) 要素ごとの誤差関数。
torch.erfc(input, out) 要素ごとの相補誤差関数。
torch.erfinv(input, out) 要素ごとの逆誤差関数。
torch.exp(input, out) 要素ごとの指数関数。
torch.exp2(input, out) 要素ごとの 2 のべき乗。
torch.expm1(input, out) 要素ごとの exp(x) - 1。
torch.fake_quantize_per_channel_affine(input, scale, zero_point, axis, quant_min, quant_max) チャネルごとの量子化をシミュレートします。
torch.fake_quantize_per_tensor_affine(input, scale, zero_point, quant_min, quant_max) テンソルごとの量子化をシミュレートします。
torch.fix(input, out) 要素ごとに整数部分を取得します(ゼロ方向への切り捨て)。
torch.float_power(input, exponent, out) 要素ごとの浮動小数点べき乗演算。
torch.floor(input, out) 要素ごとの切り捨て。
torch.floor_divide(input, other, out) 要素ごとの整数除算。
torch.fmod(input, other, out) 要素ごとの剰余(余り)。
torch.frac(input, out) 要素ごとの小数部分を取得します。
torch.frexp(input, out) 浮動小数点数を仮数と指数に分解します。
torch.gradient(input, dim, spacing, edge_order) テンソルの勾配を計算します。
torch.imag(input, out) 複素数テンソルの虚部を返します。
torch.ldexp(input, other, out) 要素ごとに input * 2**other を計算します。
torch.lerp(input, end, weight, out) 要素ごとの線形補間。
torch.lgamma(input, out) 要素ごとのガンマ関数の対数。
torch.log(input, out) 要素ごとの自然対数。
torch.log10(input, out) 要素ごとの常用対数(底 10)。
torch.log1p(input, out) 要素ごとの log(1 + x)。
torch.log2(input, out) 要素ごとの底 2 の対数。
torch.logaddexp(input, other, out) 要素ごとの log(exp(input) + exp(other))。
torch.logaddexp2(input, other, out) 要素ごとの log2(2**input + 2**other)。
torch.logical_and(input, other, out) 要素ごとの論理積(AND)。
torch.logical_not(input, out) 要素ごとの論理否定(NOT)。
torch.logical_or(input, other, out) 要素ごとの論理和(OR)。
torch.logical_xor(input, other, out) 要素ごとの排他的論理和(XOR)。
torch.logit(input, eps, out) 要素ごとのロジット関数。
torch.hypot(input, other, out) 要素ごとの hypot 関数 sqrt(input^2 + other^2)。
torch.i0(input, out) 要素ごとの変形ベッセル関数(第 1 種、0 次)。
torch.igamma(input, other, out) 要素ごとの不完全ガンマ関数。
torch.igammac(input, other, out) 要素ごとの相補不完全ガンマ関数。
torch.mul(input, other, out) 要素ごとの乗算。
torch.multiply(input, other, out) 要素ごとの乗算(mul と同じ)。
torch.mvlgamma(input, p, out) 要素ごとの多変量ガンマ関数の対数。
torch.nan_to_num(input, nan, posinf, neginf, out) NaN を指定された値に置き換えます。
torch.neg(input, out) 要素ごとの符号反転。
torch.negative(input, out) 要素ごとの符号反転(neg と同じ)。
torch.nextafter(input, other, out) 要素ごとに次の表現可能な浮動小数点数を返します。
torch.polygamma(input, n, out) 要素ごとのポリガンマ関数。
torch.positive(input, out) 要素ごとの正の値を取ります。
torch.pow(input, exponent, out) 要素ごとのべき乗演算。
torch.quantized_batch_norm(input, weight, bias, mean, var, eps, output_scale, output_zero_point) 量子化バッチ正規化。
torch.quantized_max_pool1d(input, kernel_size, stride, padding, dilation, ceil_mode) 量子化最大プーリング(1D)。
torch.quantized_max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode) 量子化最大プーリング(2D)。
torch.rad2deg(input, out) ラジアンを角度に変換します。
torch.real(input, out) 複素数テンソルの実部を返します。
torch.reciprocal(input, out) 要素ごとの逆数。
torch.remainder(input, other, out) 要素ごとの剰余。
torch.round(input, decimals, out) 要素ごとの四捨五入。
torch.rsqrt(input, out) 要素ごとの逆平方根。
torch.sigmoid(input, out) 要素ごとのシグモイド関数。
torch.sign(input, out) 要素ごとの符号(-1, 0, 1)を返します。
torch.sgn(input, out) 要素ごとの符号ベクトルを返します。
torch.signbit(input, out) 要素ごとに符号ビットを確認します。
torch.sin(input, out) 要素ごとの正弦。
torch.sinc(input, out) 要素ごとの sinc 関数 sin(pi*x)/(pi*x)。
torch.sinh(input, out) 要素ごとの双曲線正弦。
torch.softmax(input, dim, dtype) 要素ごとのソフトマックス関数。
torch.sqrt(input, out) 要素ごとの平方根。
torch.square(input, out) 要素ごとの二乗。
torch.sub(input, other, alpha, out) 要素ごとの減算。
torch.subtract(input, other, alpha, out) 要素ごとの減算(sub と同じ)。
torch.tan(input, out) 要素ごとの正接。
torch.tanh(input, out) 要素ごとの双曲線正接。
torch.true_divide(input, other, out) 要素ごとの真の除算。
torch.trunc(input, out) 要素ごとの切り捨て(整数部分を取得)。
torch.xlogy(input, other, out) 要素ごとに input * log(other) を計算します。

数学演算 - リダクション操作

関数 説明
torch.argmax(input, dim, keepdim) 次元に沿った最大値のインデックスを返します。
torch.argmin(input, dim, keepdim) 次元に沿った最小値のインデックスを返します。
torch.amax(input, dim, keepdim, out) 次元に沿った最大値を返します。
torch.amin(input, dim, keepdim, out) 次元に沿った最小値を返します。
torch.aminmax(input, dim, keepdim, out) 次元に沿った最小値と最大値を返します。
torch.all(input, dim, keepdim, out) すべての要素が True かどうかを判定します。
torch.any(input, dim, keepdim, out) いずれかの要素が True かどうかを判定します。
torch.max(input, dim, keepdim, out) 指定された次元に沿って最大値を求めます。
torch.min(input, dim, keepdim, out) 指定された次元に沿って最小値を求めます。
torch.dist(input, other, p) 2 つのテンソル間の p ノルム距離を計算します。
torch.logsumexp(input, dim, keepdim, out) log-sum-exp を計算します。
torch.mean(input, dim, keepdim, out) 指定された次元に沿って平均を求めます。
torch.nanmean(input, dim, keepdim, out) 指定された次元に沿って平均を求めます(NaN を無視)。
torch.median(input, dim, keepdim, out) 指定された次元に沿って中央値を求めます。
torch.nanmedian(input, dim, keepdim, out) 指定された次元に沿って中央値を求めます(NaN を無視)。
torch.mode(input, dim, keepdim, out) 指定された次元に沿って最頻値を求めます。
torch.norm(input, p, dim, keepdim, out) p ノルムを計算します。
torch.nansum(input, dim, keepdim, out) 指定された次元に沿って合計を求めます(NaN を無視)。
torch.prod(input, dim, keepdim, dtype, out) 指定された次元に沿って積を求めます。
torch.quantile(input, q, dim, keepdim, out, method) 分位数を計算します。
torch.nanquantile(input, q, dim, keepdim, out, method) 分位数を計算します(NaN を無視)。
torch.std(input, dim, unbiased, keepdim, out) 標準偏差を計算します。
torch.std_mean(input, dim, unbiased, keepdim) 標準偏差と平均を計算します。
torch.sum(input, dim, keepdim, dtype, out) 指定された次元に沿って合計を求めます。
torch.unique(input, sorted, return_inverse, return_counts, dim) 一意の要素を返します。
torch.unique_consecutive(input, sorted, return_inverse, return_counts, dim) 連続した一意の要素を返します。
torch.var(input, dim, unbiased, keepdim, out) 分散を計算します。
torch.var_mean(input, dim, unbiased, keepdim) 分散と平均を計算します。
torch.count_nonzero(input, dim) 非ゼロ要素の数を数えます。
torch.hash_tensor(input) テンソルのハッシュ値を計算します。

数学演算 - 比較操作

関数 説明
torch.allclose(input, other, rtol, atol, equal_nan) 2 つのテンソルが近いかどうかを確認します(すべての要素)。
torch.argsort(input, dim, descending, stable) ソートされたインデックスを返します。
torch.eq(input, other, out) 要素ごとの等価比較。
torch.equal(input, other) 2 つのテンソルが完全に等しいかどうかを確認します。
torch.ge(input, other, out) 要素ごとの以上比較。
torch.greater_equal(input, other, out) 要素ごとの以上比較(ge と同じ)。
torch.gt(input, other, out) 要素ごとのより大きい比較。
torch.greater(input, other, out) 要素ごとのより大きい比較(gt と同じ)。
torch.isclose(input, other, rtol, atol, equal_nan) 2 つのテンソルが近いかどうかを確認します(要素ごと)。
torch.isfinite(input, out) 有限値かどうかを確認します。
torch.isin(elements, test_elements, assume_unique, invert) 要素が集合に含まれるかどうかを確認します。
torch.isinf(input, out) 無限値かどうかを確認します。
torch.isposinf(input, out) 正の無限大かどうかを確認します。
torch.isneginf(input, out) 負の無限大かどうかを確認します。
torch.isnan(input, out) NaN かどうかを確認します。
torch.isreal(input, out) 実数かどうかを確認します。
torch.kthvalue(input, k, dim, keepdim, out) k 番目に小さい要素とインデックスを返します。
torch.le(input, other, out) 要素ごとの以下比較。
torch.less_equal(input, other, out) 要素ごとの以下比較(le と同じ)。
torch.lt(input, other, out) 要素ごとのより小さい比較。
torch.less(input, other, out) 要素ごとのより小さい比較(lt と同じ)。
torch.maximum(input, other, out) 要素ごとの最大値を取ります。
torch.minimum(input, other, out) 要素ごとの最小値を取ります。
torch.fmax(input, other, out) 要素ごとの最大値を取ります(NaN を無視)。
torch.fmin(input, other, out) 要素ごとの最小値を取ります(NaN を無視)。
torch.ne(input, other, out) 要素ごとの不等比較。
torch.not_equal(input, other, out) 要素ごとの不等比較(ne と同じ)。
torch.sort(input, dim, descending, stable, out) 指定された次元に沿ってソートします。
torch.topk(input, k, dim, largest, sorted, out) 最大の k 個の要素とインデックスを返します。
torch.msort(input, out) 最後の次元に沿ってソートします(ソートされたテンソルを返します)。

数学演算 - スペクトル操作

関数 説明
torch.stft(input, n_fft, hop_length, win_length, window, center, normalized, onesided, return_complex) 短時間フーリエ変換。
torch.istft(input, n_fft, hop_length, win_length, window, center, normalized, onesided, length, return_complex) 短時間フーリエ変換の逆変換。
torch.bartlett_window(window_length, periodic, dtype, device) Bartlett 窓。
torch.blackman_window(window_length, periodic, dtype, device) Blackman 窓。
torch.hamming_window(window_length, periodic, alpha, beta, dtype, device) Hamming 窓。
torch.hann_window(window_length, periodic, dtype, device) Hann 窓。
torch.kaiser_window(window_length, periodic, beta, dtype, device) Kaiser 窓。

数学演算 - その他の操作

関数 説明
torch.atleast_1d(*tensors) 入力を少なくとも 1 次元のテンソルに変換する。
torch.atleast_2d(*tensors) 入力を少なくとも 2 次元のテンソルに変換する。
torch.atleast_3d(*tensors) 入力を少なくとも 3 次元のテンソルに変換する。
torch.bincount(input, weights, minlength) 非負整数の出現回数を計算する。
torch.block_diag(*tensors) 入力テンソルからブロック対角行列を構築する。
torch.broadcast_tensors(*tensors) 入力を同じ形状にブロードキャストする。
torch.broadcast_to(input, shape) テンソルを指定された形状にブロードキャストする。
torch.broadcast_shapes(*shapes) 操作を互換にするために形状をブロードキャストする。
torch.bucketize(input, boundaries, right) 入力をバケットインデックスにマッピングする。
torch.cartesian_prod(*tensors) デカルト積を計算する。
torch.cdist(x1, x2, p, compute_mode) ペアワイズ距離を計算する。
torch.clone(input, memory_format) テンソルのコピーを返す。
torch.combinations(input, r, with_replacement) 組み合わせを計算する。
torch.corrcoef(input) 相関係数行列を計算する。
torch.cov(input, correction, fweights, aweights) 共分散行列を計算する。
torch.cross(input, dim, out) クロス積を計算する。
torch.cummax(input, dim, out) 次元に沿って最大値を累積する。
torch.cummin(input, dim, out) 次元に沿って最小値を累積する。
torch.cumprod(input, dim, out) 次元に沿って積を累積する。
torch.cumsum(input, dim, out, dtype) 次元に沿って和を累積する。
torch.diag(input, diagonal, out) 対角行列を作成するか、対角線を抽出する。
torch.diag_embed(input, offset, dim1, dim2, out) 入力を対角線として埋め込む。
torch.diagflat(input, offset, out) 対角行列を作成する(フラットな入力)。
torch.diagonal(input, offset, dim1, dim2, out) 対角線の要素を抽出する。
torch.diff(input, n, dim, prepend, append, out) 差分を計算する。
torch.einsum(equation, *operands) アインシュタインの縮約記法。
torch.flatten(input, start_dim, end_dim, out) テンソルを平坦化する。
torch.ravel(input, out) 1次元テンソルに平坦化する。
torch.kron(input, other, out) Kronecker 積を計算する。
torch.meshgrid(*tensors, indexing) グリッドを作成する。
torch.lcm(input, other, out) 要素ごとの最小公倍数。
torch.logcumsumexp(input, dim, out) 次元に沿って log-sum-exp を累積する。
torch.renorm(input, p, dim, maxnorm, out) 指定されたノルムに再正規化する。
torch.roll(input, shifts, dims) テンソルの要素をロールする。
torch.searchsorted(sorted_sequence, values, side, sorter) ソート済みシーケンス内の位置を検索する。
torch.tensordot(a, b, dims) テンソルのドット積を計算する。
torch.trace(input, out) 行列のトレースを計算する。
torch.tril(input, diagonal, out) 下三角行列を抽出する。
torch.tril_indices(row, column, offset, dtype, device, layout) 下三角インデックスを生成する。
torch.triu(input, diagonal, out) 上三角行列を抽出する。
torch.triu_indices(row, column, offset, dtype, device, layout) 上三角インデックスを生成する。
torch.unflatten(input, dim, sizes) テンソルを展開する。
torch.vander(x, N, increasing, out) Vandermonde 行列を作成する。

線形代数 (BLAS と LAPACK)

関数 説明
torch.addbmm(input, batch1, batch2, beta, alpha, out) バッチ行列乗算加算。
torch.addmm(input, mat1, mat2, beta, alpha, out) 行列乗算加算。
torch.addmv(input, mat, vec, beta, alpha, out) 行列ベクトル乗算加算。
torch.addr(input, vec1, vec2, beta, alpha, out) ベクトル外積加算。
torch.baddbmm(input, batch1, batch2, beta, alpha, out) バッチ行列乗算加算(bmm + add)。
torch.bmm(input, mat2, out) バッチ行列乗算。
torch.chain_matmul(*matrices) 連鎖行列乗算。
torch.cholesky(input, upper, out) Cholesky 分解。
torch.cholesky_inverse(input, upper, out) Cholesky 分解による逆行列。
torch.cholesky_solve(input, input2, upper, out) Cholesky 分解による線形方程式の解法。
torch.dot(input, other, out) 2つのベクトルのドット積を計算する。
torch.geqrf(input, out) QR 分解(geqrf)。
torch.ger(input, other, out) ベクトルの外積を計算する。
torch.inner(input, other, out) 内積を計算する。
torch.inverse(input, out) 行列の逆行列を計算する。
torch.det(input, out) 行列の行列式を計算する。
torch.logdet(input, out) 行列式の対数を計算する。
torch.slogdet(input, out) 行列式の符号と対数絶対値を計算する。
torch.lu(input, pivot, get_infos, out) LU 分解。
torch.lu_solve(input, LU_data, LU_pivots, out) LU 分解の解を求める。
torch.lu_unpack(LU_data, LU_pivots, unpack_data, unpack_pivots) LU 分解結果をアンパックする。
torch.matmul(input, other, out) 行列乗算(異なる次元に対応)。
torch.matrix_power(input, n, out) 行列のべき乗。
torch.matrix_exp(input, out) 行列の指数関数。
torch.mm(input, mat2, out) 行列乗算(2次元)
torch.mv(input, vec, out) 行列ベクトル乗算。
torch.orgqr(input, q, out) QR 分解から Q を再構築する。
torch.ormqr(input, mat, vec, left, transpose, out) ormqr 操作。
torch.outer(input, other, out) ベクトルの外積を計算する。
torch.pinverse(input, rcond, out) Moore-Penrose 疑似逆行列を計算する。
torch.qr(input, out) QR 分解。
torch.svd(input, some, compute_uv, out) 特異値分解。
torch.svd_lowrank(input, q, niter, M) 低ランク SVD 近似。
torch.pca_lowrank(input, q, center, niter) 低ランク PCA 近似。
torch.lobpcg(input, K, B, X, M, P, max_iter, tol, debug, ortho_iparams, fpfloor) LOBPCG 固有値ソルバー。
torch.trapz(y, x, dim, out) 台形積分(非推奨、trapezoid を使用)。
torch.trapezoid(y, x, dim, out) 台形積分。
torch.cumulative_trapezoid(y, x, dim, out) 累積台形積分。
torch.triangular_solve(input, A, upper, transpose, unitriangular, out) 三角行列の解を求める。
torch.vdot(input, other, out) ベクトルのドット積を計算する(複素数対応)。

デバイス管理

関数 説明
torch.cuda.is_available() CUDA が利用可能かどうかを確認する。
torch.cuda.device_count() CUDA デバイスの数を返す。
torch.cuda.current_device() 現在の CUDA デバイスインデックスを返す。
torch.cuda.device(name) デバイスオブジェクトを作成する。
torch.cuda.device_context(device) デバイスコンテキストを作成する。
torch.device(device) デバイスオブジェクトを作成する(例:'cpu' 或 'cuda:0')。
torch.Tensor.to(device) テンソルを指定されたデバイスに移動する。
torch.get_device_module(device_type) デバイスモジュールを取得する(cuda、mps など)。

並列計算

関数 説明
torch.get_num_threads() CPU 操作に使用される総スレッド数を取得する。
torch.set_num_threads(int) CPU 操作に使用されるスレッド数を設定する。
torch.get_num_interop_threads() inter-op 並列スレッド数を取得する。
torch.set_num_interop_threads(int) inter-op 並列スレッド数を設定する。

ユーティリティ関数

関数 説明
torch.compiled_with_cxx11_abi() C++11 ABI を使用してコンパイルされているかどうかを確認する。
torch.result_type(tensor, other) 操作結果の dtype を返す。
torch.can_cast(from_dtype, to_dtype) データ型を変換できるかどうかを確認する。
torch.promote_types(type1, type2) 昇格後のデータ型を返す。
torch.use_deterministic_algorithms(mode, warn_only) 決定論的アルゴリズムを有効/無効にする。
torch.are_deterministic_algorithms_enabled() 決定論的アルゴリズムが有効かどうかを確認する。
torch.is_deterministic_algorithms_warn_only_enabled() 決定論的アルゴリズムが警告モードかどうかを確認する。
torch.set_deterministic_debug_mode(debug_mode) 決定論的デバッグモードを設定する。
torch.get_deterministic_debug_mode() 決定論的デバッグモードを取得する。
torch.set_float32_matmul_precision(precision) float32 行列乗算の精度を設定する。
torch.get_float32_matmul_precision() float32 行列乗算の精度を取得する。
torch.set_warn_always(enabled) 警告を常に表示するかどうかを設定する。
torch.is_warn_always_enabled() 警告を常に表示するかどうかを確認する。
torch.vmap(fn, in_dims, out_dims, randomness, chunk_size) ベクトル化マップ。
torch._assert(condition, message) アサーションチェック(内部使用)。
torch.typename(t) 型の文字列表現を返す。

コンパイル最適化

関数 説明
torch.compile(model, backend, options, dynamic) PyTorch モデルをコンパイルして最適化する。

例

例

import torch

# テンソルの作成
x = torch.tensor([1, 2, 3])
y = torch.zeros(2, 3)

# 数学演算
z = torch.add(x, 1)  # 要素ごとに 1 を加算
print(z)

# インデックスとスライス
mask = x > 1
selected = torch.masked_select(x, mask)
print(selected)

# デバイス管理
if torch.cuda.is_available():
    device = torch.device('cuda')
    x = x.to(device)
    print(x.device)

# 行列演算
a = torch.randn(3, 4)
b = torch.randn(4, 5)
c = torch.matmul(a, b)
print(c.shape)

# 勾配計算
x = torch.tensor([1., 2., 3.], requires_grad=True)
y = x.sum()
y.backward()
print(x.grad)

出力結果:

tensor([2, 3, 4])
tensor([2, 3])

より詳細な情報が必要な場合は、参照してくださいPyTorch 公式ドキュメント。

その他の拡張