batch_dot实现
载入库
import tensorflow as tf
tf.enable_eager_execution()
import keras.backend as K
import numpy as np
生成输入数据
x1 = tf.convert_to_tensor([[1,2,3],[4,5,6]])
x2 = tf.convert_to_tensor([[1,2,3],[4,5,6]])
K.batch_dot(x1,x2,axes=1).numpy()
axes为1 的batch_dot输出如下:
array([[14],
[77]], dtype=int32)
axes为2的batch_dot输出如下:
K.batch_dot(x1,x2,axes=0).numpy()
array([[17],
[29],
[45]], dtype=int32)
实际上与先经过位对位乘法然后按某一个轴作聚合加法返回的结果一直,下面是验证结果。
tf.reduce_sum(tf.multiply(x1 , x2) , axis=0).numpy()
array([17, 29, 45], dtype=int32)
tf.reduce_sum(tf.multiply(x1 , x2) , axis=1).numpy()
array([14, 77], dtype=int32)
本文详细介绍了在TensorFlow框架下使用Keras的batch_dot函数进行矩阵运算的方法。通过实例演示了不同axes参数设置下batch_dot的计算过程,并与tf.reduce_sum和tf.multiply结合使用验证结果的一致性。
928

被折叠的 条评论
为什么被折叠?



