TensorFlow

【TensorFlow】Tensorを用いた数値計算

【TensorFlow】Tensorを用いた数値計算

Googleによって開発されている機械学習ライブラリであるTensorFlowで、Tensorを用いた数値計算をする方法を紹介します。

Tensorを用いた数値計算

数値計算

TensorFlowで実装されている代表的な数値計算は以下のようなものがあります。

演算子関数名説明
+tf.add加算(例:1 + 1 = 2)
tf.subtract減算(例:2 – 1 = 1)
*tf.multiply乗算(例:2 * 5 = 10)
/tf.divide除算(例:5 / 2 = 2.5)
**tf.pow累乗(例:2 ** 3 = 8)
//tf.math.floordiv切り捨て除算(例:5 // 2 = 2)
%tf.math.mod剰余(例:5 % 2 = 1)
tf.negativeマイナス(例:-2)

上記の演算は、Tensorとしてxとyがあった場合に「x+y」等のように演算子を使って直感的に使用することができます。また、それぞれの演算子と関数が紐づいているため、tf.add(x, y)のように使用して構いません。

なお、演算では、要素同士で計算が実行され、サイズが異なる場合はNumPyのブロードキャストのような動作をします。NumPyのブロードキャストの考え方については「ブロードキャスト(broadcast)の基本」でまとめているので興味があれば参考にしてください。

以降では、上記演算に関して簡単なプログラム例を紹介します。

加算 tf.add

Tensorの加算をするには、以下のようにtf.addまたは+演算子を用います。

import tensorflow as tf

a = tf.constant([1, 2, 3, 4, 5])
b = tf.constant([6, 7, 8, 9, 10])
c = tf.constant(2)

# 関数で計算
print(tf.add(a, b))
print(tf.add(a, c), "\n")

# 演算子で計算
print(a + b)
print(a + c)
【実行結果】
tf.Tensor([ 7  9 11 13 15], shape=(5,), dtype=int32)
tf.Tensor([3 4 5 6 7], shape=(5,), dtype=int32) 

tf.Tensor([ 7  9 11 13 15], shape=(5,), dtype=int32)
tf.Tensor([3 4 5 6 7], shape=(5,), dtype=int32)

減算 tf.subtract

Tensorの減算をするには、以下のようにtf.subtractまたは-演算子を用います。

import tensorflow as tf

a = tf.constant([1, 2, 3, 4, 5])
b = tf.constant([6, 7, 8, 9, 10])
c = tf.constant(2)

# 関数で計算
print(tf.subtract(a, b))
print(tf.subtract(a, c), "\n")

# 演算子で計算
print(a - b)
print(a - c)
【実行結果】
tf.Tensor([-5 -5 -5 -5 -5], shape=(5,), dtype=int32)
tf.Tensor([-1  0  1  2  3], shape=(5,), dtype=int32) 

tf.Tensor([-5 -5 -5 -5 -5], shape=(5,), dtype=int32)
tf.Tensor([-1  0  1  2  3], shape=(5,), dtype=int32)

乗算 tf.multiply

Tensorの乗算をするには、以下のようにtf.multiplyまたは*演算子を用います。

import tensorflow as tf

a = tf.constant([1, 2, 3, 4, 5])
b = tf.constant([6, 7, 8, 9, 10])
c = tf.constant(2)

# 関数で計算
print(tf.multiply(a, b))
print(tf.multiply(a, c), "\n")

# 演算子で計算
print(a * b)
print(a * c)
【実行結果】
tf.Tensor([ 6 14 24 36 50], shape=(5,), dtype=int32)
tf.Tensor([ 2  4  6  8 10], shape=(5,), dtype=int32) 

tf.Tensor([ 6 14 24 36 50], shape=(5,), dtype=int32)
tf.Tensor([ 2  4  6  8 10], shape=(5,), dtype=int32)

除算 tf.divide

Tensorの除算をするには、以下のようにtf.divideまたは/演算子を用います。

import tensorflow as tf

a = tf.constant([1, 2, 3, 4, 5])
b = tf.constant([6, 7, 8, 9, 10])
c = tf.constant(2)

# 関数で計算
print(tf.divide(a, b))
print(tf.divide(a, c), "\n")

# 演算子で計算
print(a / b)
print(a / c)
【実行結果】
tf.Tensor([0.16666667 0.28571429 0.375      0.44444444 0.5       ], shape=(5,), dtype=float64)
tf.Tensor([0.5 1.  1.5 2.  2.5], shape=(5,), dtype=float64) 

tf.Tensor([0.16666667 0.28571429 0.375      0.44444444 0.5       ], shape=(5,), dtype=float64)
tf.Tensor([0.5 1.  1.5 2.  2.5], shape=(5,), dtype=float64)

累乗 tf.pow

Tensorの累乗をするには、以下のようにtf.powまたは**演算子を用います。

import tensorflow as tf

a = tf.constant([1, 2, 3, 4, 5])
b = tf.constant([6, 7, 8, 9, 10])
c = tf.constant(2)

# 関数で計算
print(tf.pow(a, b))
print(tf.pow(a, c), "\n")

# 演算子で計算
print(a**b)
print(a**c)
【実行結果】
tf.Tensor([      1     128    6561  262144 9765625], shape=(5,), dtype=int32)
tf.Tensor([ 1  4  9 16 25], shape=(5,), dtype=int32) 

tf.Tensor([      1     128    6561  262144 9765625], shape=(5,), dtype=int32)
tf.Tensor([ 1  4  9 16 25], shape=(5,), dtype=int32)

切り捨て除算 tf.math.floordiv

Tensorの切り捨て除算をするには、以下のようにtf.math.floordivまたは//演算子を用います。

import tensorflow as tf

a = tf.constant([6, 7, 8, 9, 10])
b = tf.constant([1, 2, 3, 4, 5])
c = tf.constant(2)

# 関数で計算
print(tf.math.floordiv(a, b))
print(tf.math.floordiv(a, c), "\n")

# 演算子で計算
print(a // b)
print(a // c)
【実行結果】
tf.Tensor([6 3 2 2 2], shape=(5,), dtype=int32)
tf.Tensor([3 3 4 4 5], shape=(5,), dtype=int32) 

tf.Tensor([6 3 2 2 2], shape=(5,), dtype=int32)
tf.Tensor([3 3 4 4 5], shape=(5,), dtype=int32)

剰余 tf.math.mod

Tensorの剰余を求めるには、以下のようにtf.math.modまたは%演算子を用います。

import tensorflow as tf

a = tf.constant([6, 7, 8, 9, 10])
b = tf.constant([1, 2, 3, 4, 5])
c = tf.constant(2)

# 関数で計算
print(tf.math.mod(a, b))
print(tf.math.mod(a, c), "\n")

# 演算子で計算
print(a % b)
print(a % c)
【実行結果】
tf.Tensor([0 1 2 1 0], shape=(5,), dtype=int32)
tf.Tensor([0 1 0 1 0], shape=(5,), dtype=int32) 

tf.Tensor([0 1 2 1 0], shape=(5,), dtype=int32)
tf.Tensor([0 1 0 1 0], shape=(5,), dtype=int32)

マイナス tf.negative

Tensorをマイナスするには、以下のようにtf.negativeまたは-演算子を用います。

import tensorflow as tf

a = tf.constant([1, 2, 3, 4, 5])
b = tf.constant(2)

# 関数で計算
print(tf.negative(a))
print(tf.negative(b), "\n")

# 演算子で計算
print(-a)
print(-b)
【実行結果】
tf.Tensor([-1 -2 -3 -4 -5], shape=(5,), dtype=int32)
tf.Tensor(-2, shape=(), dtype=int32) 

tf.Tensor([-1 -2 -3 -4 -5], shape=(5,), dtype=int32)
tf.Tensor(-2, shape=(), dtype=int32)

他にも色々ある数学の関数

TensorFlowでは、各種数学関数がtf.mathにて実装されています。上記で紹介した数値計算はその中のごく一部で、他にも多くの数学の関数が用意されています。

tf.mathの公式ドキュメントのこちらを見ると一覧とリンクが確認できます。自分が必要としている関数がないか探すときに使用してみてください。

まとめ

Googleによって開発されている機械学習ライブラリであるTensorFlowで、Tensorを用いた数値計算をする方法を紹介しました。

今回紹介したのは加算等の最もシンプルなものですが、TensorFlowではtf.mathに様々な数学の関数が用意されています。目的にあわせて確認してうまく活用してもらえるとよいかと思います。

Pythonによるディープラーニング」はTensorFlow/Kerasの中~上級者向けの本ですが非常におすすめです。CNN, RNN, Transformer, GAN等高度なモデルも扱っており面白い&TensorFlow/Kerasの実装力をつけることができます。