这是个很有用的函数,我们经常需要将python的数据类型转换成TensorFlow可用的tensor数据类型,所以仔细研究一下这个函数还是很有必要的。
参考官方说明文档
format:convert_to_tensor(value, dtype=None, name=None, preferred_dtype=None)
Args:
value: An object whose type has a registered `Tensor` conversion function.(这个说明这个函数只能转换特定的python数据类型)
dtype: Optional element type for the returned tensor. If missing, the(可以指定转化成tensor后输出的数据类型)
type is inferred from the type of `value`.
name: Optional name to use if a new `Tensor` is created.
preferred_dtype: Optional element type for the returned tensor,
used when dtype is None. In some cases, a caller may not have a
dtype in mind when converting to a tensor, so preferred_dtype
can be used as a soft preference. If the conversion to
`preferred_dtype` is not possible, this argument has no effect.
Returns:
An `Output` based on `value`.(显然这个函数转换python成TensorFlow可用的tensor,但是具体的数类型还是有参数value决定)
This function converts Python objects of various types to `Tensor` objects. It accepts `Tensor` objects, numpy arrays, Python lists and Python scalars.
翻译过来:这个函数把python的变量类型转换成tensor,而这个value可以是tensor,numpy arrays(numpy 的数组),python list(python 列表)python的表量
import tensorflow as tf
import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
print (a)
b=tf.constant(a)
with tf.Session() as sess:
print (b)
for x in b.eval(): #b.eval()就得到tensor的数组形式
print (x)
print ('a是数组',a)
tensor_a=tf.convert_to_tensor(a)
print ('现在转换为tensor了...',tensor_a)
结果:
[[1 2 3]
[4 5 6]
[7 8 9]]
Tensor("Const:0", shape=(3, 3), dtype=int64)
[1 2 3]
[4 5 6]
[7 8 9]
a是数组 [[1 2 3]
[4 5 6]
[7 8 9]]
现在转换为tensor了... Tensor("Const_1:0", shape=(3, 3), dtype=int64)
tf.one_hot:
输出指定tensor的one_hot编码,第二个参数为输出的长度
classes = 3
labels = tf.constant([0,1,2]) # 输入的元素值最小为0,最大为2
output = tf.one_hot(labels,classes)
with tf.Session() as sess:
print(sess.run(output))
输出结果:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
tf.reduce_mean:
tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
reduce_mean(input_tensor,
axis=None,
keep_dims=False,
name=None,
reduction_indices=None)
第一个参数input_tensor: 输入的待降维的tensor;
第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;
第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
第四个参数name: 操作的名称;
第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;
x = [[1, 2, 3],
[1, 2, 3]]
xx = tf.cast(x, tf.float32)
mean_all = tf.reduce_mean(xx, keep_dims=False)
mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False)
mean_1 = tf.reduce_mean(xx, axis=1, keep_dims=False)
with tf.Session() as sess:
m_a, m_0, m_1 = sess.run([mean_all, mean_0, mean_1])
print(m_a)
print(m_0)
print(m_1)
输出结果:
2.0
[1. 2. 3.]
[2. 2.]
xx = tf.constant([[[1., 1, 1], [2., 2, 2]], [[3, 3, 3], [4, 4, 4]]]) m3 = tf.reduce_mean(xx, axis=(0,1)) with tf.Session() as sess: print(sess.run(m3))
现在给定的xx是一个三维的矩阵,给定的axis为0,1,也就是说要要消除前两个维度,只保留第三个维度,第三维是有三个元素,那么输出的结果也是有三个元素。[1., 1, 1],[2., 2, 2],[3,3,3],[4,4,4]各拿出一个来做一个平均就能达到只保留第三个维度的作用。因此最后的结果是[2.5 2.5 2.5]
m3 = tf.reduce_mean(xx, axis=(1,2))
现在的意思是保留第一个维度,第一个维度只有两个元素,那么reduce之后的结果也是只有两个元素。当前第一位的两个元素分别是[[1., 1, 1], [2., 2, 2]],[[3, 3, 3], [4, 4, 4]]那么就是111222,333444分别求均值就行了。结果是[1.5 3.5]
m3 = tf.reduce_mean(xx, axis=(0,2))
只保留第二维,第二维的元素个数是两个,因此最后输出的结果也是只有两个,第三维直接求平均之后消除了第三维,结果是
[[1. 2.]
[3. 4.]]
然后再消除第一维,结果是[2. 3.]
m3 = tf.reduce_mean(xx, axis=0)
消除第一维,保留第二维和第三维,第一维的两个元素分别是[[1., 1, 1], [2., 2, 2]],[[3, 3, 3], [4, 4, 4]],那么这两个元素对应位置直接求平均就ok,输出的结果是
[[2. 2. 2.]
[3. 3. 3.]]
m3 = tf.reduce_mean(xx, axis=1)
消除第二维,保留第一维和第三维。[1., 1, 1], [2., 2, 2]对应位置求平均,[3, 3, 3], [4, 4, 4]对应位置求平均就可以消除
[[1.5 1.5 1.5]
[3.5 3.5 3.5]]
m3 = tf.reduce_mean(xx, axis=2)
消除第三维,直接第三维上的三个数求平均就行了,结果是
[[1. 2.]
[3. 4.]]