47
52

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TensorFlow入門 - 四則演算と基礎的な数学関数まとめ

Posted at

TensorFlowで主に使う数学関連の関数をまとめてみました。

#Arithmetic Operators(四則演算)

関数 役割
tf.add(x, y, name=None) 要素ごとの和
tf.sub(x, y, name=None) 要素ごとの差
tf.mul(x, y, name=None) 要素ごとの積
tf.div(x, y, name=None) 要素ごとの商 
※テンソルの数値型がint等の浮動小数でない型である場合、小数点以下切り捨て
tf.truediv(x, y, name=None) 要素ごとの商
※テンソルの数値型がint等の浮動小数でない型である場合、先に浮動小数点型に変換する
tf.floordiv(x, y, name=None) 要素ごとの商
※テンソルの数値型が浮動小数点型の場合、結果の小数点以下切り捨て
tf.mod(x, y, name=None) 要素ごとの剰余

使用例)

vim arithmetic_operators.py

import tensorflow as tf

def add(j, k):
   _j = tf.constant(j)
   _k = tf.constant(k)
   result = tf.add(_j, _k)
   return result

def sub(j,k):
   _j = tf.constant(j)
   _k = tf.constant(k)
   result = tf.sub(_j,_k)
   return result

def mul(j,k):
   _j = tf.constant(j)
   _k = tf.constant(k)
   result = tf.mul(_j,_k)
   return result

def mod(j,k):
   _j = tf.constant(j)
   _k = tf.constant(k)
   result = tf.mod(_j,_k)
   return result

def div(j,k):
   _j = tf.constant(j)
   _k = tf.constant(k)
   result = tf.div(_j,_k)
   return result


with tf.Session() as sess:
     result = sess.run([mod(10,3)]) #10 %3 = 1
     result2 = sess.run([mul(5,4)]) #5 x 4 = 20
     result3 = sess.run([sub(10,6)]) #10 - 6 = 4
     result4 = sess.run([add(5,6)]) #5 + 6 =11
     result5 = sess.run([div(11.,7.)]) #11 / 7 = 1.5714285
     print result
     print result2
     print result3
     print result4
     print result5

結果

python arithmetic_operators.py
[1]
[20]
[4]
[11]
[1.5714285]

#Basic Math Functions(基礎的な数学関数)

関数 役割
tf.add_n(inputs, name=None) 要素ごとの和 
※inputsはテンソルのリスト、全てが同じサイズをもつ必要あり
tf.abs(x, name=None) 要素ごとの絶対値
tf.neg(x, name=None) 要素ごとにマイナスをかける
tf.sign(x, name=None) 要素ごとに正なら1、0なら0、負なら-1となる変換をかける
tf.inv(x, name=None) 要素ごとの逆数
tf.square(x, name=None) 要素ごとに二乗をとる
tf.round(x, name=None) 要素ごとに四捨五入
tf.sqrt(x, name=None) 要素ごとにルートをとる
tf.rsqrt(x, name=None) 要素ごとにルートの逆数を取る
tf.pow(x, y, name=None) 要素ごとに累乗(xの要素^yの要素)
tf.exp(x, name=None) 要素ごとに自然数を底とする指数関数をとる
tf.log(x, name=None) 要素ごとに自然対数をとる
tf.ceil(x, name=None) 要素ごとに小数点以下繰り上げ
tf.floor(x, name=None) 要素ごとに小数点以下切り捨て
tf.maximum(x, y, name=None) 要素ごとに最大値をとる
tf.minimum(x, y, name=None) 要素ごとに最小値をとる
tf.cos(x, name=None) 要素ごとにcosをとる
tf.sin(x, name=None) 要素ごとにsinをとる

squareを利用した例で、以下の式を利用します。

y=x2+b

使用例)
vim square_test.py

import tensorflow as tf

def x2_plus_b(x, b):
    _x = tf.constant(x)
    _b = tf.constant(b)
    result = tf.square(_x)
    result = tf.add(result, _b)
    return result

with tf.Session() as sess:
    result = sess.run([x2_plus_b(2.0,3.0)])
    print result

結果

python square_test.py

[7.0]

その他数学関数はこちら
のブログを参考しました。

vim basic_math_fun.py

import tensorflow as tf

sess = tf.InteractiveSession()

################
# tf.add_n
################
a = tf.constant([1., 2.])
b = tf.constant([3., 4.])
c = tf.constant([5., 6.])
tf_addn = tf.add_n([a, b, c])
print "tf.add_n"
print sess.run(tf_addn)

# output:

# tf.add_n
# [  9.  12.]

################
# tf.abs
################
x = tf.constant([[-1., 2.], [3., -4.]])
tf_abs = tf.abs(x)
print "tf.abs"
print sess.run(tf_abs)

# output:

# tf.abs
# [[ 1.  2.]
#  [ 3.  4.]]

################
# tf.neg
################
x = tf.constant([[-1., 2.], [3., -4.]])
tf_neg = tf.neg(x)
print "tf.neg"
print sess.run(tf_neg)

# output:

# tf.neg
# [[ 1. -2.]
#  [-3.  4.]]

################
# tf.sign
################
x = tf.constant([[-1., 2.], [3., -4.]])
tf_sign = tf.sign(x)
print "tf.sign"
print sess.run(tf_sign)

# output:

# tf.sign
# [[-1.  1.]
#  [ 1. -1.]]

################
# tf.inv
################
x = tf.constant([[-1., 2.], [3., -4.]])
tf_inv = tf.inv(x)
print "tf.inv"
print sess.run(tf_inv)

# output:

# tf.inv
# [[-1.          0.5       ]
#  [ 0.33333334 -0.25      ]]

################
# tf.square
################
x = tf.constant([[-1., 2.], [3., -4.]])
tf_square = tf.square(x)
print "tf.square"
print sess.run(tf_square)

# output:

# tf.square
# [[  1.   4.]
#  [  9.  16.]]

################
# tf.round
################
x = tf.constant([0.9, 2.5, 2.3, -4.4])
tf_round = tf.round(x)
print "tf.round"
print sess.run(tf_round)

# output:

# tf.round
# [ 1.  3.  2. -4.]

################
# tf.sqrt
################
x = tf.constant([[1., 2.], [3., 4.]])
tf_sqrt = tf.sqrt(x)
print "tf.sqrt"
print sess.run(tf_sqrt)

# output:

# tf.sqrt
# [[ 0.99999994  1.41421342]
#  [ 1.73205078  1.99999988]]

################
# tf.rsqrt
################
x = tf.constant([[1., 2.], [3., 4.]])
tf_rsqrt = tf.rsqrt(x)
print "tf.rsqrt"
print sess.run(tf_rsqrt)

# output:

# tf.rsqrt
# [[ 0.99999994  0.70710671]
# [ 0.57735026  0.49999997]]

################
# tf.pow
################
x = tf.constant([[2, 2], [3, 3]])
y = tf.constant([[8, 16], [2, 3]])
tf_pow = tf.pow(x, y)
print "tf.pow"
print sess.run(tf_pow)

# output:

# tf.pow
# [[  256 65536]
#  [    9    27]]

################
# tf.exp
################
x = tf.constant([[1., 2.], [3., 4.]])
tf_exp = tf.exp(x)
print "tf.exp"
print sess.run(tf_exp)

# output:

# tf.exp
# [[  2.71828175   7.38905621]
#  [ 20.08553696  54.59815216]]

################
# tf.log
################
x = tf.constant([[1., 2.], [3., 4.]])
tf_log = tf.log(x)
print "tf.log"
print sess.run(tf_log)

# output:

# tf.log
# [[ 0.          0.69314718]
#  [ 1.09861231  1.38629436]]

################
# tf.ceil
################
x = tf.constant([[1.1, 2.2], [3.3, 4.4]])
tf_ceil = tf.ceil(x)
print "tf.ceil"
print sess.run(tf_ceil)

# output:

# tf.ceil
# [[ 2.  3.]
#  [ 4.  5.]]

################
# tf.floor
################
x = tf.constant([[1.1, 2.2], [3.3, 4.4]])
tf_floor = tf.floor(x)
print "tf.floor"
print sess.run(tf_floor)

# output:

# tf.floor
# [[ 1.  2.]
#  [ 3.  4.]]

################
# tf.maximum
################
x = tf.constant([[2, 8], [3, 12]])
y = tf.constant([[4, 10], [1, 9]])
tf_maximum = tf.maximum(x, y)
print "tf.maximum"
print sess.run(tf_maximum)

# output:

# tf.maximum
# [[ 4 10]
#  [ 3 12]]

################
# tf.minimum
################
x = tf.constant([[2, 8], [3, 12]])
y = tf.constant([[4, 10], [1, 9]])
tf_minimum = tf.minimum(x, y)
print "tf.minimum"
print sess.run(tf_minimum)

# output:

# tf.minimum
# [[2 8]
#  [1 9]]

################
# tf.cos
################
x = tf.constant([[2., 8.], [3., 12.]])
tf_cos = tf.cos(x)
print "tf.cos"
print sess.run(tf_cos)

# output:

# tf.cos
# [[-0.41614681 -0.14550003]
#  [-0.9899925   0.84385395]]

################
# tf.sin
################
x = tf.constant([[2., 8.], [3., 12.]])
tf_sin = tf.sin(x)
print "tf.sin"
print sess.run(tf_sin)

# output:

# tf.sin
# [[ 0.90929741  0.98935825]
#  [ 0.14112    -0.53657293]]

sess.close()

参考:
https://www.tensorflow.org/versions/r0.9/api_docs/python/math_ops.html
http://dev.classmethod.jp/machine-learning/tensorflow-math/
http://mirai-tec.hatenablog.com/entry/2016/02/22/001459

47
52
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
52

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?