我有一个函数,需要使用大小为(?,61,80)
的2D张量的大小为(40,61)
的Keras batch_dot。维度?
用于自定义层中的批量大小。在使用Keras repeat_elements
时,我们需要指定批量大小以使其为(batch_size, 40,61)
的张量。但是,repeat_elements
不适用于?
批处理大小。
代码是
M1 = K.expand_dims(M,axis=0)
BatchM = K.repeat_elements(x=M1,rep=batch_size,axis=0)
out1 = K.batch_dot(BatchM,Ash1,axes=[2,1])
这里M
是大小(40,61)
的2D张量。 BatchM
应该为(batch_size,40,61)
,而Ash1
的大小为(?,61,80)
。
编辑1:
A= Input(shape=(61,80))
M= K.variable(np.random.rand(40,61))
n=1
import tensorflow as tf
M1 = K.expand_dims(M,axis=0)
BatchM = K.repeat_elements(x=M1,rep=tf.shape(A)[0],axis=0)
out1 = K.batch_dot(BatchM,Ash1,axes=[2,1])
此返回错误显示:
Traceback (most recent call last)
File "<ipython-input-7-edc5ef31181b>", line 3, in <module>
BatchM = K.repeat_elements(x=M1,rep=tf.shape(A)[0],axis=0)
File "/home/hanumant/.conda/envs/kerasenv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2092, in repeat_elements
x_rep = [s for s in splits for _ in range(rep)]
File "/home/hanumant/.conda/envs/kerasenv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2092, in <listcomp>
x_rep = [s for s in splits for _ in range(rep)]
TypeError: 'Tensor' object cannot be interpreted as an integer
答案 0 :(得分:1)
实际上,您无需repeat_elements
使用未知的batch_size。您可以将K.dot()
和K.permute_dimensions
直接用于同一目的。
def customer_dot(a,b):
a = K.permute_dimensions(a, (0, 2, 1)) # x = (?,80,61)
b = K.permute_dimensions(b, (1, 0)) # kernel = (61,40)
ab_dot = K.permute_dimensions(K.dot(a, b), (0, 2, 1)) # ab_dot = (?,40,80)
return ab_dot
A = Input(shape=(61,80))
M = K.variable(np.random.rand(40,61))
result = customer_dot(A,M)
print(result.shape)
# print
(?, 40, 80)
并且您可以使用以下示例来查看结果与代码操作的结果相同。
# print
A = K.constant(np.random.rand(3,2,4))
M = K.constant(np.random.rand(5,2))
M1 = K.expand_dims(M,axis=0)
BatchM = K.repeat_elements(x=M1,rep=K.int_shape(A)[0],axis=0)
out1 = K.batch_dot(BatchM,A,axes=[2,1])
print(K.eval(out1))
result = customer_dot(A,M)
print(K.eval(result))
[[[0.07588554 0.19896106 0.4122516 0.16694324]
[0.02837059 0.07994501 0.15250334 0.05631477]
[0.02922964 0.03180532 0.17185953 0.11346529]
[0.24399586 0.64474815 1.3240533 0.53126353]
[0.06582426 0.0952256 0.38014278 0.22963922]]
[[0.05856805 0.31629622 0.37190455 0.15167782]
[0.02006819 0.12145159 0.1384899 0.0497717 ]
[0.03729554 0.09602766 0.14768752 0.11432388]
[0.18666261 1.0198846 1.1952925 0.481425 ]
[0.07623056 0.2298356 0.33025196 0.22802524]]
[[0.29545793 0.27023914 0.14775626 0.22487558]
[0.10839225 0.10083499 0.05140937 0.07595014]
[0.13047284 0.10567644 0.08779343 0.15208915]
[0.9481214 0.868726 0.47162086 0.7157058 ]
[0.28504598 0.23714545 0.18145116 0.30803293]]]
[[[0.07588554 0.19896106 0.4122516 0.16694324]
[0.02837059 0.07994501 0.15250334 0.05631477]
[0.02922964 0.03180532 0.17185953 0.11346529]
[0.24399586 0.64474815 1.3240533 0.53126353]
[0.06582426 0.0952256 0.38014278 0.22963922]]
[[0.05856805 0.31629622 0.37190455 0.15167782]
[0.02006819 0.12145159 0.1384899 0.0497717 ]
[0.03729554 0.09602766 0.14768752 0.11432388]
[0.18666261 1.0198846 1.1952925 0.481425 ]
[0.07623056 0.2298356 0.33025196 0.22802524]]
[[0.29545793 0.27023914 0.14775626 0.22487558]
[0.10839225 0.10083499 0.05140937 0.07595014]
[0.13047284 0.10567644 0.08779343 0.15208915]
[0.9481214 0.868726 0.47162086 0.7157058 ]
[0.28504598 0.23714545 0.18145116 0.30803293]]]