#书上称这一系列为规约函数。基本上都是降维
import tensorflow as tf
import numpy as np
t=np.random.randint(0,3,[3,4])
sess=tf.Session()
print("Test matrix is:\n",t)
print("现在测试tf.reduce_sum,对tensor中的元素求和")
print("tf.reduce_sum():",sess.run(tf.reduce_sum(t)))
print("tf.reduce_sun(axis=0):",sess.run(tf.reduce_sum(t,axis=0)))
print("tf.reduce_sun(axis=1):",sess.run(tf.reduce_sum(t,axis=1)))
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_prod,对tensor中的元素求乘积")
print("tf.reduce_prod():",sess.run(tf.reduce_prod(t)))
print("tf.reduce_prod(axis=0):",sess.run(tf.reduce_prod(t,axis=0)))
print("tf.reduce_prod(axis=1):",sess.run(tf.reduce_prod(t,axis=1)))
print("tf.reduce_prod(axis=0,keep_dims=True):",sess.run(tf.reduce_prod(t,axis=0,keep_dims=True)))
print("tf.reduce_prod(axis=1,keep_dims=True):",sess.run(tf.reduce_prod(t,axis=1,keep_dims=True)))
print("输出提示keep_dims将从以后的TF中移除,所以下面的测试不再测试这个参数,默认为False")
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_min,对tensor中的元素求最小值,reduce_max参数意义相同,忽略测试")
print("tf.reduce_min():",sess.run(tf.reduce_min(t)))
print("tf.reduce_min(axis=0):",sess.run(tf.reduce_min(t,axis=0)))
print("tf.reduce_min(axis=1):",sess.run(tf.reduce_min(t,axis=1)))
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_mean,对tensor中的元素求均值,如果tensor元素是整数,则计算结果自动只取整数部分")
print("tf.reduce_mean():",sess.run(tf.reduce_mean(t)))
print("tf.reduce_mean(axis=0):",sess.run(tf.reduce_mean(t,axis=0)))
print("tf.reduce_mean(axis=1):",sess.run(tf.reduce_mean(t,axis=1)))
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_all,对tensor中的元素求逻辑与")
print("tf.reduce_all():",sess.run(tf.reduce_all(t)))
print("tf.reduce_all(axis=0):",sess.run(tf.reduce_all(t,axis=0)))
print("tf.reduce_all(axis=1):",sess.run(tf.reduce_all(t,axis=1)))
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_any,对tensor中的元素求逻辑或")
print("tf.reduce_any():",sess.run(tf.reduce_any(t)))
print("tf.reduce_any(axis=0):",sess.run(tf.reduce_any(t,axis=0)))
print("tf.reduce_any(axis=1):",sess.run(tf.reduce_any(t,axis=1)))
运行结果点击链接
import tensorflow as tf
import numpy as np
t=np.random.randint(0,3,[3,4])
sess=tf.Session()
print("Test matrix is:\n",t)
print("现在测试tf.reduce_sum,对tensor中的元素求和")
print("tf.reduce_sum():",sess.run(tf.reduce_sum(t)))
print("tf.reduce_sun(axis=0):",sess.run(tf.reduce_sum(t,axis=0)))
print("tf.reduce_sun(axis=1):",sess.run(tf.reduce_sum(t,axis=1)))
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_prod,对tensor中的元素求乘积")
print("tf.reduce_prod():",sess.run(tf.reduce_prod(t)))
print("tf.reduce_prod(axis=0):",sess.run(tf.reduce_prod(t,axis=0)))
print("tf.reduce_prod(axis=1):",sess.run(tf.reduce_prod(t,axis=1)))
print("tf.reduce_prod(axis=0,keep_dims=True):",sess.run(tf.reduce_prod(t,axis=0,keep_dims=True)))
print("tf.reduce_prod(axis=1,keep_dims=True):",sess.run(tf.reduce_prod(t,axis=1,keep_dims=True)))
print("输出提示keep_dims将从以后的TF中移除,所以下面的测试不再测试这个参数,默认为False")
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_min,对tensor中的元素求最小值,reduce_max参数意义相同,忽略测试")
print("tf.reduce_min():",sess.run(tf.reduce_min(t)))
print("tf.reduce_min(axis=0):",sess.run(tf.reduce_min(t,axis=0)))
print("tf.reduce_min(axis=1):",sess.run(tf.reduce_min(t,axis=1)))
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_mean,对tensor中的元素求均值,如果tensor元素是整数,则计算结果自动只取整数部分")
print("tf.reduce_mean():",sess.run(tf.reduce_mean(t)))
print("tf.reduce_mean(axis=0):",sess.run(tf.reduce_mean(t,axis=0)))
print("tf.reduce_mean(axis=1):",sess.run(tf.reduce_mean(t,axis=1)))
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_all,对tensor中的元素求逻辑与")
print("tf.reduce_all():",sess.run(tf.reduce_all(t)))
print("tf.reduce_all(axis=0):",sess.run(tf.reduce_all(t,axis=0)))
print("tf.reduce_all(axis=1):",sess.run(tf.reduce_all(t,axis=1)))
print("-----------------------------------------------------------------------------------------------")
print("现在测试tf.reduce_any,对tensor中的元素求逻辑或")
print("tf.reduce_any():",sess.run(tf.reduce_any(t)))
print("tf.reduce_any(axis=0):",sess.run(tf.reduce_any(t,axis=0)))
print("tf.reduce_any(axis=1):",sess.run(tf.reduce_any(t,axis=1)))
运行结果点击链接
Comments
Post a Comment