tensorflow - Tensorflow:将矩阵列乘以向量的元素并返回矩阵
问题描述
这是我想在 Tensorflow 中完成的任务。
我有 2x2 矩阵(可训练)
x_1 x_2
x_3 x_4
我有输入向量
a
b
我想将矩阵的每一列乘以向量的元素并返回以下矩阵
ax_1 bx_2
ax_3 bx_4
我可以通过将矩阵的每一列声明为单独的变量来得到这个结果,但我想知道是否有更优雅的解决方案。
解决方案
感谢广播,您应该可以使用常规乘法运算符:
import tensorflow as tf
x = tf.constant([[3, 5], [7, 11]], dtype=tf.int32)
a = tf.constant([4, 8], dtype=tf.int32)
y = x * a
with tf.Session() as sess:
print(sess.run(y)) # Result: [[12, 40], [28, 88]]
推荐阅读
- amazon-web-services - 从 AWS 服务访问放置在 VPN 后面的 Rest Endpoint
- javascript - 视频标签未在 Safari 中加载
- r - 需要解决的范围问题
- c# - C#。在达到 5 次失败尝试后,消息“您已用完猜测!它是数字”+ 数字永远不会显示,我需要帮助。谢谢
- azure-devops - Azure 管道自托管代理 - 是否应该有单独的用户来构建和部署?
- mysql - SQL:只要有 5 的倍数就增加一个新列,否则不要
- c# - PowerPDF API - 将 VB 代码转换为 C# - 错误 HRESULT E_FAIL
- python - 在python中将数据提取到矩阵或numpy数组中的有效方法
- scala - 如何解决不同版本的传递依赖?
- c++ - 在程序中找到最小的变量?