【发布时间】:2017-11-27 19:19:52
【问题描述】:
例如,在 TensorFlow 中,
tf.matmul(x, y) 等价于 python3.5+ 中的x @ y。
如果没有函数告诉它,Tensorflow 如何隐式知道将x @ y 操作添加到图中?它是在 C 后端完成的吗?我四处寻找,没有找到喜欢的东西。
【问题讨论】:
标签: python-3.x tensorflow deep-learning
例如,在 TensorFlow 中,
tf.matmul(x, y) 等价于 python3.5+ 中的x @ y。
如果没有函数告诉它,Tensorflow 如何隐式知道将x @ y 操作添加到图中?它是在 C 后端完成的吗?我四处寻找,没有找到喜欢的东西。
【问题讨论】:
标签: python-3.x tensorflow deep-learning
使用运算符重载是可能的。
x 和 y 都具有 Tensor 类型。 Tensor 实现了__matmul__ 方法(对应于@ 运算符)。
__matmul__ 的实现可能如下所示:
def __matmul__(self, other):
return tf.matmul(self, other)
关于运算符重载的官方文档:link。
【讨论】: