张量
在编写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
输出:


?
?
?
?
?
?
?
|