深度学习之路4

张量

在编写tensorflow程序时,程序传递和运算的主要目标是张量。

 张量的类型

 张量的阶

张量的创建

创建随机张量 

张量的变换

代码:

#张量的演示
def tensor_demo():
    '''
    张量的演示
    :return:
    '''
    tensor1 = tf.constant(4.0)
    tensor2 = tf.constant([1,2,3,4])
    linear_squares = tf.constant([[4],[9],[16],[25]],dtype=tf.int32)
    #0张量
    zero = tf.zeros(shape=[3,4])
    #1张量
    one = tf.ones(shape=[4,3])
    #随机张量.mean是均值,stddev是标准差
    random= tf.random_normal(shape=[2,3],mean=1.75,stddev=0.2)
    print("tensor1:\n",tensor1)
    print("tensor2:\n",tensor2)
    print("linear_squares:\n",linear_squares)
    print("zero:\n",zero)
    print("one:\n",one)
    print("random:\n",random)
    #张量类型的修改
    #改变张量的数据类型
    l_cast = tf.cast(linear_squares,dtype=tf.float32)
    print("l_cast:\n",l_cast)

    #改变静态形状(必须用placeholder,占位张量)
    a_p = tf.placeholder(dtype=tf.float32,shape=[None,None])
    b_p = tf.placeholder(dtype=tf.float32,shape=[None,10])
    c_h = tf.placeholder(dtype=tf.float32,shape=[3,2])
    print("a_p:\n",a_p)
    print("b_p:\n",b_p)
    print("c_h:\n", c_h)
    #静态更新形状未确定的部分,只能更新NONE的地方,并且维数 也是确定的
    #a_p.set_shape([3,2])
    #b_p.set_shape([2,10])
    #动态更新,
    a_p_reshape = tf.reshape(a_p,shape=[2,3,1])
    c_h_reshape = tf.reshape(a_p, shape=[3,3])
    print("c_h_reshape:\n", c_h_reshape)
    print("a_p:\n",a_p)
    print("a_p_reshape:\n",a_p_reshape)
    print("b_p:\n",b_p)
    return None

输出:

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值