Tensorlow 中文API:tf.zeros() tf.ones()tf.fill()tf.constant()

在程序中有一处不理解的地方 import basic.util.prints这个basic包找不到也搜不到,有知道的帮忙留言,谢谢

可以在下面使用print(data.eval())来输出结果


Tensors常量值函数

  • tf.zeros(shape, dtype=tf.float32, name=None)
  • tf.zeros_like(tensor, dtype=None, name=None)
  • tf.ones(shape, dtype=tf.float32, name=None)
  • tf.ones_like(tensor, dtype=None, name=None)
  • tf.fill(dims, value, name=None)
  • tf.constant(value, dtype=None, shape=None, name='Const')

在Tensorflow中,任何参数的运算和操作都需要转换成对应的TensorFlow数据类型,例如现实中的字符串类型是不能直接在TensorFlow中进行运算的,必须转换成对应的TensorFlow类型才行

故而Tensorflow提供了一些函数用于生成常量值.如上是一些基本的生成常量的方法.

tf.zeros(shape, dtype=tf.float32, name=None)

创建一个所有的参数为0的tensor对象

This operation returns a tensor of type dtype with shape shape and all elements set to zero.

这个操作会返回一个类型为dtype,并且维度为sharp的tensor,并且所有的参数均为0.

参数:
  • shape: 用于表示维度,通常为一个int32类型数组,或者一个一维(1-D)的tf.int32数字.注意不能直接使用数字
  • dtype: 所要创建的tensor对象的数据类型
  • name: 一个该操作的别名(可选的).
返回:

所有参数都为0的tensor对象

用例以及结果:

#coding=utf8
import tensorflow as tf
import basic.util.prints as p

sess = tf.InteractiveSession()

# 创建一个维度为1, 类型为int的对象
data = tf.zeros([1], dtype=tf.int32)
p.printValue("sess.zeros([1], dtype=tf.int32)", data)
# 创建一个维度为3, 类型为int的对象
data = tf.zeros([1,2,1], dtype=tf.int32)
p.printValue("sess.zeros([3,4,5], dtype=tf.int32)", data)
# double
data = tf.zeros([8], dtype=tf.double)
p.printValue("sess.zeros([1], dtype=tf.double)", data)
# float
data = tf.zeros([8], dtype=tf.float16)
p.printValue("sess.zeros([1], dtype=tf.float16)", data)


sess.close()
# sess.zeros([1], dtype=tf.int32) : Tensor("zeros:0", shape=(1,), dtype=int32) - 
[0]
# sess.zeros([3,4,5], dtype=tf.int32) : Tensor("zeros_1:0", shape=(1, 2, 1), dtype=int32) - 
[[[0]
  [0]]]
# sess.zeros([1], dtype=tf.double) : Tensor("zeros_2:0", shape=(8,), dtype=float64) - 
[ 0.  0.  0.  0.  0.  0.  0.  0.]
# sess.zeros([1], dtype=tf.float16) : Tensor("zeros_3:0", shape=(8,), dtype=float16) - 
[ 0.  0.  0.  0.  0.  0.  0.  0.]
另外,需要注意的是不能直接使用数字作为 shape 的参数,例如下面用例就会报错:
...
# sharp不能为int类型,需要制定为tensor sharp类型
data = tf.zeros(1, dtype=tf.int32)
p.printValue("tf.zeros(1, dtype=tf.int32)", data)
...
Traceback (most recent call last):
  File "/services/git/GIthub/ml-example/tensorflow/basic/casting/string_to_number.py", line 20, in 
    data = tf.zeros(1, dtype=tf.int32)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 623, in zeros
    output = fill(shape, constant(0, dtype=dtype), name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 531, in fill
    return _op_def_lib.apply_op("Fill", dims=dims, value=value, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2156, in create_op
    set_shapes_for_outputs(ret)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1612, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1165, in _FillShape
    dimensions_shape = op.inputs[0].get_shape().with_rank(1)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 625, in with_rank
    raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape () must have rank 1
这个时候可以通过如下两个方式解决
# 可是使用如下方法替换
data = tf.zeros([1], dtype=tf.int32)
p.printValue("tf.zeros([1], dtype=tf.int32)", data)

tf.zeros_like(tensor, dtype=None, name=None)

该方法用于创建一个所有参数均为0的tensor对象

给定一个tensor(tensor对象),该方法会返回一个类似当前参数类型以及维度的对象,但是所有参数的值均为0.当参数dtype选定了后,所有返回参数的类型也会变成选定的类型

该方法实际上为一个拷贝函数:默认情况下,它会拷贝参数tensor的类型,维度等数据,并将其中的值设置为0.当参数dtype设置后,那么拷贝后的tensor对象

参数:

  • tensor: tensor对象
  • dtype: 返回的tensor对象类型,不设置(为空时)时返回类型同参数tensor一致.该参数必须为如下tensorflow类型: float32, float64, int8, int16, int32, int64, uint8以及complex64.

  • name: 该操作别名 (可选).

返回:

所有参数为0的tensor对象

测试用例与结果如下:

#coding=utf8
import tensorflow as tf
import basic.util.prints as p

sess = tf.InteractiveSession()

# create a tensor object
original = [[1,2,3],[4,5,6]]
p.printRowValue("Original value", original)

# 调用zeros_like,默认类型为int
data = tf.zeros_like(original)
p.printValue("tf.zeros_like(original)", data)

# 调用zeros_like,默认类型为double
data = tf.zeros_like(original, dtype=tf.double)
p.printValue("tf.zeros_like(original, dtype=tf.double)", data)

# dtype类型为: float, int, double, uint以及complex(复数), 如下类型支持
data = tf.zeros_like(original, dtype=tf.float16)
data = tf.zeros_like(original, dtype=tf.float16_ref)
data = tf.zeros_like(original, dtype=tf.float32)
data = tf.zeros_like(original, dtype=tf.float32_ref)
data = tf.zeros_like(original, dtype=tf.float64)
data = tf.zeros_like(original, dtype=tf.float64_ref)
data = tf.zeros_like(original, dtype=tf.int8)
data = tf.zeros_like(original, dtype=tf.int8_ref)
data = tf.zeros_like(original, dtype=tf.int16)
data = tf.zeros_like(original, dtype=tf.int16_ref)
data = tf.zeros_like(original, dtype=tf.int32)
data = tf.zeros_like(original, dtype=tf.int32_ref)
data = tf.zeros_like(original, dtype=tf.int64)
data = tf.zeros_like(original, dtype=tf.int64_ref)
data = tf.zeros_like(original, dtype=tf.uint8)
data = tf.zeros_like(original, dtype=tf.uint8_ref)
data = tf.zeros_like(original, dtype=tf.uint16)
data = tf.zeros_like(original, dtype=tf.uint16_ref)
data = tf.zeros_like(original, dtype=tf.double)
data = tf.zeros_like(original, dtype=tf.double_ref)
data = tf.zeros_like(original, dtype=tf.complex64)
data = tf.zeros_like(original, dtype=tf.complex64_ref)
data = tf.zeros_like(original, dtype=tf.complex128)
data = tf.zeros_like(original, dtype=tf.complex128_ref)

# [ERROR] 不支持类型包括: bfloat, qint, quint
# data = tf.zeros_like(original, dtype=tf.bfloat16)
# data = tf.zeros_like(original, dtype=tf.quint8)
# data = tf.zeros_like(original, dtype=tf.qint16)
# data = tf.zeros_like(original, dtype=tf.qint32)


sess.close()
# Original value : [[1, 2, 3], [4, 5, 6]]
# tf.zeros_like(original) : Tensor("zeros_like:0", shape=(2, 3), dtype=int32) - 
[[0 0 0]
 [0 0 0]]
# tf.zeros_like(original, dtype=tf.double) : Tensor("zeros_like_1:0", shape=(2, 3), dtype=float64) - 
[[ 0.  0.  0.]
 [ 0.  0.  0.]]
该方法在参数复制并置0的时候非常有用

tf.ones(shape, dtype=tf.float32, name=None)

创建一个所有的参数为1的tensor对象

这个操作会返回一个类型为dtype,并且维度为sharp的tensor,并且所有的参数均为0.

参数:

  • shape: 用于表示维度,通常为一个int32类型数组,或者一个一维(1-D)的tf.int32数字.注意不能直接使用数字
  • dtype: 所要创建的tensor对象的数据类型
  • name: 一个该操作的别名(可选的).

返回:

所有参数都为1的tensor对象

用例以及结果:

#coding=utf8
import tensorflow as tf
import basic.util.prints as p

sess = tf.InteractiveSession()

# 创建一个维度为1, 类型为int的对象
data = tf.ones([1], dtype=tf.int32)
p.printValue("sess.ones([1], dtype=tf.int32)", data)
# 创建一个维度为3, 类型为int的对象
data = tf.ones([1,2,1], dtype=tf.int32)
p.printValue("sess.ones([3,4,5], dtype=tf.int32)", data)
# double
data = tf.ones([8], dtype=tf.double)
p.printValue("sess.ones([1], dtype=tf.double)", data)
# float
data = tf.ones([8], dtype=tf.float16)
p.printValue("sess.ones([1], dtype=tf.float16)", data)

# [ERROR] sharp不能为int类型,需要制定为tensor sharp类型
# data = tf.ones(1, dtype=tf.int32)
# p.printValue("tf.ones(1, dtype=tf.int32)", data)

# 可是使用如下方法替换
data = tf.ones([1], dtype=tf.int32)
p.printValue("tf.ones([1], dtype=tf.int32)", data)

sess.close()
执行返回结果
# sess.ones([1], dtype=tf.int32) : Tensor("ones:0", shape=(1,), dtype=int32) - 
[1]
# sess.ones([3,4,5], dtype=tf.int32) : Tensor("ones_1:0", shape=(1, 2, 1), dtype=int32) - 
[[[1]
  [1]]]
# sess.ones([1], dtype=tf.double) : Tensor("ones_2:0", shape=(8,), dtype=float64) - 
[ 1.  1.  1.  1.  1.  1.  1.  1.]
# sess.ones([1], dtype=tf.float16) : Tensor("ones_3:0", shape=(8,), dtype=float16) - 
[ 1.  1.  1.  1.  1.  1.  1.  1.]
# tf.ones([1], dtype=tf.int32) : Tensor("ones_4:0", shape=(1,), dtype=int32) - [1]

tf.ones_like(tensor, dtype=None, name=None)

该方法用于创建一个所有参数均为1的tensor对象

给定一个tensor(tensor对象),该方法会返回一个类似当前参数类型以及维度的对象,但是所有参数的值均为1.当参数dtype选定了后,所有返回参数的类型也会变成选定的类型

该方法实际上为一个拷贝函数:默认情况下,它会拷贝参数tensor的类型,维度等数据,并将其中的值设置为1.当参数dtype设置后,那么拷贝后的tensor对象

参数:

  • tensor: tensor对象
  • dtype: 返回的tensor对象类型,不设置(为空时)时返回类型同参数tensor一致.该参数必须为如下tensorflow类型: float32, float64, int8, int16, int32, int64, uint8以及complex64.

  • name: 该操作别名 (可选).

返回:

所有参数为1的tensor对象

测试用例与结果如下:

#coding=utf8
import tensorflow as tf
import basic.util.prints as p

sess = tf.InteractiveSession()

# create a tensor object
original = [1,2,3,4,5]
# original = tf.zeros([5], dtype=tf.float16)
p.printRowValue("Original value", original)

# 调用ones_like,默认类型为int
data = tf.ones_like(original)
p.printValue("tf.ones_like(original)", data)

# 调用ones_like,默认类型为double
data = tf.ones_like(original, dtype=tf.double)
p.printValue("tf.ones_like(original, dtype=tf.double)", data)

# dtype类型为: float, int, double, uint以及complex(复数), 如下类型支持

data = tf.ones_like(original, dtype=tf.float32)
p.printValue("tf.ones_like(original, dtype=tf.float32)", data)
data = tf.ones_like(original, dtype=tf.float32_ref)
p.printValue("tf.ones_like(original, dtype=tf.float32_ref)", data)
data = tf.ones_like(original, dtype=tf.float64)
p.printValue("tf.ones_like(original, dtype=tf.float64)", data)
data = tf.ones_like(original, dtype=tf.float64_ref)
p.printValue("tf.ones_like(original, dtype=tf.float64_ref)", data)
data = tf.ones_like(original, dtype=tf.int8)
p.printValue("tf.ones_like(original, dtype=tf.int8)", data)
data = tf.ones_like(original, dtype=tf.int8_ref)
p.printValue("tf.ones_like(original, dtype=tf.int8_ref)", data)
data = tf.ones_like(original, dtype=tf.int16)
p.printValue("tf.ones_like(original, dtype=tf.int16)", data)
data = tf.ones_like(original, dtype=tf.int16_ref)
p.printValue("tf.ones_like(original, dtype=tf.int16_ref)", data)
data = tf.ones_like(original, dtype=tf.int32)
p.printValue("tf.ones_like(original, dtype=tf.int32)", data)
data = tf.ones_like(original, dtype=tf.int32_ref)
p.printValue("tf.ones_like(original, dtype=tf.int32_ref)", data)
data = tf.ones_like(original, dtype=tf.int64)
p.printValue("tf.ones_like(original, dtype=tf.int64)", data)
data = tf.ones_like(original, dtype=tf.int64_ref)
p.printValue("tf.ones_like(original, dtype=tf.int64_ref)", data)
data = tf.ones_like(original, dtype=tf.uint8)
p.printValue("tf.ones_like(original, dtype=tf.uint8)", data)
data = tf.ones_like(original, dtype=tf.uint8_ref)
p.printValue("tf.ones_like(original, dtype=tf.uint8_ref)", data)

data = tf.ones_like(original, dtype=tf.double)
p.printValue("tf.ones_like(original, dtype=tf.double)", data)
data = tf.ones_like(original, dtype=tf.double_ref)
p.printValue("tf.ones_like(original, dtype=tf.double_ref)", data)
data = tf.ones_like(original, dtype=tf.complex64)
p.printValue("tf.ones_like(original, dtype=tf.complex64)", data)
data = tf.ones_like(original, dtype=tf.complex64_ref)
p.printValue("tf.ones_like(original, dtype=tf.complex64_ref)", data)
data = tf.ones_like(original, dtype=tf.complex128)
p.printValue("tf.ones_like(original, dtype=tf.complex128)", data)
data = tf.ones_like(original, dtype=tf.complex128_ref)
p.printValue("tf.ones_like(original, dtype=tf.complex128_ref)", data)

# 特殊情况
data = tf.ones_like(original, dtype=tf.float16)
# p.printValue("tf.ones_like(original, dtype=tf.float16)", data)
data = tf.ones_like(original, dtype=tf.float16_ref)
# p.printValue("tf.ones_like(original, dtype=tf.float16_ref)", data)
data = tf.ones_like(original, dtype=tf.uint16)
# p.printValue("tf.ones_like(original, dtype=tf.uint16)", data)
data = tf.ones_like(original, dtype=tf.uint16_ref)
# p.printValue("tf.ones_like(original, dtype=tf.uint16_ref)", data)

# [ERROR] 不支持类型包括: bfloat, qint, quint
# data = tf.ones_like(original, dtype=tf.bfloat16)
# data = tf.ones_like(original, dtype=tf.quint8)
# data = tf.ones_like(original, dtype=tf.qint16)
# data = tf.ones_like(original, dtype=tf.qint32)


sess.close()

运行结果:

# Original value : [1, 2, 3, 4, 5]
# tf.ones_like(original) : Tensor("ones_like:0", shape=(5,), dtype=int32) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.double) : Tensor("ones_like_1:0", shape=(5,), dtype=float64) - 
[ 1.  1.  1.  1.  1.]
# tf.ones_like(original, dtype=tf.float32) : Tensor("ones_like_2:0", shape=(5,), dtype=float32) - 
[ 1.  1.  1.  1.  1.]
# tf.ones_like(original, dtype=tf.float32_ref) : Tensor("ones_like_3:0", shape=(5,), dtype=float32) - 
[ 1.  1.  1.  1.  1.]
# tf.ones_like(original, dtype=tf.float64) : Tensor("ones_like_4:0", shape=(5,), dtype=float64) - 
[ 1.  1.  1.  1.  1.]
# tf.ones_like(original, dtype=tf.float64_ref) : Tensor("ones_like_5:0", shape=(5,), dtype=float64) - 
[ 1.  1.  1.  1.  1.]
# tf.ones_like(original, dtype=tf.int8) : Tensor("ones_like_6:0", shape=(5,), dtype=int8) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.int8_ref) : Tensor("ones_like_7:0", shape=(5,), dtype=int8) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.int16) : Tensor("ones_like_8:0", shape=(5,), dtype=int16) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.int16_ref) : Tensor("ones_like_9:0", shape=(5,), dtype=int16) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.int32) : Tensor("ones_like_10:0", shape=(5,), dtype=int32) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.int32_ref) : Tensor("ones_like_11:0", shape=(5,), dtype=int32) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.int64) : Tensor("ones_like_12:0", shape=(5,), dtype=int64) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.int64_ref) : Tensor("ones_like_13:0", shape=(5,), dtype=int64) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.uint8) : Tensor("ones_like_14:0", shape=(5,), dtype=uint8) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.uint8_ref) : Tensor("ones_like_15:0", shape=(5,), dtype=uint8) - 
[1 1 1 1 1]
# tf.ones_like(original, dtype=tf.double) : Tensor("ones_like_16:0", shape=(5,), dtype=float64) - 
[ 1.  1.  1.  1.  1.]
# tf.ones_like(original, dtype=tf.double_ref) : Tensor("ones_like_17:0", shape=(5,), dtype=float64) - 
[ 1.  1.  1.  1.  1.]
# tf.ones_like(original, dtype=tf.complex64) : Tensor("ones_like_18:0", shape=(5,), dtype=complex64) - 
[ 1.+0.j  1.+0.j  1.+0.j  1.+0.j  1.+0.j]
# tf.ones_like(original, dtype=tf.complex64_ref) : Tensor("ones_like_19:0", shape=(5,), dtype=complex64) - 
[ 1.+0.j  1.+0.j  1.+0.j  1.+0.j  1.+0.j]
# tf.ones_like(original, dtype=tf.complex128) : Tensor("ones_like_20:0", shape=(5,), dtype=complex128) - 
[ 1.+0.j  1.+0.j  1.+0.j  1.+0.j  1.+0.j]
# tf.ones_like(original, dtype=tf.complex128_ref) : Tensor("ones_like_21:0", shape=(5,), dtype=complex128) - 
[ 1.+0.j  1.+0.j  1.+0.j  1.+0.j  1.+0.j]


tf.fill(dims, value, name=None)

创建一个维度为dims,值为value的tensor对象.该操作会创建一个维度为dims的tensor对象,并将其值设置为value,该tensor对象中的值类型和value一致

    • 当value为0时,该方法等同于tf.zeros()
    • 当value为1时,该方法等同于tf.ones()
参数:
  • dims: 类型为int32的tensor对象,用于表示输出的维度(1-D, n-D),通常为一个int32数组,如:[1], [2,3]等
  • value: 常量值(字符串,数字等),该参数用于设置到最终返回的tensor对象值中
  • name: 当前操作别名(可选)
返回:

tensor对象,类型和value一致

测试用例如下:

#coding=utf8
import tensorflow as tf
import basic.util.prints as p

sess = tf.InteractiveSession()

dim = [2,3]
data = tf.fill(dim, 5)
p.printValue("tf.fill(dim, value)", data)
data = tf.fill(dim, 5.0)
p.printValue("tf.fill(dim, value)", data)
data = tf.fill(dim, "5.0")
p.printValue("tf.fill(dim, value)", data)

sess.close()

运行返回如下:

# tf.fill(dim, value) : Tensor("Fill:0", shape=(2, 3), dtype=int32) - 
[[5 5 5]
 [5 5 5]]
# tf.fill(dim, value) : Tensor("Fill_1:0", shape=(2, 3), dtype=float32) - 
[[ 5.  5.  5.]
 [ 5.  5.  5.]]
# tf.fill(dim, value) : Tensor("Fill_2:0", shape=(2, 3), dtype=string) - 
[['5.0' '5.0' '5.0']
 ['5.0' '5.0' '5.0']]


tf.constant(value,dtype=None,shape=None,name=’Const’) 

创建一个常量tensor,按照给出value来赋值,可以用shape来指定其形状。value可以是一个数,也可以是一个list。  如果是一个数,那么这个常亮中所有值的按该数来赋值。  如果是list,那么len(value)一定要小于等于shape展开后的长度。赋值时,先将value中的值逐个存入。不够的部分,则全部存入value的最后一个值。


a = tf.constant(2,shape=[2])
b = tf.constant(2,shape=[2,2])
c = tf.constant([1,2,3],shape=[6])
d = tf.constant([1,2,3],shape=[3,2])

sess = tf.InteractiveSession()
print(sess.run(a))
#[2 2]
print(sess.run(b))
#[[2 2]
# [2 2]]
print(sess.run(c))
#[1 2 3 3 3 3]
print(sess.run(d))
#[[1 2]
# [3 3]
# [3 3]]




你可能感兴趣的:(tensorflow学习)