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)
392



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



