算術演算子(Arithmetic Operators)
基本的関数(Basic Math Functions)
行列関数(Matrix Math Functions)
テンソル関数(Tensor Math Function)
複素数関数(Complex Number Functions)
リダクション(Reduction)
スキャン(Scan)
セグメンテーション(Segmentation)
シーケンスの比較と索引付け(Sequence Comparison and Indexing)
算術演算子(Arithmetic Operators)
TensorFlowは、基本的な算術演算子をグラフに追加するために使用できるいくつかの演算を提供します。
tf.add(x, y)
行列の場合、各要素ごとに x+y を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4], shape=[2, 2]) y = tf.constant([5, 6, 7, 8], shape=[2, 2]) # execute add() result = tf.add(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[ 6 8] # [10 12]]
tf.subtract(x, y)
行列の場合、各要素ごとに x-y を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4], shape=[2, 2]) y = tf.constant([5, 6, 7, 8], shape=[2, 2]) # execute subtract() result = tf.subtract(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[-4 -4] # [-4 -4]]
tf.multiply(x, y)
行列の場合、各要素ごとに x*y を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4], shape=[2, 2]) y = tf.constant([5, 6, 7, 8], shape=[2, 2]) # execute multiply() result = tf.multiply(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[ 5 12] # [21 32]]
tf.scalar_mul(x, y)
行列ではなくスカラー値のみを入力とし、その積を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant(2) y = tf.constant(3) # execute scalar_mul() result = tf.scalar_mul(x, y) with tf.Session() as sess: print(sess.run(result)) # result # 6
tf.div(x, y)
行列の場合、各要素ごとに x/y を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5.0, 6.0, 7.0, 8.0], shape=[2, 2]) result = tf.div(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0.2 0.33333334] # [0.42857143 0.5 ]]
tf.divide(x, y)
div()と同じく、行列の場合、各要素ごとに x/y を計算した行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5.0, 6.0, 7.0, 8.0], shape=[2, 2]) result = tf.divide(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0.2 0.33333334] # [0.42857143 0.5 ]]
tf.truediv(x, y)
div()と同じく、行列の場合、各要素ごとに x/y を計算した行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5.0, 6.0, 7.0, 8.0], shape=[2, 2]) result = tf.truediv(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0.2 0.33333334] # [0.42857143 0.5 ]]
tf.floordiv(x, y)
行列の場合、各要素ごとに x/y を計算し小数点以下切り捨てした行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5.0, 6.0, 7.0, 8.0], shape=[2, 2]) result = tf.floordiv(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 0.] # [0. 0.]]
tf.realdiv(x, y)
div()と同じく、行列の場合、各要素ごとに x/y を計算した行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5.0, 6.0, 7.0, 8.0], shape=[2, 2]) result = tf.realdiv(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0.2 0.33333334] # [0.42857143 0.5 ]]
tf.truncatediv(x, y)
行列の場合、各要素ごとに x/y を計算し小数点以下切り捨てした行列を返す(tf.floordivと挙動が異なり、0に向かって丸める)。xおよびyの要素をfloatにするとエラーが出る。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, -4], shape=[2, 2]) y = tf.constant([5, 6, 7, 3], shape=[2, 2]) result = tf.truncatediv(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0] # [0 -1]]
tf.floor_div(x, y)
行列の場合、各要素ごとに x/y を計算し小数点以下切り捨てした行列を返す(tf.floordivと同じ)。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5.0, 6.0, 7.0, 8.0], shape=[2, 2]) result = tf.floor_div(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 0.] # [0. 0.]]
tf.truncatemod(x, y)
行列の場合、要素ごとに x/y のmod(余り)を計算し小数点以下切り捨てした値を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([5, 6, 7, 8], shape=[2, 2]) y = tf.constant([1, 2, 3, 4], shape=[2, 2]) result = tf.truncatemod(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0] # [1 0]]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([5, 6, -7, 8], shape=[2, 2]) y = tf.constant([1, 2, 3, 4], shape=[2, 2]) result = tf.truncatemod(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0] # [-1 0]]
tf.floormod(x, y)
行列の場合、要素ごとに x/y のmod(余り)を計算し小数点以下切り捨てした値を返す。xまたはyが0未満の際の挙動がtf.truncatemodとは異なることに注意。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([5, 6, 7, 8], shape=[2, 2]) y = tf.constant([1, 2, 3, 4], shape=[2, 2]) result = tf.truncatemod(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0] # [1 0]]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([5, 6, -7, 8], shape=[2, 2]) y = tf.constant([1, 2, 3, 4], shape=[2, 2]) result = tf.truncatemod(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0] # [2 0]]
tf.mod(x, y)
行列の場合、要素ごとに x/y のmod(余り)を計算し小数点以下切り捨てした値を返す。xまたはyが0未満の際の挙動がtf.truncatemodとは異なることに注意。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([5, 6, 7, 8], shape=[2, 2]) y = tf.constant([1, 2, 3, 4], shape=[2, 2]) result = tf.mod(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0] # [1 0]]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([5, 6, -7, 8], shape=[2, 2]) y = tf.constant([1, 2, 3, 4], shape=[2, 2]) result = tf.mod(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0] # [2 0]]
tf.cross(x, y)
外積を計算する。外積はその計算の内容からxおよびyが3次元以上のベクトルである必要がある。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9], shape=[3, 3]) y = tf.constant([9, 8, 7, 6, 5, 4, 3, 2, 1], shape=[3, 3]) result = tf.cross(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[-10 20 -10] # [-10 20 -10] # [-10 20 -10]]
基本的関数(Basic Math Functions)
TensorFlowは、基本的な数学関数をグラフに追加するために使用できるいくつかの演算を提供します。
tf.add_n([x, y])
配列として指定された行列x,y,…につき要素ごとに加えた行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4], shape=[2, 2]) y = tf.constant([9, 8, 7, 6], shape=[2, 2]) result = tf.add_n([x, y]) with tf.Session() as sess: print(sess.run(result)) # result # [[10 10] # [10 10]]
tf.abs(x)
行列の場合、与えられたxの要素ごとに絶対値を計算した行列を返す。xとして配列にした複数の行列を渡すとそれぞれにつき計算する。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, -2, 3, -4], shape=[2, 2]) result = tf.abs(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1 2] # [3 4]]
tf.negative(x)
行列の場合、与えられた行列の要素ごとに正負の符号を反転させた行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, -2, 3, -4], shape=[2, 2]) result = tf.negative(x) with tf.Session() as sess: print(sess.run(result)) # result # [[-1 2] # [-3 4]]
tf.sign(x)
行列の場合、与えられた行列の要素ごとに正負の符号(正は1、負は-1、ゼロは0)を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, -2, 3, -4], shape=[2, 2]) result = tf.sign(x) with tf.Session() as sess: print(sess.run(result)) # result # [[ 1 -1] # [ 1 -1]]
tf.reciprocal(x)
行列の場合、各要素の逆数を要素とする行列を返す。要素は小数点以下を含む(1ではなく1.0)必要がある。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) result = tf.reciprocal(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 0.5 ] # [0.33333334 0.25 ]]
tf.square(x)
行列の場合、各要素の二乗を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) result = tf.square(x) with tf.Session() as sess: print(sess.run(result)) # result # [[ 1. 4.] # [ 9. 16.]]
tf.round(x)
行列の場合、各要素の四捨五入値を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.3, 2.6, 3.2, 4.9], shape=[2, 2]) result = tf.round(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 3.] # [3. 5.]]
tf.sqrt(x)
行列の場合、各要素の平方根を要素とする行列を返す。与える行列の要素は浮動小数(1ではなく1.0)である必要がある。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1., 2., 3., 4.], shape=[2, 2]) result = tf.sqrt(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 1.4142135] # [1.7320508 2. ]]
tf.rsqrt(x)
行列の場合、各要素の平方根の逆数を要素とする行列を返す。与える行列の要素は浮動小数(1ではなく1.0)である必要がある。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1., 2., 3., 4.], shape=[2, 2]) result = tf.sqrt(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 0.70710677] # [0.57735026 0.5 ]]
tf.pow(x, y)
行列の場合、各要素ごとにxのy乗を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4], shape=[2, 2]) y = tf.constant([5, 4, 3, 2], shape=[2, 2]) result = tf.pow(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[ 1 16] # [27 16]]
tf.exp(x)
行列の場合、各要素ごとに自然対数の底e(ネイピア数)のx乗を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1., 2., 3., 4.], shape=[2, 2]) result = tf.exp(x) with tf.Session() as sess: print(sess.run(result)) # result # [[ 2.7182817 7.389056 ] # [20.085537 54.59815 ]]
tf.expm1(x)
行列の場合、各要素ごとに自然対数の底e(ネイピア数)のx乗から1を引いたを要素とする行列を返す。行列の各要素の値はtf.exp(x)で得る値から1を引いた値である。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1., 2., 3., 4.], shape=[2, 2]) result = tf.expm1(x) with tf.Session() as sess: print(sess.run(result)) # result # [[ 1.7182819 6.389056 ] # [19.085537 53.59815 ]]
tf.log(x)
行列の場合、各要素ごとにxの自然対数(log x)を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1., 2., 3., 4.], shape=[2, 2]) result = tf.log(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 0.6931472] # [1.0986123 1.3862944]]
tf.log1p(x)
行列の場合、各要素ごとにxの自然対数(log (x+1))を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1., 2., 3., 4.], shape=[2, 2]) result = tf.log1p(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0.6931472 1.0986123] # [1.3862944 1.609438 ]]
tf.ceil(x)
行列の場合、各要素ごとにそれを超える最小の整数を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.1, 2.2, 3.3, 4.4], shape=[2, 2]) result = tf.ceil(x) with tf.Session() as sess: print(sess.run(result)) # result # [[2. 3.] # [4. 5.]]
tf.floor(x)
行列の場合、各要素ごとに小数点以下切り捨てた整数値を要素とする行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.1, 2.2, 3.3, 4.4], shape=[2, 2]) result = tf.floor(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 2.] # [3. 4.]]
tf.maximum(x, y)
行列の場合、xとyの呼応する各要素ごとに大小を比較して大きい方の要素を寄せ集めた行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.1, 2.2, 3.3, 4.4], shape=[2, 2]) y = tf.constant([5., 4., 3., 2.], shape=[2, 2]) result = tf.maximum(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[5. 4. ] # [3.3 4.4]]
tf.minimum(x, y)
行列の場合、xとyの呼応する各要素ごとに大小を比較して小さい方の要素を寄せ集めた行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.1, 2.2, 3.3, 4.4], shape=[2, 2]) y = tf.constant([5., 4., 3., 2.], shape=[2, 2]) result = tf.minimum(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[1.1 2.2] # [3. 2. ]]
tf.cos(x)
行列の場合、各要素ごとに cos x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 1.07, 3.14, 4.21], shape=[2, 2]) result = tf.cos(x) with tf.Session() as sess: print(sess.run(result)) # result # [[ 1. 0.48012418] # [-0.99999875 -0.48152065]]
tf.sin(x)
行列の場合、各要素ごとに sin x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 1.07, 3.14, 4.21], shape=[2, 2]) result = tf.sin(x) with tf.Session() as sess: print(sess.run(result)) # result # [[ 0. 0.87720054] # [ 0.00159255 -0.87643474]]
tf.lbeta(x)
行列の場合、各要素ごとにベータ関数により計算した値を持つ行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 1.07, 3.14, 4.21], shape=[2, 2]) result = tf.lbeta(x) with tf.Session() as sess: print(sess.run(result)) # result # [ inf -4.3561463]
tf.tan(x)
行列の場合、各要素ごとに tan x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 1.07, 3.14, 4.21], shape=[2, 2]) result = tf.tan(x) with tf.Session() as sess: print(sess.run(result)) # result # [[ 0.0000000e+00 1.8270284e+00] # [-1.5925501e-03 1.8201394e+00]]
tf.acos(x)
行列の場合、各要素ごとに acos x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 0.2, 0.5, 1.0], shape=[2, 2]) result = tf.acos(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1.5707964 1.3694383] # [1.0471975 0. ]]
tf.asin(x)
行列の場合、各要素ごとに asin x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 0.2, 0.5, 1.0], shape=[2, 2]) result = tf.asin(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 0.20135793] # [0.5235988 1.5707963 ]]
tf.atan(x)
行列の場合、各要素ごとに atan x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 0.2, 0.5, 1.0], shape=[2, 2]) result = tf.atan(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 0.19739556] # [0.4636476 0.7853982 ]]
tf.cosh(x)
行列の場合、各要素ごとに cosh x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 0.2, 0.5, 1.0], shape=[2, 2]) result = tf.cosh(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 1.0200667] # [1.127626 1.5430807]]
tf.sinh(x)
行列の場合、各要素ごとに sinh x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 0.2, 0.5, 1.0], shape=[2, 2]) result = tf.sinh(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 0.20133601] # [0.5210953 1.1752012 ]]
tf.asinh(x)
行列の場合、各要素ごとに asinh x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0.0, 0.2, 0.5, 1.0], shape=[2, 2]) result = tf.asinh(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 0.19869012] # [0.4812118 0.8813736 ]]
tf.acosh(x)
行列の場合、各要素ごとに acosh x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) result = tf.acosh(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 1.316958 ] # [1.7627472 2.063437 ]]
tf.atanh(x)
行列の場合、各要素ごとに atanh x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) result = tf.atanh(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 1.316958 ] # [1.7627472 2.063437 ]]
tf.lgamma(x)
行列の場合、各要素ごとに lgamma x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) result = tf.lgamma(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0. 0. ] # [0.6931472 1.7917595]]
tf.digamma(x)
行列の場合、各要素ごとに digamma x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) result = tf.digamma(x) with tf.Session() as sess: print(sess.run(result)) # result # [[-0.5772159 0.4227842] # [ 0.9227842 1.2561177]]
tf.erf(x)
行列の場合、各要素ごとに erf x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) result = tf.erf(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0.8427008 0.9953223] # [0.9999779 1. ]]
tf.erfc(x)
行列の場合、各要素ごとに erfc x を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) result = tf.erfc(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1.5729921e-01 4.6777348e-03] # [2.2090497e-05 1.5417259e-08]]
tf.squared_difference(x, y)
行列の場合、各要素ごとに (y-x)^2 を計算した行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5., 4., 3., 2.], shape=[2, 2]) result = tf.squared_difference(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[16. 4.] # [ 0. 4.]]
tf.igamma(x, y)
行列の場合、各要素ごとに igamma(x, y) を計算した行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5., 4., 3., 2.], shape=[2, 2]) result = tf.igamma(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0.99326205 0.9084218 ] # [0.5768099 0.14287655]]
tf.igammac(x, y)
行列の場合、各要素ごとに igammac(x, y) を計算した行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0], shape=[2, 2]) y = tf.constant([5., 4., 3., 2.], shape=[2, 2]) result = tf.igammac(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0.00673795 0.0915782 ] # [0.4231901 0.85712343]]
tf.zeta(x, y)
行列の場合、各要素ごとにゼータ関数 zeta(x, y) を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2.0, 3.0, 4.0, 5.0], shape=[2, 2]) y = tf.constant([5.0, 4.0, 3.0, 2.0], shape=[2, 2]) result = tf.zeta(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[0.22132295 0.04001987] # [0.01982323 0.03692776]]
tf.polygamma(x, y)
行列の場合、各要素ごとにゼータ関数 zeta(x, y) を計算した行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2.0, 3.0, 4.0, 5.0], shape=[2, 2]) y = tf.constant([5.0, 4.0, 3.0, 2.0], shape=[2, 2]) result = tf.polygamma(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[-0.04878973 0.04486533] # [-0.13626613 2.0811675 ]]
tf.betainc(x, y, z)
行列の場合、各要素ごとに不完全ベータ積分 betainc(x, y, z) を計算した行列を返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2.0, 3.0, 4.0, 5.0], shape=[2, 2]) y = tf.constant([5.0, 4.0, 3.0, 2.0], shape=[2, 2]) z = tf.constant([1.0, 0.0, 0.0, 1.0], shape=[2, 2]) result = tf.betainc(x, y, z) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 0.] # [0. 1.]]
tf.rint(x)
行列の場合、各要素ごとにもっとも近い整数の計算 rint(x) をした行列を返す。[グラフを見る]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2.2, -3.6, 4.7, -5.1], shape=[2, 2]) result = tf.rint(x) with tf.Session() as sess: print(sess.run(result)) # result # [[ 2. -4.] # [ 5. -5.]]
行列関数(Matrix Math Functions)
TensorFlowは、線形代数関数を行列にグラフに追加するために使用できるいくつかの演算を提供します。
tf.diag(x)
与えられた入力を対角線に要素として持つ正方行列を生成します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4]) result = tf.diag(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1 0 0 0] # [0 2 0 0] # [0 0 3 0] # [0 0 0 4]]
tf.diag_part(x)
与えられた入力の対角線要素のみを抜き取り、それらを要素として持つ行列を生成します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 4], shape=[4, 4]) result = tf.diag_part(x) with tf.Session() as sess: print(sess.run(result)) # result # [1 2 3 4]
tf.trace(x)
与えられた入力のトレース(対角和:行列の対角線の要素の単純和)値を返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 4], shape=[4, 4]) result = tf.diag_part(x) with tf.Session() as sess: print(sess.run(result)) # result # 10
tf.transpose(x)
与えられた入力の転置行列を返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 0], shape=[4, 4]) result = tf.transpose(x) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0 0 4] # [0 0 3 0] # [0 2 0 0] # [1 0 0 0]]
tf.eye(x, num_columns=y)
行(横)がx行、列(縦)がy列の単位行列を返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant(4) result = tf.eye(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 0. 0. 0.] # [0. 1. 0. 0.] # [0. 0. 1. 0.] # [0. 0. 0. 1.]]
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant(4) result = tf.eye(x, num_columns=5) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 0. 0. 0. 0.] # [0. 1. 0. 0. 0.] # [0. 0. 1. 0. 0.] # [0. 0. 0. 1. 0.]]
tf.matrix_diag(x)
入力されたテンソルをdiagonal分解して新たなテンソルを生成します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 4], shape=[4, 4]) result = tf.matrix_diag(x) with tf.Session() as sess: print(sess.run(result)) # result # [[[1 0 0 0] # [0 0 0 0] # [0 0 0 0] # [0 0 0 0]] # # [[0 0 0 0] # [0 2 0 0] # [0 0 0 0] # [0 0 0 0]] # # [[0 0 0 0] # [0 0 0 0] # [0 0 3 0] # [0 0 0 0]] # # [[0 0 0 0] # [0 0 0 0] # [0 0 0 0] # [0 0 0 4]]]
tf.matrix_diag_part(x)
入力されたテンソルの対角線要素を取り出して新たなテンソルを生成します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 4], shape=[4, 4]) result = tf.matrix_diag_part(x) with tf.Session() as sess: print(sess.run(result)) # result # [1 2 3 4]
tf.matrix_band_part(x, y, z)
入力されたテンソルにつき、それぞれの最も内側の行列の中央のバンドの外側にあるすべてのものをゼロに設定するテンソルをコピーします。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 4], shape=[4, 4]) y = tf.constant(1) z = tf.constant(-1) result = tf.matrix_band_part(x, y, z) with tf.Session() as sess: print(sess.run(result)) # result # [[1 0 0 0] # [0 2 0 0] # [0 0 3 0] # [0 0 0 4]]
tf.matrix_set_diag(x, y)
入力に対して、新しいバッチ対角値を持つバッチ行列テンソルを返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0]]) y = tf.constant([1.0, 2.0, 3.0]) result = tf.matrix_set_diag(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 1. 0.] # [1. 2. 1.] # [1. 1. 3.]]
tf.matrix_transpose(x)
入力されたテンソルに対し、転置テンソルを生成します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0], shape=[4, 4]) result = tf.matrix_transpose(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 5. 8. 4.] # [2. 6. 7. 3.] # [3. 7. 6. 2.] # [4. 8. 5. 1.]]
tf.matmul(x, y)
入力されたテンソルxとyに対し、内積を計算し新たなテンソルを計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0], shape=[4, 4]) y = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0], shape=[4, 4]) result = tf.matmul(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[ 51. 47. 43. 39.] # [123. 119. 115. 111.] # [111. 115. 119. 123.] # [ 39. 43. 47. 51.]]
tf.norm(x)
入力されたテンソルにつきノルムを計算する。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0], shape=[4, 4]) result = tf.norm(x) with tf.Session() as sess: print(sess.run(result)) # result # 20.199009
tf.matrix_determinant(x)
テンソルの行列式を計算して返す。
# -*- coding: utf-8 -*- import tensorflow as tf #x = tf.constant(4) x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], shape=[3, 3]) result = tf.matrix_determinant(x) with tf.Session() as sess: print(sess.run(result)) # result # 0
tf.matrix_inverse(x)
テンソルの逆行列を計算して返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 9.0, 9.0], shape=[3, 3]) result = tf.matrix_inverse(x) with tf.Session() as sess: print(sess.run(result)) # result # [[-1.4999995 1.4999998 -0.49999988] # [ 0.99999917 -1.9999994 0.9999998 ] # [ 0.16666709 0.83333296 -0.49999988]]
tf.cholesky(x)
入力されたテンソルに対してコレスキー分解を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) result = tf.cholesky(x) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 0. 0.] # [0. 1. 0.] # [0. 0. 1.]]
tf.cholesky_solve(x, y)
入力されたテンソルに対して不完全コレスキー分解を計算します
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) y = tf.constant([1.0, 2.0, 0.0, 0.0, 0.0, 1.0], shape=[3, 2]) result = tf.cholesky_solve(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 2.] # [0. 0.] # [0. 1.]]
tf.matrix_solve(x, y)
入力されたテンソルに対して行列式が示す連立方程式の解を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) y = tf.constant([1.0, 2.0, 0.0, 0.0, 0.0, 1.0], shape=[3, 2]) result = tf.matrix_solve(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 2.] # [0. 0.] # [0. 1.]]
tf.matrix_triangular_solve(x, y)
逆置換によって、入力されたテンソルに対して上三角行列または下三角行列を持つ線形方程式のシステムを解きます。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) y = tf.constant([1.0, 2.0, 0.0, 0.0, 0.0, 1.0], shape=[3, 2]) result = tf.matrix_triangular_solve(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 2.] # [0. 0.] # [0. 1.]]
tf.matrix_solve_ls(x, y)
1つ以上の線形最小二乗問題を解く。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) y = tf.constant([1.0, 2.0, 0.0, 0.0, 0.0, 1.0], shape=[3, 2]) result = tf.matrix_solve_ls(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[ 1. 6.] # [ 0. -2.] # [ 0. 1.]]
tf.qr(x)
1つまたは複数の行列のQR分解を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) result = tf.qr(x) with tf.Session() as sess: print(sess.run(result)) # result # Qr(q=array([[1., 0., 0.], # [0., 1., 0.], # [0., 0., 1.]], dtype=float32), r=array([[1., 2., 0.], # [0., 1., 2.], # [0., 0., 1.]], dtype=float32))
tf.self_adjoint_eig(x)
自己随伴行列のバッチの固有分解を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) result = tf.self_adjoint_eig(x) with tf.Session() as sess: print(sess.run(result)) # result # (array([1., 1., 1.], dtype=float32), array([[1., 0., 0.], # [0., 1., 0.], # [0., 0., 1.]], dtype=float32))
tf.self_adjoint_eigvals(x)
1つまたは複数の自己随伴行列の固有値を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) result = tf.self_adjoint_eig(x) with tf.Session() as sess: print(sess.run(result)) # result # [1. 1. 1.]
tf.svd(x)
1つまたは複数の行列の特異値分解を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) result = tf.svd(x) with tf.Session() as sess: print(sess.run(result)) # result # (array([2.709276 , 1.9032122 , 0.19393659], dtype=float32), array([[ 0.63178134, -0.7557893 , 0.17214787], # [ 0.7392388 , 0.5206574 , -0.42713234], # [ 0.23319198, 0.39711258, 0.8876505 ]], dtype=float32), array([[ 0.233192 , -0.39711252, 0.8876505 ], # [ 0.73923886, -0.52065736, -0.42713228], # [ 0.63178134, 0.75578946, 0.17214787]], dtype=float32))
テンソル関数(Tensor Math Function)
TensorFlowは、グラフにテンソル関数を追加するために使用できる操作を提供します。
tf.tensordot(x, y, z)
指定軸に沿ったaとbのテンソル収縮。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) y = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) z = tf.constant([[1], [0]]) result = tf.tensordot(x, y, axes = z) with tf.Session() as sess: print(sess.run(result)) # result # [[1. 4. 4.] # [0. 1. 4.] # [0. 0. 1.]]
複素数関数(Complex Number Functions)
TensorFlowは、複素数関数をグラフに追加するために使用できるいくつかの演算を提供します。
tf.complex(x, y)
入力となる2つのテンソルx,yに対して x+yj となる虚数を要素に持つテンソルを返す(jは虚数単位)。
# -*- coding: utf-8 -*- import tensorflow as tf #x = tf.constant(4) x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) y = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) result = tf.complex(x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[1.+1.j 2.+2.j 0.+0.j] # [0.+0.j 1.+1.j 2.+2.j] # [0.+0.j 0.+0.j 1.+1.j]]
tf.conj(x)
入力されたテンソルの各要素を複素数の複素共役(虚数部分の符号が反転)に変えて返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([-2.25 + 4.75j, 3.25 + 5.75j, -2.25 + 4.75j, 3.25 + 5.75j], shape=[2, 2]) result = tf.conj(x) with tf.Session() as sess: print(sess.run(result)) # result # [[-2.25-4.75j 3.25-5.75j] # [-2.25-4.75j 3.25-5.75j]]
tf.imag(x)
入力されたテンソルの各要素につき虚数部分(例 3+5j なら 5)を要素とするテンソルを返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([-2.25 + 4.75j, 3.25 + 5.75j, -2.25 + 4.75j, 3.25 + 5.75j], shape=[2, 2]) result = tf.imag(x) with tf.Session() as sess: print(sess.run(result)) # result # [[4.75 5.75] # [4.75 5.75]]
tf.angle(x)
入力されたテンソルの各要素につきその複素数空間での偏角(正の実数軸とその複素ベクトルがなす角度)を要素とするテンソルを返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([-2.25 + 4.75j, 3.25 + 5.75j], shape=[1, 2]) result = tf.imag(x) with tf.Session() as sess: print(sess.run(result)) # result # [[2.01317055 1.05634501]]
tf.real(x)
入力されたテンソルの各要素につき実数部分(例 3+5j なら 3)を要素とするテンソルを返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([-2.25 + 4.75j, 3.25 + 5.75j], shape=[1, 2]) result = tf.real(x) with tf.Session() as sess: print(sess.run(result)) # result # [[-2.25 3.25]]
リダクション(Reduction)
TensorFlowは、テンソルのさまざまな次元を減らす一般的な数学計算を実行するために使用できるいくつかの演算を提供します。
tf.reduce_sum(x)
入力されたテンソルの各要素を足し上げた数値を計算して返します。(廃止予定の関数です)
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1.0, 2.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0], shape=[3, 3]) result = tf.reduce_sum(x) with tf.Session() as sess: print(sess.run(result)) # result # 7.0
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[1.0, 2.0, 0.0], [0.0, 1.0, 2.0], [0.0, 0.0, 1.0]], shape=[1, 3, 3]) result = tf.reduce_sum(x) with tf.Session() as sess: print(sess.run(result)) # result # 7.0
tf.reduce_prod(x)
入力されたテンソルの次元間の要素の積を計算します。(廃止予定の関数)
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[1.0, 2.0, 3.0], [2.0, 1.0, 2.0], [1.0, 3.0, 1.0]], shape=[1, 3, 3]) result = tf.reduce_prod(x) with tf.Session() as sess: print(sess.run(result)) # result # 72.0
tf.reduce_min(x)
入力されたテンソルの次元間の要素の最小値を計算します。(廃止予定の関数)
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[1.0, 2.0, 3.0], [2.0, 1.0, 2.0], [1.0, 3.0, 1.0]], shape=[1, 3, 3]) result = tf.reduce_min(x) with tf.Session() as sess: print(sess.run(result)) # result # 1.0
tf.reduce_max(x)
入力されたテンソルの次元間の要素の最大値を計算します。(廃止予定の関数)
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[1.0, 2.0, 3.0], [2.0, 1.0, 2.0], [1.0, 3.0, 1.0]], shape=[1, 3, 3]) result = tf.reduce_max(x) with tf.Session() as sess: print(sess.run(result)) # result # 3.0
tf.reduce_mean(x)
入力されたテンソルの次元間の要素の平均値を計算します。(廃止予定の関数)
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[1.0, 2.0, 3.0], [2.0, 1.0, 2.0], [1.0, 3.0, 1.0]], shape=[1, 3, 3]) result = tf.reduce_mean(x) with tf.Session() as sess: print(sess.run(result)) # result # 1.7777778
tf.reduce_all(x)
テンソルの次元にわたって要素の「論理積」を計算します(つまり、要素に1つでもFalseがあれば計算結果はFalse)。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[True, False, False], [True, True, False], [False, True, True]], shape=[1, 3, 3]) result = tf.reduce_all(x) with tf.Session() as sess: print(sess.run(result)) # result # False
tf.reduce_any(x)
テンソルの次元にわたって要素の「論理和」を計算します(つまり、要素に1つでもTrueがあれば計算結果はTrue)。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[True, False, False], [True, True, False], [False, True, True]], shape=[1, 3, 3]) result = tf.reduce_any(x) with tf.Session() as sess: print(sess.run(result)) # result # True
tf.reduce_logsumexp(x)
log(sum(exp(テンソルの次元間の各要素)))を計算します。(廃止予定の関数)
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[1.0, 0.0, 0.0], [2.0, 1.0, 0.0], [0.0, 3.0, 1.0]], shape=[1, 3, 3]) result = tf.reduce_logsumexp(x) with tf.Session() as sess: print(sess.run(result)) # result # 3.679572
tf.count_nonzero(x)
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[1.0, 0.0, 0.0], [2.0, 1.0, 0.0], [0.0, 3.0, 1.0]], shape=[1, 3, 3]) result = tf.count_nonzero(x) with tf.Session() as sess: print(sess.run(result)) # result # 5
tf.accumulate_n(x, y, …)
配列形式で入力された全てのテンソルの対応する各要素を加えたテンソルを計算し返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([[1.0, 0.0, 0.0], [2.0, 1.0, 0.0], [0.0, 3.0, 1.0]], shape=[1, 3, 3]) y = tf.constant([[2.0, 1.0, 0.0], [2.0, 1.0, 0.0], [1.0, 1.0, 0.0]], shape=[1, 3, 3]) result = tf.accumulate_n([x, y]) with tf.Session() as sess: print(sess.run(result)) # result # [[[3. 1. 0.] # [4. 2. 0.] # [1. 4. 1.]]]
tf.einsum(equation, x, y)
任意の次元のテンソル間の一般化された収縮を行います。
# -*- coding: utf-8 -*- import tensorflow as tf equation = 'ij,jk->ik' x = tf.constant([2, 1, 1, 2], shape=[2, 2]) y = tf.constant([1, 3, 1, 2], shape=[2, 2]) result = tf.einsum(equation, x, y) with tf.Session() as sess: print(sess.run(result)) # result # [[3 8] # [3 7]]
スキャン(Scan)
TensorFlowは、テンソルの1つの軸をスキャンする(合計を実行する)ために使用できるいくつかの操作を提供します。
tf.cumsum(x)
軸に沿ったテンソルxの累積合計を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2, 1, 3, 1, 2, 3, 3, 2, 1], shape=[3, 3]) result = tf.cumsum(x) with tf.Session() as sess: print(sess.run(result)) # result # [[2 1 3] # [3 3 6] # [6 5 7]]
tf.cumprod(x)
軸に沿ったテンソルxの累積積を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2, 1, 3, 1, 2, 3, 3, 2, 1], shape=[3, 3]) result = tf.cumprod(x) with tf.Session() as sess: print(sess.run(result)) # result # [[2 1 3] # [2 2 9] # [6 4 9]]
セグメンテーション(Segmentation)
TensorFlowは、テンソルセグメントで一般的な数学計算を実行するために使用できるいくつかの演算を提供します。 ここで、セグメント化は、第1の次元に沿ったテンソルの分割であり、すなわち、第1の次元からsegment_idへのマッピングを定義します。 segment_idsテンソルは、連続したIDが0〜kの範囲にある最初の次元のサイズd0でなければなりません。ここで、k < d0です。 特に、行列テンソルのセグメント化は、行とセグメントとのマッピングです。
例:
c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
tf.segment_sum(c, tf.constant([0, 0, 1]))
==> [[0 0 0 0]
[5 6 7 8]]
tf.segment_sum(x, segment_ids)
segment_idsで指定したidごとにxの要素を足し合わせた合計を要素とするテンソルを計算し返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2, 1, 3, 1, 2, 3, 3, 2, 1]) segment_ids = tf.constant([0, 0, 1, 1, 2, 2, 2, 3, 3]) result = tf.segment_sum(x, segment_ids) with tf.Session() as sess: print(sess.run(result)) # result # [3 4 8 3]
tf.segment_prod(x, segment_ids)
segment_idsで指定したidごとにxの要素掛け合わせた合計を要素とするテンソルを計算し返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2, 1, 3, 1, 2, 3, 3, 2, 1]) segment_ids = tf.constant([0, 0, 1, 1, 2, 2, 2, 3, 3]) result = tf.segment_prod(x, segment_ids) with tf.Session() as sess: print(sess.run(result)) # result # [ 2 3 18 2]
tf.segment_min(x, segment_ids)
segment_idsで指定したidごとにxの要素の最小値を要素とするテンソルを計算し返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2, 1, 3, 1, 2, 3, 3, 2, 1]) segment_ids = tf.constant([0, 0, 1, 1, 2, 2, 2, 3, 3]) result = tf.segment_min(x, segment_ids) with tf.Session() as sess: print(sess.run(result)) # result # [1 1 2 1]
tf.segment_max(x, segment_ids)
segment_idsで指定したidごとにxの要素の最大値を要素とするテンソルを計算し返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2, 1, 3, 1, 2, 3, 3, 2, 1]) segment_ids = tf.constant([0, 0, 1, 1, 2, 2, 2, 3, 3]) result = tf.segment_min(x, segment_ids) with tf.Session() as sess: print(sess.run(result)) # result # [2 3 3 2]
tf.segment_mean(x, segment_ids)
segment_idsで指定したidごとにxの要素の平均値を要素とするテンソルを計算し返す。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2., 1., 3., 1., 2., 3., 3., 2., 1.]) segment_ids = tf.constant([0, 0, 1, 1, 2, 2, 2, 3, 3]) result = tf.segment_mean(x, segment_ids) with tf.Session() as sess: print(sess.run(result)) # result # [1.5 2. 2.6666667 1.5 ]
tf.unsorted_segment_sum(x, segment_ids, num_segments)
segment_idsがソートされていない状態の時のsegment_sumを演算する。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2., 1., 3., 1., 2., 3., 3., 2., 1.]) segment_ids = tf.constant([0, 0, 1, 1, 2, 2, 2, 3, 2]) num_segments = tf.constant(4) # y(=segment_ids)が0から3までの4つあるので4 result = tf.unsorted_segment_sum(x, segment_ids, num_segments) with tf.Session() as sess: print(sess.run(result)) # result # [3. 4. 9. 2.]
tf.sparse_segment_sum(x, indices, segment_ids)
テンソルの疎セグメントに沿った合計を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2., 1., 3., 1., 2., 3., 3., 2., 1.], shape=[3, 3]) indices = tf.constant([0, 1]) segment_ids = tf.constant([0, 0]) result = tf.sparse_segment_sum(x, indices, segment_ids) with tf.Session() as sess: print(sess.run(result)) # result # [[3. 3. 6.]]
tf.sparse_segment_mean(x, indices, segment_ids)
テンソルの疎セグメントに沿った平均を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2., 1., 3., 1., 2., 3., 3., 2., 1.], shape=[3, 3]) indices = tf.constant([0, 1]) segment_ids = tf.constant([0, 0]) result = tf.sparse_segment_sum(x, indices, segment_ids) with tf.Session() as sess: print(sess.run(result)) # result # [[1.5 1.5 3. ]][[3. 3. 6.]]
tf.sparse_segment_sqrt_n(x, indices, segment_ids)
テンソルの疎セグメントに沿った和をsqrt(N)で割って計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2., 1., 3., 1., 2., 3., 3., 2., 1.], shape=[3, 3]) indices = tf.constant([0, 1]) segment_ids = tf.constant([0, 0]) result = tf.sparse_segment_sqrt_n(x, indices, segment_ids) with tf.Session() as sess: print(sess.run(result)) # result # [[2.1213205 2.1213205 4.242641 ]]
シーケンスの比較と索引付け(Sequence Comparison and Indexing)
TensorFlowは、シーケンス比較とインデックス抽出をグラフに追加するために使用できるいくつかの操作を提供します。 これらの操作を使用してシーケンスの差異を判断し、テンソル内の特定の値のインデックスを決定することができます。
tf.argmin(x)
テンソルの軸間で最小の値を持つインデックスを返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2., 1., 3., 1., 2., 3., 3., 2., 1.], shape=[3, 3]) result = tf.argmin(x) with tf.Session() as sess: print(sess.run(result)) # result # [1 0 2]
tf.argmax(x)
テンソルの軸間で最大の値を持つインデックスを返します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([2., 1., 3., 1., 2., 3., 3., 2., 1.], shape=[3, 3]) result = tf.argmax(x) with tf.Session() as sess: print(sess.run(result)) # result # [2 1 0]
tf.setdiff1d(x, y)
数値または文字列の2つのリストの差を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4, 5, 6]) y = tf.constant([1, 3]) result = tf.setdiff1d(x, y) with tf.Session() as sess: print(sess.run(result)) # result # ListDiff(out=array([2, 4, 5, 6], dtype=int32), idx=array([1, 3, 4, 5], dtype=int32))
tf.where(condition)
条件に応じてxまたはyのいずれかの要素を返します。
# -*- coding: utf-8 -*- import tensorflow as tf condition = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9], shape=[3, 3]) result = tf.where(condition) with tf.Session() as sess: print(sess.run(result)) # result # [[0 0] # [0 1] # [0 2] # [1 0] # [1 1] # [1 2] # [2 0] # [2 1] # [2 2]]
tf.unique(x)
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([1, 2, 3, 4, 3, 2, 4, 8, 9]) result = tf.unique(x) with tf.Session() as sess: print(sess.run(result)) # result # Unique(y=array([1, 2, 3, 4, 8, 9], dtype=int32), idx=array([0, 1, 2, 3, 2, 1, 3, 4, 5], dtype=int32))
tf.edit_distance(hypothesis, truth)
シーケンス間のLevenshtein距離を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf hypothesis = tf.SparseTensor( [[0, 0, 0], [1, 0, 0]], ["a", "b"], (2, 1, 1)) truth = tf.SparseTensor( [[0, 1, 0], [1, 0, 0], [1, 0, 1], [1, 1, 0]], ["a", "b", "c", "a"], (2, 2, 2)) result = tf.edit_distance(hypothesis, truth) with tf.Session() as sess: print(sess.run(result)) # result # [[inf 1. ] # [0.5 1. ]]
tf.invert_permutation(x)
テンソルの逆転置を計算します。
# -*- coding: utf-8 -*- import tensorflow as tf x = tf.constant([3, 4, 0, 2, 1]) result = tf.invert_permutation(x) with tf.Session() as sess: print(sess.run(result)) # result # [2 4 3 0 1]