1、tf.squeeze(
input,
axis=None,
name=None,
squeeze_dims=None
)
该函数返回一个张量,将原始输入的input张量默认删除所有为1的维度,
也可以设定位置axis进行删除所在维度为1的位置。
# 't' 是一个维度是[1, 2, 1, 3, 1, 1]的张量
tf.shape(tf.squeeze(t)) # [2, 3], 默认删除所有为1的维度
# 't' 是一个维度[1, 2, 1, 3, 1, 1]的张量
tf.shape(tf.squeeze(t, [2, 4])) # [1, 2, 3, 1],标号从零开始,只删掉了2和4维的1
2、tf.constant(
value,
dtype=None,
shape=None,
name=‘Const’,
verify_shape=False
)
该函数返回一个张量,输入的value可以是一个数值,也可以是一个列表,dtype代表着数据类型,shape表明输出时张量的形状。
当value是一个数字时,shape是一个【2,3】时,里面的数值都用该数字填充;当value是一个列表时【1,2】,shape是一个【3,4】时,后面的数值都用列表最后的数字填充。
const1 = tf.constant(4,shape=[2,3])
>>>[[4 4 4]
[4 4 4]]
const2 = tf.constant((1,3),shape=[3,4])
>>>[[1 3 3 3]
[3 3 3 3]
[3 3 3 3]]
3、tf.globle_variables_initializer()
该函数进行初始化变量。
4、tf.random_normal(
shape,
mean=0.0,
stddev=1.0,
dtype=tf.float32,
seed=None,
name=None
)
该函数shape: 输出张量的形状,必选
mean: 正态分布的均值,默认为0
stddev: 正态分布的标准差,默认为1.0
dtype: 输出的类型,默认为tf.float32
seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样
name: 操作的名称
5、tf.nn.moments(
x,
axes,
shift=None, # pylint: disable=unused-argument
name=None,
keep_dims=False
)
该函数用于计算指定维度均值和方差。
参数:
x:一个Tensor,可以理解为我们输出的数据,形如 [batchsize, height, width, kernels]。
axes:整数数组,用于指定计算均值和方差的轴。如果x是1-D向量且axes=[0] 那么该函数就是计算整个向量的均值与方差。
shift:未在当前实现中使用。
name:用于计算moment的操作范围的名称。
keep_dims:产生与输入具有相同维度的moment,通俗点说就是是否保持维度。
实例:
import tensorflow as tf
img = tf.Variable(tf.random_normal([3, 3]))
axis = list(range(len(img.get_shape()) - 1)) # len(img.get_shape()) :返回img,get_shape()的维度
mean, variance = tf.nn.moments(img, axis)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(img))
print(axis)
resultMean = sess.run(mean)
print('resultMean',resultMean)
resultVar = sess.run(variance)
print('resultVar',resultVar)
> [[ 0.9398157 1.1222504 -0.6046098 ]
[ 1.4187386 -0.298682 1.033441 ]
[ 0.64805275 0.40496045 1.4371132 ]]
> [0]
> resultMean [1.0022024 0.40950966 0.62198144]
> resultVar [0.10093883 0.33651853 0.77942157]
6、tf.squared_difference() 功能说明:计算张量 x、y 对应元素差平方
定义:
tf.squared_difference(
x,
y,
name=None
)
initial_x = [[1.,1.],[2.,2.]]
x = tf.Variable(initial_x,dtype=tf.float32)
initial_y=[[1.,2.],[4.,5.]]
y=tf.Variable(initial_y,dtype=tf.float32)
diff = tf.squared_difference(x,y)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
print (sess.run(diff))
与tf.square()相似,计算张量x对应元素平方
定义:
tf.square(
x,
name=None
)
7、tf.reduce_mean() 功能:计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值
reduce_mean(input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None)
x = [[1,2,3],
[1,2,3]]
xx = tf.cast(x,tf.float32)
mean_0 = tf.reduce_mean(xx, keep_dims=False)
mean_1 = tf.reduce_mean(xx, axis=0, keep_dims=False)
mean_2 = tf.reduce_mean(xx, axis=1, keep_dims=False)
print (m_0) >>> 2.0
print (m_1 ) >>> [ 1. 2. 3.]
print( m_2) >>> [ 2. 2.]
当reduce_mean里的参数改为keep_dims=True,设置保持原来的张量的维度,
print (m_0 ) >>> [[ 2.]]
print( m_1 ) >>> [[ 1. 2. 3.]]
print (m_2 ) >>> [[ 2.], [ 2.]]
8、tf.concat() 功能:将Tensor进行拼接
定义:
tf.concat(
values,
axis,
name=“concat”)
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 1) # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
# tensor t3 with shape [2, 3]
# tensor t4 with shape [2, 3]
tf.shape(tf.concat([t3, t4], 0)) # [4, 3]
tf.shape(tf.concat([t3, t4], 1)) # [2, 6]
当参数axis=-1时,将拼接在最后一维的数值上
t1 = [[[1, 2], [2, 3]], [[4, 4], [5, 3]]]
t2 = [[[7, 4], [8, 4]], [[2, 10], [15, 11]]]
tf.concat([t1, t2], -1)
>>>[[[ 1, 2, 7, 4],
[ 2, 3, 8, 4]],
[[ 4, 4, 2, 10],
[ 5, 3, 15, 11]]]