极简TensorFlow教程学习-----TensorFlow基本的常量、变量以及运算操作

本博客有各种实例,简单明了。

参考TensoFlow官网的API,链接为http://www.tensorfly.cn/tfdoc/get_started/introduction.html

1、tf.constant()

#def constant(value, dtype=None, shape=None, name="Const"):
#创建一个常数张量,value为张量的值,dtype为张量的类型,shape为张量的形状,name为张量的名称
a = tf.constant(2.0, dtype=tf.float32, shape=[2, 3], name='a') #Tensor("a:0", shape=(2, 3), dtype=float32)
b = tf.constant(2, name='b') #Tensor("b:0", shape=(), dtype=int32)
c = tf.constant([[1, 2], [3, 4]], name='c') #Tensor("c:0", shape=(2, 2), dtype=int32)
d = tf.constant(2) #Tensor("Const:0", shape=(), dtype=int32)

2、tf.zeros()

#def zeros(shape, dtype=dtypes.float32, name=None)
#创建一个值为0的张量 其中 dtype为张量的类型,shape为张量的形状,name为张量的名称
a = tf.zeros(shape=[2,3], dtype=tf.int32, name='a') #Tensor("a:0", shape=(2, 3), dtype=int32)
b = tf.zeros([3, 4], tf.int32) #Tensor("zeros:0", shape=(3, 4), dtype=int32)

3、tf.zeros_like()

#tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)
#根据输入张量创建一个值为0的张量,形状和输入张量相同。其中 input_tensor为输入张量,dtype为类型,name为名称,optimize默认为True。
input_tensor = tf.constant([[1, 2, 3], [4, 5, 6]]) #Tensor("Const:0", shape=(2, 3), dtype=int32)
a = tf.zeros_like(input_tensor, dtype=tf.float32)  #Tensor("zeros_like:0", shape=(2, 3), dtype=float32)

4、tf.ones()

#tf.ones(shape, dtype=tf.float32, name=None),与tf.zeros()类似。
a = tf.ones(shape=[2, 3], dtype=tf.float32, name='a') #Tensor("a:0", shape=(2, 3), dtype=float32)

5、tf.ones_like()

#tf.ones_like(input_tensor, dtype=None, name=None, optimize=True),与tf.zeros_like()类似。
tensor = tf.constant(2, dtype=tf.float32, shape=[2,3], name='tensor')
b = tf.ones_like(tensor, dtype=tf.int32, name='b') #Tensor("b:0", shape=(2, 3), dtype=int32)

6、tf.fill()

#def fill(dims, value, name=None)
#产生一个张量,用一个具体值填充张量, 其中,dims为张量形状,同上述shape参数作用一样,vlaue为填充值,name为名称。
b = tf.fill([2, 3], 4) #Tensor("Fill:0", shape=(2, 3), dtype=int32)

更新中

猜你喜欢

转载自blog.csdn.net/qq_28057379/article/details/105614373