TensorFlow创建tensor语句

    技术2025-10-16  11

    学习课程 1.np.array转tf:

    tf.convert_to_tensor(np.ones(2,1)) tf.convert_to_tensor([2,1]) tf.convert_to_tensor([[2],[1]])

    2.tf.zeros

    tf.zeros([]) #一个数 tf.zeros([2]) #一个向量 tf.zeros([2,2]) #一个矩阵 tf.zeros([2,3,3])

    3.tf.zeros_like

    a = tf.zeros([2,1]) tf.zeros_like(a) #和a大小一致的tensor tf.zeros(a.shape)

    4.tf.ones

    tf.ones(1) #一维向量 tf.ones([]) #一个数 tf.ones([2]) #二维向量 tf.ones([2,2]) tf.ones_like(a)

    5.Fill

    tf.fill([2.3],2) #2*3矩阵。每个数都是2

    6.Normal

    tf.random.normal([2,3],mean=1,stddev=1) #2*3矩阵,每个数服从均值为1 方差为1的正态分布 tf.random.trunated_normal([2,3],mean=1,stddev=1)

    7.uniform

    tf.random.uniform([2,3],minval=0,maxval=100,tf.dtype = tf.int32) #均匀分布

    8.Random Permutation

    idx = tf.range(10) idx = tf.random.shuffle(idx) #随机打散

    9.tf.constant

    tf.tf.constant(1) #数值为1 tf.tf.constant([1]) #向量为[1] tf.tf.constant([1,2]) #向量为[1,2]
    Processed: 0.010, SQL: 9