#-*-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动态形状改变