Here is a summary of math-related functions that are mainly used in TensorFlow.
function | role |
---|---|
tf.add(x, y, name=None) | Sum for each element |
tf.sub(x, y, name=None) | Difference between elements |
tf.mul(x, y, name=None) | Product of each element |
tf.div(x, y, name=None) | Element-by-element quotient * If the numeric type of the tensor is a non-floating point type such as int, it is truncated after the decimal point. |
tf.truediv(x, y, name=None) | Element-by-element quotient * If the numeric type of the tensor is a non-floating point type such as int, convert it to a floating point type first. |
tf.floordiv(x, y, name=None) | Element-by-element quotient * If the numeric type of the tensor is a floating point type, the result is truncated after the decimal point. |
tf.mod(x, y, name=None) | Residue for each element |
Example of use)
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
result
python arithmetic_operators.py
[1]
[20]
[4]
[11]
[1.5714285]
function | role |
---|---|
tf.add_n(inputs, name=None) | Sum for each element * Inputs is a list of tensors, all must have the same size |
tf.abs(x, name=None) | Absolute value for each element |
tf.neg(x, name=None) | Multiply each element by minus |
tf.sign(x, name=None) | 1 for positive, 0 for 0, negative for each element-Multiply the conversion to be 1 |
tf.inv(x, name=None) | Reciprocal of each element |
tf.square(x, name=None) | Take the square for each element |
tf.round(x, name=None) | Rounded by element |
tf.sqrt(x, name=None) | Take a root for each element |
tf.rsqrt(x, name=None) | Take the reciprocal of the route for each element |
tf.pow(x, y, name=None) | Exponentiation for each element(element of x^element of y) |
tf.exp(x, name=None) | Takes an exponential function with a natural number as the base for each element |
tf.log(x, name=None) | Take the natural logarithm for each element |
tf.ceil(x, name=None) | Carry up after the decimal point for each element |
tf.floor(x, name=None) | Truncate after the decimal point for each element |
tf.maximum(x, y, name=None) | Take the maximum value for each element |
tf.minimum(x, y, name=None) | Take the minimum value for each element |
tf.cos(x, name=None) | Take cos for each element |
tf.sin(x, name=None) | Take sin for each element |
In the example using square, use the following formula.
y=x2+b
Example of use) 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
result
python square_test.py
[7.0]
Other mathematical functions are here I referred to the blog of.
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()
reference: 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
Recommended Posts