gpt4 book ai didi

python - 具有未知batch_size的Keras重复元素

转载 作者:行者123 更新时间:2023-12-01 01:09:56 32 4
gpt4 key购买 nike

我有一个函数,我需要对大小为 (?,61,80) 的张量和大小为 (40,61) 的 2D 张量执行 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)的二维张量。 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

最佳答案

事实上,您不需要使用未知的batch_size来repeat_elements。您可以直接使用 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]]]

关于python - 具有未知batch_size的Keras重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54960227/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com