Arithmetic Operators
tf.add、tf.subtract、tf.multiply、tf.scalar_mul、tf.div、tf.divide、tf.truediv、tf.floordiv、tf.realdiv、tf.truncatediv、tf.floor_div、tf.truncatemod、tf.floormod、tf.mod、tf.cross
import tensorflow as tf
x = tf.constant([[10, 20]], dtype=tf.float32)
y = tf.constant([[3, 4]], dtype=tf.float32)
with tf.Session() as sess:
print '\ntf.add(x, y) : ', sess.run(tf.add(x, y))
print '\ntf.subtract(x, y) : ', sess.run(tf.subtract(x, y))
print '\ntf.multiply(x, y) : ', sess.run(tf.multiply(x, y))
print '\ntf.scalar_mul(9.0, x) : ', sess.run(tf.scalar_mul(9.0, x))
print '\ntf.div(x, y) : ', sess.run(tf.div(x, y))
print '\ntf.divide(x, y) : ', sess.run(tf.divide(x, y))
print '\ntf.truediv(x, y) : ', sess.run(tf.truediv(x, y))
print '\ntf.floordiv(x, y) : ', sess.run(tf.floordiv(x, y))
print '\ntf.truncatemod(x, y) : ', sess.run(tf.truncatemod(x, y))
print '\ntf.floormod(x, y) : ', sess.run(tf.floormod(x, y))
print '\ntf.mod(x, y) : ', sess.run(tf.mod(x, y))
print '\ntf.cross([[10, 20, 30]], [[1, 2, 3]]) : ', sess.run(tf.cross([[10, 20, 30]], [[1, 2, 1]]))
tf.add(x, y) : [[ 13. 24.]]
tf.subtract(x, y) : [[ 7. 16.]]
tf.multiply(x, y) : [[ 30. 80.]]
tf.scalar_mul(9.0, x) : [[ 90. 180.]]
tf.div(x, y) : [[ 3.33333325 5. ]]
tf.divide(x, y) : [[ 3.33333325 5. ]]
tf.truediv(x, y) : [[ 3.33333325 5. ]]
tf.floordiv(x, y) : [[ 3. 5.]]
tf.truncatemod(x, y) : [[ 1. 0.]]
tf.floormod(x, y) : [[ 1. 0.]]
tf.mod(x, y) : [[ 1. 0.]]
tf.cross([[10, 20, 30]], [[1, 2, 3]]) : [[-40 20 0]]
Basic Math Functions
tf.add_n、tf.abs、tf.negative、tf.sign、tf.reciprocal、tf.square、tf.round、tf.sqrt、tf.rsqrt、tf.pow、tf.exp、tf.expm1、tf.log、tf.log1p、tf.ceil、tf.floor、tf.maximum、tf.minimum、tf.cos、tf.sin、tf.lbeta、tf.tan、tf.acos、tf.asin、tf.atan、tf.cosh、tf.sinh、tf.asinh、tf.acosh、tf.atanh、tf.lgamma、tf.digamma、tf.erf、tf.erfc、tf.squared_difference、tf.rint
import tensorflow as tf
x = tf.constant([[-1.5, 0, 2]], dtype=tf.float32)
y = tf.constant([[2, 0, 1]], dtype=tf.float32)
with tf.Session() as sess:
print '\ntf.add_n([x, y]) : ', sess.run(tf.add_n([x, y]))
print '\ntf.abs(x) : ', sess.run(tf.abs(x))
print '\ntf.negative(x) : ', sess.run(tf.negative(x))
print '\ntf.sign(x) : ', sess.run(tf.sign(x))
print '\ntf.reciprocal(x) : ', sess.run(tf.reciprocal(x))
print '\ntf.square(x) : ', sess.run(tf.square(x))
print '\ntf.round(x) : ', sess.run(tf.round(x))
print '\ntf.sqrt(x) : ', sess.run(tf.sqrt(x))
print '\ntf.rsqrt(x) : ', sess.run(tf.rsqrt(x))
print '\ntf.pow(x) : ', sess.run(tf.pow(x, y))
print '\ntf.exp(x) : ', sess.run(tf.exp(x))
print '\ntf.expm1(x) : ', sess.run(tf.expm1(x))
print '\ntf.log(x) : ', sess.run(tf.log(x))
print '\ntf.log1p(x) : ', sess.run(tf.log1p(x))
print '\ntf.ceil(x) : ', sess.run(tf.ceil(x))
print '\ntf.floor(x) : ', sess.run(tf.floor(x))
print '\ntf.maximum(x) : ', sess.run(tf.maximum(x, y))
print '\ntf.minimum(x) : ', sess.run(tf.minimum(x, y))
print '\ntf.cos(x) : ', sess.run(tf.cos(x))
print '\ntf.sin(x) : ', sess.run(tf.sin(x))
print '\ntf.lbeta(x) : ', sess.run(tf.lbeta(x))
print '\ntf.tan(x) : ', sess.run(tf.tan(x))
print '\ntf.acos(x) : ', sess.run(tf.acos(x))
print '\ntf.asin(x) : ', sess.run(tf.asin(x))
print '\ntf.atan(x) : ', sess.run(tf.atan(x))
print '\ntf.cosh(x) : ', sess.run(tf.cosh(x))
print '\ntf.sinh(x) : ', sess.run(tf.sinh(x))
print '\ntf.asinh(x) : ', sess.run(tf.asinh(x))
print '\ntf.acosh(x) : ', sess.run(tf.acosh(x))
print '\ntf.atanh(x) : ', sess.run(tf.atanh(x))
print '\ntf.lgamma(x) : ', sess.run(tf.lgamma(x))
print '\ntf.digamma(x) : ', sess.run(tf.digamma(x))
print '\ntf.erf(x) : ', sess.run(tf.erf(x))
print '\ntf.erfc(x) : ', sess.run(tf.erfc(x))
print '\ntf.squared_difference(x) : ', sess.run(tf.squared_difference(x, y))
print '\ntf.rint(x) : ', sess.run(tf.rint(x))
tf.add_n([x, y]) : [[ 0.5 0. 3. ]]
tf.abs(x) : [[ 1.5 0. 2. ]]
tf.negative(x) : [[ 1.5 -0. -2. ]]
tf.sign(x) : [[-1. 0. 1.]]
tf.reciprocal(x) : [[-0.66666669 inf 0.5 ]]
tf.square(x) : [[ 2.25 0. 4. ]]
tf.round(x) : [[-2. 0. 2.]]
tf.sqrt(x) : [[ nan 0. 1.41421354]]
tf.rsqrt(x) : [[ nan inf 0.70710677]]
tf.pow(x) : [[ 2.25 1. 2. ]]
tf.exp(x) : [[ 0.22313017 1. 7.38905621]]
tf.expm1(x) : [[-0.77686983 0. 6.38905621]]
tf.log(x) : [[ nan -inf 0.69314718]]
tf.log1p(x) : [[ nan 0. 1.09861231]]
tf.ceil(x) : [[-1. 0. 2.]]
tf.floor(x) : [[-2. 0. 2.]]
tf.maximum(x) : [[ 2. 0. 2.]]
tf.minimum(x) : [[-1.5 0. 1. ]]
tf.cos(x) : [[ 0.0707372 1. -0.41614684]]
tf.sin(x) : [[-0.997495 0. 0.90929741]]
tf.lbeta(x) : [ inf]
tf.tan(x) : [[-14.1014204 0. -2.18503976]]
tf.acos(x) : [[ nan 1.57079637 nan]]
tf.asin(x) : [[ nan 0. nan]]
tf.atan(x) : [[-0.98279375 0. 1.10714877]]
tf.cosh(x) : [[ 2.3524096 1. 3.76219583]]
tf.sinh(x) : [[-2.12927938 0. 3.62686038]]
tf.asinh(x) : [[-1.19476318 0. 1.44363546]]
tf.acosh(x) : [[ nan nan 1.31695795]]
tf.atanh(x) : [[ nan 0. nan]]
tf.lgamma(x) : [[ 0.86004704 inf 0. ]]
tf.digamma(x) : [[ 0.70315683 inf 0.42278421]]
tf.erf(x) : [[-0.96610516 0. 0.99532229]]
tf.erfc(x) : [[ 1.9661051 1. 0.00467773]]
tf.squared_difference(x) : [[ 12.25 0. 1. ]]
tf.rint(x) : [[-2. 0. 2.]]
Matrix Math Functions
tf.diag、tf.diag_part、tf.trace、tf.transpose(a, perm=None)、tf.eye、tf.matrix_diag、tf.matrix_diag_part、tf.matrix_band_part、tf.matrix_set_diag、tf.matrix_transpose、tf.matmul、tf.norm、tf.matrix_determinant、tf.matrix_inverse、tf.cholesky、tf.matrix_triangular_solve、tf.matrix_solve_ls、tf.qr、tf.self_adjoint_eig、tf.self_adjoint_eigvals、tf.svd
import tensorflow as tf
x = tf.constant([[1,5,0],[0,2,0],[0,0,3]])
x_f = tf.cast(x, tf.float32)
y = tf.constant([10, 20, 30])
with tf.Session() as sess:
print '\ntf.diag(y) : \n', sess.run(tf.diag(y))
print '\ntf.diag_part(x) : \n', sess.run(tf.diag_part(x))
print '\ntf.trace(x) : \n', sess.run(tf.trace(x))
print '\ntf.transpose(x) : \n', sess.run(tf.transpose(x))
print '\ntf.eye(3) : \n', sess.run(tf.eye(3))
print '\ntf.matrix_diag([[1,2,3],[4,5,6]]) : \n', sess.run(tf.matrix_diag([[1,2,3],[4,5,6]]))
print '\ntf.matrix_diag_part([x, x]) : \n', sess.run(tf.matrix_diag_part([x, x]))
print '\ntf.matrix_band_part(x, 0, -1) : \n', sess.run(tf.matrix_band_part(x, 0, -1))
print '\ntf.matrix_band_part(x, -1, 0) : \n', sess.run(tf.matrix_band_part(x, -1, 0))
print '\ntf.matrix_band_part(x, 0, 0) : \n', sess.run(tf.matrix_band_part(x, 0, 0))
print '\ntf.matrix_set_diag(x, y) : \n', sess.run(tf.matrix_set_diag(x, y))
print '\ntf.matrix_transpose(x) : \n', sess.run(tf.matrix_transpose(x))
print '\ntf.matmul(x, x) : \n', sess.run(tf.matmul(x, x))
print '\ntf.norm(x_f) : \n', sess.run(tf.norm(x_f))
print '\ntf.matrix_determinant(x_f) : \n', sess.run(tf.matrix_determinant(x_f))
print '\ntf.matrix_inverse(x_f) : \n', sess.run(tf.matrix_inverse(x_f))
print '\ntf.cholesky(tf.constant([[2, -1], [-1, 5]], dtype=tf.float32))) : \n', sess.run(tf.cholesky(tf.constant([[2, -1], [-1, 5]], dtype=tf.float32)))
print '\ntf.qr(x_f) : \n', sess.run(tf.qr(x_f))
print '\ntf.tf.self_adjoint_eig(x_f) : \n', sess.run(tf.self_adjoint_eig(x_f))
print '\ntf.self_adjoint_eigvals([x_f, x_f]) : \n', sess.run(tf.self_adjoint_eigvals([x_f, x_f]))
print '\ntf.svd(x_f) : \n', sess.run(tf.svd(x_f))
tf.diag(y) :
[[10 0 0]
[ 0 20 0]
[ 0 0 30]]
tf.diag_part(x) :
[1 2 3]
tf.trace(x) :
6
tf.transpose(x) :
[[1 0 0]
[5 2 0]
[0 0 3]]
tf.eye(3) :
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
tf.matrix_diag([[1,2,3],[4,5,6]]) :
[[[1 0 0]
[0 2 0]
[0 0 3]]
[[4 0 0]
[0 5 0]
[0 0 6]]]
tf.matrix_diag_part([x, x]) :
[[1 2 3]
[1 2 3]]
tf.matrix_band_part(x, 0, -1) :
[[1 5 0]
[0 2 0]
[0 0 3]]
tf.matrix_band_part(x, -1, 0) :
[[1 0 0]
[0 2 0]
[0 0 3]]
tf.matrix_band_part(x, 0, 0) :
[[1 0 0]
[0 2 0]
[0 0 3]]
tf.matrix_set_diag(x, y) :
[[10 5 0]
[ 0 20 0]
[ 0 0 30]]
tf.matrix_transpose(x) :
[[1 0 0]
[5 2 0]
[0 0 3]]
tf.matmul(x, x) :
[[ 1 15 0]
[ 0 4 0]
[ 0 0 9]]
tf.norm(x_f) :
6.245
tf.matrix_determinant(x_f) :
6.0
tf.matrix_inverse(x_f) :
[[ 1. -2.5 0. ]
[ 0. 0.5 0. ]
[ 0. 0. 0.33333334]]
tf.cholesky(tf.constant([[2, -1], [-1, 5]], dtype=tf.float32))) :
[[ 1.41421354 0. ]
[-0.70710677 2.12132025]]
tf.qr(x_f) :
Qr(q=array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]], dtype=float32), r=array([[ 1., 5., 0.],
[ 0., 2., 0.],
[ 0., 0., 3.]], dtype=float32))
tf.tf.self_adjoint_eig(x_f) :
(array([ 1., 2., 3.], dtype=float32), array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]], dtype=float32))
tf.self_adjoint_eigvals([x_f, x_f]) :
[[ 1. 2. 3.]
[ 1. 2. 3.]]
tf.svd(x_f) :
(array([ 5.46498585, 3. , 0.36596629], dtype=float32), array([[ 0.93272191, 0. , 0.36059672],
[ 0.36059672, 0. , -0.93272191],
[ 0. , 1. , 0. ]], dtype=float32), array([[ 0.17067236, 0. , 0.9853279 ],
[ 0.9853279 , 0. , -0.17067236],
[ 0. , 1. , 0. ]], dtype=float32))
Tensor Math Function
tf.tensordot
import tensorflow as tf
x = tf.constant([[1,5,0],[0,2,0],[0,0,3]])
x_f = tf.cast(x, tf.float32)
y = tf.constant([10, 20, 30])
with tf.Session() as sess:
print '\ntf.tensordot(x, x, axes=1) : \n', sess.run(tf.tensordot(x, x, axes=1))
tf.tensordot(x, x, axes=1) :
[[ 1 15 0]
[ 0 4 0]
[ 0 0 9]]
Complex Number Functions
tf.complex、tf.conj、tf.imag、tf.real
import tensorflow as tf
x = tf.constant([[1,5,0],[0,2,0],[0,0,3]])
x_f = tf.cast(x, tf.float32)
y = tf.constant([10, 20, 30])
with tf.Session() as sess:
print '\ntf.complex(x_f, x_f) : \n', sess.run(tf.complex(x_f, x_f))
print '\ntf.conj(tf.constant([1+2j, 2-3j])) : \n', sess.run(tf.conj(tf.constant([1+2j,2-3j])))
print '\ntf.imag(tf.constant([1+2j,2-3j])): \n', sess.run(tf.imag(tf.constant([1+2j,2-3j])))
print '\ntf.real(tf.constant([1+2j,2-3j])): \n', sess.run(tf.real(tf.constant([1+2j,2-3j])))
tf.complex(x_f, x_f) :
[[ 1.+1.j 5.+5.j 0.+0.j]
[ 0.+0.j 2.+2.j 0.+0.j]
[ 0.+0.j 0.+0.j 3.+3.j]]
tf.conj(tf.constant([1+2j, 2-3j])) :
[ 1.-2.j 2.+3.j]
tf.imag(tf.constant([1+2j,2-3j])):
[ 2. -3.]
tf.real(tf.constant([1+2j,2-3j])):
[ 1. 2.]
Reduction
tf.reduce_sum、tf.reduce_prod、tf.reduce_min、tf.reduce_max、tf.reduce_mean、tf.reduce_all、tf.reduce_any、tf.reduce_logsumexp、tf.count_nonzero、tf.accumulate_n
import tensorflow as tf
x = tf.constant(range(1, 10), shape=[3, 3])
x_bool = tf.constant([[True,True,False,False],[True,False,False,True]])
with tf.Session() as sess:
print '\ntf.reduce_sum(x) : \n', sess.run(tf.reduce_sum(x))
print '\ntf.reduce_sum(x, axis=0) : \n', sess.run(tf.reduce_sum(x, axis=0))
print '\ntf.reduce_sum(x, axis=1) : \n', sess.run(tf.reduce_sum(x, axis=1))
print '\ntf.reduce_prod(x) : \n', sess.run(tf.reduce_prod(x))
print '\ntf.reduce_prod(x, axis=0) : \n', sess.run(tf.reduce_prod(x, axis=0))
print '\ntf.reduce_prod(x, axis=1) : \n', sess.run(tf.reduce_prod(x, axis=1))
print '\ntf.reduce_min(x) : \n', sess.run(tf.reduce_min(x))
print '\ntf.reduce_min(x, axis=0) : \n', sess.run(tf.reduce_min(x, axis=0))
print '\ntf.reduce_min(x, axis=1) : \n', sess.run(tf.reduce_min(x, axis=1))
print '\ntf.reduce_max(x) : \n', sess.run(tf.reduce_max(x))
print '\ntf.reduce_max(x, axis=0) : \n', sess.run(tf.reduce_max(x, axis=0))
print '\ntf.reduce_max(x, axis=1) : \n', sess.run(tf.reduce_max(x, axis=1))
print '\ntf.reduce_mean(x) : \n', sess.run(tf.reduce_mean(x))
print '\ntf.reduce_mean(x, axis=0) : \n', sess.run(tf.reduce_mean(x, axis=0))
print '\ntf.reduce_mean(x, axis=1) : \n', sess.run(tf.reduce_mean(x, axis=1))
print '\ntf.reduce_all(x_bool) : \n', sess.run(tf.reduce_all(x_bool))
print '\ntf.reduce_all(x_bool, ax_boolis=0) : \n', sess.run(tf.reduce_all(x_bool, axis=0))
print '\ntf.reduce_all(x_bool, ax_boolis=1) : \n', sess.run(tf.reduce_all(x_bool, axis=1))
print '\ntf.reduce_any(x_bool) : \n', sess.run(tf.reduce_any(x_bool))
print '\ntf.reduce_any(x_bool, ax_boolis=0) : \n', sess.run(tf.reduce_any(x_bool, axis=0))
print '\ntf.reduce_any(x_bool, ax_boolis=1) : \n', sess.run(tf.reduce_any(x_bool, axis=1))
print '\ntf.reduce_min(x) : \n', sess.run(tf.reduce_min(x))
print '\ntf.reduce_min(x, axis=0) : \n', sess.run(tf.reduce_min(x, axis=0))
print '\ntf.reduce_min(x, axis=1) : \n', sess.run(tf.reduce_min(x, axis=1))
a = tf.constant([[0,0,0], [0,1,2]])
print '\ntf.count_nonzero(a) : \n', sess.run(tf.count_nonzero(a))
print '\ntf.count_nonzero(a, axis=0) : \n', sess.run(tf.count_nonzero(a, axis=0))
print '\ntf.count_nonzero(a, axis=1) : \n', sess.run(tf.count_nonzero(a, axis=1))
print '\ntf.accumulate_n([x, x]) : \n', sess.run(tf.accumulate_n([x, x]))
tf.reduce_sum(x) :
45
tf.reduce_sum(x, axis=0) :
[12 15 18]
tf.reduce_sum(x, axis=1) :
[ 6 15 24]
tf.reduce_prod(x) :
362880
tf.reduce_prod(x, axis=0) :
[ 28 80 162]
tf.reduce_prod(x, axis=1) :
[ 6 120 504]
tf.reduce_min(x) :
1
tf.reduce_min(x, axis=0) :
[1 2 3]
tf.reduce_min(x, axis=1) :
[1 4 7]
tf.reduce_max(x) :
9
tf.reduce_max(x, axis=0) :
[7 8 9]
tf.reduce_max(x, axis=1) :
[3 6 9]
tf.reduce_mean(x) :
5
tf.reduce_mean(x, axis=0) :
[4 5 6]
tf.reduce_mean(x, axis=1) :
[2 5 8]
tf.reduce_all(x_bool) :
False
tf.reduce_all(x_bool, ax_boolis=0) :
[ True False False False]
tf.reduce_all(x_bool, ax_boolis=1) :
[False False]
tf.reduce_any(x_bool) :
True
tf.reduce_any(x_bool, ax_boolis=0) :
[ True True False True]
tf.reduce_any(x_bool, ax_boolis=1) :
[ True True]
tf.reduce_min(x) :
1
tf.reduce_min(x, axis=0) :
[1 2 3]
tf.reduce_min(x, axis=1) :
[1 4 7]
tf.count_nonzero(a) :
2
tf.count_nonzero(a, axis=0) :
[0 1 1]
tf.count_nonzero(a, axis=1) :
[0 2]
tf.accumulate_n([x, x]) :
[[ 2 4 6]
[ 8 10 12]
[14 16 18]]
Scan
tf.cumsum、tf.cumprod
import tensorflow as tf
x = tf.constant(range(1, 10), shape=[3, 3])
x_bool = tf.constant([[True,True,False,False],[True,False,False,True]])
with tf.Session() as sess:
print '\ntf.cumsum(x) : \n', sess.run(tf.cumsum(x))
print '\ntf.cumsum(x, axis=0) : \n', sess.run(tf.cumsum(x, axis=0))
print '\ntf.cumsum(x, axis=1) : \n', sess.run(tf.cumsum(x, axis=1))
print '\ntf.cumprod(x) : \n', sess.run(tf.cumprod(x))
print '\ntf.cumprod(x, axis=0) : \n', sess.run(tf.cumprod(x, axis=0))
print '\ntf.cumprod(x, axis=1) : \n', sess.run(tf.cumprod(x, axis=1))
tf.cumsum(x) :
[[ 1 2 3]
[ 5 7 9]
[12 15 18]]
tf.cumsum(x, axis=0) :
[[ 1 2 3]
[ 5 7 9]
[12 15 18]]
tf.cumsum(x, axis=1) :
[[ 1 3 6]
[ 4 9 15]
[ 7 15 24]]
tf.cumprod(x) :
[[ 1 2 3]
[ 4 10 18]
[ 28 80 162]]
tf.cumprod(x, axis=0) :
[[ 1 2 3]
[ 4 10 18]
[ 28 80 162]]
tf.cumprod(x, axis=1) :
[[ 1 2 6]
[ 4 20 120]
[ 7 56 504]]
Segmentation
Sequence Comparison and Indexing
tf.argmin、tf.argmax
import tensorflow as tf
x = tf.constant(range(1, 10), shape=[3, 3])
x_bool = tf.constant([[True,True,False,False],[True,False,False,True]])
with tf.Session() as sess:
print '\ntf.argmin(x) : \n', sess.run(tf.argmin(x))
print '\ntf.argmin(x, axis=0) : \n', sess.run(tf.argmin(x, axis=0))
print '\ntf.argmin(x, axis=1) : \n', sess.run(tf.argmin(x, axis=1))
print '\ntf.argmax(x) : \n', sess.run(tf.argmax(x))
print '\ntf.argmax(x, axis=0) : \n', sess.run(tf.argmax(x, axis=0))
print '\ntf.argmax(x, axis=1) : \n', sess.run(tf.argmax(x, axis=1))
tf.argmin(x) :
[0 0 0]
tf.argmin(x, axis=0) :
[0 0 0]
tf.argmin(x, axis=1) :
[0 0 0]
tf.argmax(x) :
[2 2 2]
tf.argmax(x, axis=0) :
[2 2 2]
tf.argmax(x, axis=1) :
[2 2 2]