假设有一个输入矩阵的维度 N 未知(可能是 2D、3D 或某个 D)到接收端:
import tensorflow as tf
m = tf.keras.backend.random_normal([2, 3,4], mean=6, stddev=0.1, seed = 1) # a 3D matrix
或者
m = tf.keras.backend.random_normal([3], mean=6, stddev=0.1, seed = 1) # a 1D matrix
或者
m = tf.keras.backend.random_normal([3,7], mean=6, stddev=0.1, seed = 1). # a 2D matrix
在接收端,我需要添加矩阵中的所有元素。我怎么做?
我在想某种 for 循环
for n in N # N is the dimension of the matrix
sum = tf.reduce_sum(m, axis=-1)
我不确定如何进入N
接收端,还是应该以不同的方式完成?在 MATLAB 中,它就像
sum_of_a_m = sum(m(:));
非常感谢。非常感谢几行代码。
你能用这个吗:
import tensorflow as tf
import numpy as np
m = tf.keras.backend.random_normal([3], mean=6, stddev=0.1, seed = 1)
ma = tf.keras.backend.eval(m)
sum_elements = ma.sum()
print(sum_elements)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句