【Tensorflow学习】之常用函数笔记

    xiaoxiao2022-07-05  147

    tf.contact()

    数据连接

    axis = 0,相当于增加样本数,第一个维度中数据变多

    axis = 1,相当于增加第二个维度的特征数,第二个维度中数据变多

    axis = 2,相当于增加第三个维度的特征数,第三个维度中数据变多

    import tensorflow as tf import numpy as np t1 = [[1, 2, 3], [4, 5, 6]] print(np.shape(np.array(t1))) t2 = [[7, 8, 9], [10, 11, 12]] print(tf.concat([t1, t2], 0)) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] print(tf.concat([t1, t2], 1)) # [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]] t1 = [[[1, 2], [2, 3]], [[4, 4], [5, 3]]] print(np.shape(np.array(t1))) t2 = [[[7, 4], [8, 4]], [[2, 10], [15, 11]]] print(tf.concat([t1, t2], 2)) ''' [[[1, 2, 7, 4], [2, 3, 8, 4]], [[4, 4, 2, 10], [5, 3, 15, 11]]] '''

    tf.stack(values, axis=0, name="stack")

    tf.concat拼接的是除了拼接维度axis外其他维度的shape完全相同的张量,并且产生的张量的阶数不会发生变化,而tf.stack则会在新的张量阶上拼接,产生的张量的阶数将会增加

     

    给出N个张量,并且每个张量的shape是(A, B, C)

    如果axis == 0,则tf.stack的输出为(N, A, B, C)

    如果axis == 1,则tf.stack的输出为(A, N, B, C)

    原文是:

     Given a list of length `N` of tensors of shape `(A, B, C)`;

      if `axis == 0` then the `output` tensor will have the shape `(N, A, B, C)`.   if `axis == 1` then the `output` tensor will have the shape `(A, N, B, C)`.

    t1 = [[1, 2, 3], [4, 5, 6]] print(np.shape(np.array(t1))) t2 = [[7, 8, 9], [10, 11, 12]] t3 = [[7, 8, 9], [10, 11, 12]] sess = tf.Session() print(sess.run(tf.concat([t1, t2, t3], 0))) print(tf.concat([t1, t2, t3], 0)) print(sess.run(tf.stack([t1, t2, t3], axis=0))) print(tf.stack([t1, t2, t3], axis=0))

    而tf.unstack与tf.stack的操作相反,是将一个高阶数的张量在某个axis上分解为低阶数的张量

    t1 = [[1, 2, 3], [4, 5, 6]] print(np.shape(np.array(t1))) t2 = [[7, 8, 9], [10, 11, 12]] t3 = [[13, 14, 15], [16, 17, 18]] sess = tf.Session() print(sess.run(tf.concat([t1, t2, t3], 0))) print(tf.concat([t1, t2, t3], 0)) stack = tf.stack([t1, t2, t3], axis=0) print(sess.run(stack)) print(stack) unstack = tf.unstack(stack) print(unstack) print(sess.run(unstack)) #[<tf.Tensor 'unstack:0' shape=(2, 3) dtype=int32>, <tf.Tensor 'unstack:1' shape=(2, 3) dtype=int32>, <tf.Tensor 'unstack:2' shape=(2, 3) dtype=int32>]

    tf.gather()

    类似于数组的索引,可以把向量中某些索引值提取出来,得到新的向量,适用于要提取的索引为不连续的情况。这个函数似乎只适合在一维的情况下使用。

    import tensorflow as tf a = tf.Variable([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]) index_a = tf.Variable([0, 2]) b = tf.Variable([0,1, 2, 3, 4, 5, 6, 7, 8, 9]) index_b = tf.Variable([2, 4, 6, 8]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(tf.gather(a, index_a))) print(sess.run(tf.gather(b, index_b))) ''' [[ 1 2 3 4 5] [11 12 13 14 15]] ''' ''' [2 4 6 8] '''

    tf.gather_nd(params, indices, name=None)

    效果同上,只不过适用于多维数据,就是相当于indices(坐标点)是用来索引params的某个坐标点数据

     

    import tensorflow as tf a = tf.Variable([[0,1, 2, 3, 4], [5,6, 7, 8, 9]]) index_a = tf.Variable([[0, 2], [0, 4], [1, 1]]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(tf.gather_nd(a, index_a))) # [2 4 6]

     

    tf.cast(x, dtype, name=None)

    数据类型转换

    a = tf.constant([0, 1, 2, 3], dtype='int32') print(a) #Tensor("Const:0", shape=(4,), dtype=int32) b = tf.cast(a, 'float32') print(b) # Tensor("Cast:0", shape=(4,), dtype=float32)

    tf.expand_dims(input, axis=None, name=None, dim=None)

    增加张量的维度 

    tf.squeeze(input, axis=None, name=None, squeeze_dims=None)

    压缩张量的维度  

    a = tf.constant([0, 1, 2, 3], dtype='int32') print(a) # Tensor("Const_1:0", shape=(4,), dtype=int32) b = tf.expand_dims(a, 0) print(b) # Tensor("ExpandDims:0", shape=(1, 4), dtype=int32) print(tf.squeeze(b, 0)) # Tensor("Squeeze:0", shape=(4,), dtype=int32)

     

     

     

     

    最新回复(0)