极简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)

更新中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值