tensorflow-累加accumulate_n,add_n

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 27 11:16:32 2018

@author: myhaspl
"""

# TensorFlow and tf.keras
import tensorflow as tf
a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 0], [0, 6]]) 
c=tf.ones([2,2],tf.int32)
result1=tf.accumulate_n([a, b])
result2=tf.accumulate_n([a, b,c])

result3=tf.accumulate_n([a, b, c], shape=[2, 2], tensor_dtype=tf.int32)

sess=tf.Session()
with sess:
    print sess.run(result1)
    print sess.run(result2)
    print sess.run(result3)

[[ 6  2]
 [ 3 10]]
[[ 7  3]
 [ 4 11]]
[[ 7  3]
 [ 4 11]]

import tensorflow as tf
a = tf.constant([[1., 2.], [3., 4.]])
b = a*2-4
c= (b-1)*2

res=tf.add_n([a,b,c])

sess=tf.Session()
with sess:
    print sess.run(res)

[[-7.  0.]
 [ 7. 14.]]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章