深度学习tensorflow框架的张量

#-*-coding:utf-8-*-
import   tensorflow  as  tf
#默认不进行转化
a=tf.constant(3.0,dtype=tf.float32)
c=tf.constant([[3.0,4.0],[5.0,7.0]],dtype=tf.float32)
b=tf.constant([3.0,4.0],dtype=tf.float32)
#张量的形状与np.array()的形状理解一样
print(a.shape)
print(b.shape)
#张量类型的转化
a=tf.to_int32(a)#返回转化后的值,但不改变a本身
print(a)
#在交互的会话环境中,进行run--op.eval()
#计算的时候转化数值型,保存时基本都是字符串
b=tf.cast(b,dtype=tf.int32,name='cast_int32')
print(b)
#创建张量
#创建固定值张量
#tf.ones  tf.zeros  tf.constant
#创建随机张量--符合正太分布张量
w=tf.random_normal(shape=(2,2),mean=0.0,stddev=1.0,dtype=tf.float64,name='w')

print(w)
#更改形状--reshape可以同阶更改形状,也可以跨阶更改形状,元素个数不能变化
w=tf.reshape(w,shape=(10,))#错误的
w=tf.reshape(w,shape=(4,1))
w=tf.reshape(w,shape=(1,1,1,2,2))

with  tf.Session()  as ss:
    print(ss.run(w))
#形状
g=tf.placeholder(dtype=tf.float64,shape=[None,2],name='g')
#w.set_shape(shape=(4,1))#不可以
#set_shape静态形状改变
g.set_shape((2,2))#只能用来将形状不确定的固定下来
g=tf.reshape(g,shape=(2,2))
#reshape动态形状改变

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值