gpt4 book ai didi

python - 如何在 Tensorflow 中为数组创建 np.subtract.outer

转载 作者:太空宇宙 更新时间:2023-11-03 20:42:13 25 4
gpt4 key购买 nike

假设x是一个二维数组,其中X1 = [a,b]并且a,b只是标量

然后输入如下:x = [X1, X2, X3]

我想计算:

y = [ [X1-X1, X1-X2, X1-X3],
[X2-X1, X2-X2, X2-X3],
[X3-X1, X3-X2, X3-X3] ]

最佳答案

这是使用隐式广播来实现此目的的一种方法:

import tensorflow as tf
import numpy as np

X_np = np.array([[1, 2], [3, 4], [10, 10]])

X_tf = tf.placeholder(tf.float32, shape=(None, None,))
y_tf = X_tf[:, None, :] - X_tf[None, :, :]

with tf.Session() as sess:
out = sess.run(y_tf, feed_dict={X_tf: X_np})
print(out) # prints [[[ 0. 0.]
# [-2. -2.]
# [-9. -8.]]
#
# [[ 2. 2.]
# [ 0. 0.]
# [-7. -6.]]
#
# [[ 9. 8.]
# [ 7. 6.]
# [ 0. 0.]]]

关于python - 如何在 Tensorflow 中为数组创建 np.subtract.outer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56796185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com