zoukankan      html  css  js  c++  java
  • Modify tensor shape

    一 Transformation method

    • TensorFlow tensor has two shape transfomations,dynamic shape and static shape.Static shape conversion calls set_shape().The conversion must be the same shape as the initial tensor creation.For a tensor with a static shape fixed,the static shape cannot be set again.
    • Dynamic shape conversion calls tf.reshape(tensor,shape),the number of elements of the dynamically transformed shape tensor must be the same as before modification.

    二 Code example

    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    import tensorflow as tf 
    def tensor_shape_demo():
        a_p = tf.placeholder(dtype=tf.float32,shape=[None,None])
        b_p = tf.placeholder(dtype=tf.float32,shape=[None,5])
        print('======================Static shape modification')
        print('The shape before modification:')
        print('a_p:',a_p)
        print('b_p:',b_p)
        print('The shape after modification:')
        a_p.set_shape([3,4])
        b_p.set_shape([2,5])
        print('a_p:',a_p)
        print('b_p:',b_p)
        print('======================Dynamic shape modification')
        c_p = tf.placeholder(dtype=tf.float32,shape=[3,4])
        print('The shape before modification:')
        print('c_p:',c_p)
        print('The shape after modification:')
        c_p = tf.reshape(c_p,shape=[2,6])
        print('c_p:',c_p)
        c_p = tf.reshape(c_p,shape=[4,3])
        print('c_p:',c_p)
        c_p = tf.reshape(c_p,shape=[3,4,1])
        print('c_p:',c_p)
    if __name__ == '__main__':
        tensor_shape_demo()
    

    三 Operation result

    正是江南好风景
  • 相关阅读:
    [csp-s模拟测试72] 简单的期望
    [csp-s模拟测试72] 简单的序列
    csp-s模拟测试70
    经验积累
    [csp-s模拟测试69] 回滚莫队
    [模板]主席树查前趋后继
    复习及学习计划
    错排公式
    csp-s模拟测试 56~65
    LIS LCS LCIS
  • 原文地址:https://www.cnblogs.com/monsterhy123/p/13066715.html
Copyright © 2011-2022 走看看