zoukankan      html  css  js  c++  java
  • tensorflow 小记——如何对张量做任意行求和,得到新tensor(一种方法:列表生成式)

    希望实现图片上的功能

    import tensorflow as tf
    a = tf.range(10,dtype=float)
    b = a
    a = tf.reshape(a,[-1,1])
    a = tf.tile(a,[1,3])

    sess = tf.Session()
    print(sess.run(b))
    print(sess.run(a))

    [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
    [[0. 0. 0.]
    [1. 1. 1.]
    [2. 2. 2.]
    [3. 3. 3.]
    [4. 4. 4.]
    [5. 5. 5.]
    [6. 6. 6.]
    [7. 7. 7.]
    [8. 8. 8.]
    [9. 9. 9.]]


    有没有什么方法让10*3这个tensor,每三行相加?

    解答图片的问题:

    tensorflow方法如下:

    import tensorflow as tf
    a = tf.range(10,dtype=float)
    b = a
    a = tf.reshape(a,[-1,1])
    a = tf.tile(a,[1,3])
    
    
    c = tf.convert_to_tensor([a[i] if i<=1 else tf.reduce_sum(a[i-2:i+1],0)/3 +a[i] for i in range(a.shape[0])])        
            
    sess = tf.Session()
    print(sess.run(b))
    print(sess.run(a))
    print(sess.run(c))

    pytorch方法大致如下:

  • 相关阅读:
    8.11记---我来啦!
    关于博主
    CSP-S 2019 第二轮 游记 AFO
    读入优化
    CSP-S 2019 第一轮 游记
    2019国庆正睿成都集训
    成外集训小记
    收藏夹(持续更新)
    博客建成
    博客施工中,敬请期待
  • 原文地址:https://www.cnblogs.com/qiezi-online/p/13894193.html
Copyright © 2011-2022 走看看