import tensorflow as tf
import numpy as np
a = np.array([[0.2, 0.5, 0.3], [0.4, 0.1, 0.5]])
x = tf.placeholder(tf.float64, [2, 3])
# weighted_sum_op = tf.reduce_sum(x * np.arange(0, a.shape[1]), 1,)
# or if you want to have the range in the TensorFlow graph:
weighted_sum_op = tf.reduce_sum(x * tf.range(0., tf.cast(tf.shape(x)[1], tf.float64)), 1, )
# You ccould also make use of tf.py_func
# weighted_sum_op = tf.py_func(lambda y: np.dot(y, np.arange(0, y.shape[1])), [x], tf.float64)
with tf.Session() as sess:
print(sess.run(weighted_sum_op, {x: a}))
2 回答
这是内在产品的定义 . 使用numpy.dot:
以下代码应该有效: