首页 > 解决方案 > 如何将张量连接到批次中的 keras 层(不指定批次大小)?

问题描述

我想将嵌入层的输出与自定义张量 ( myarr/ myconst) 连接起来。我可以指定具有固定批量大小的所有内容,如下所示:

import numpy as np
import tensorflow as tf

BATCH_SIZE = 100
myarr = np.ones((10, 5))
myconst = tf.constant(np.tile(myarr, (BATCH_SIZE, 1, 1)))

# Model definition
inputs = tf.keras.layers.Input((10,), batch_size=BATCH_SIZE)
x = tf.keras.layers.Embedding(10, 5)(inputs)
x = tf.keras.layers.Concatenate(axis=1)([x, myconst])
model = tf.keras.models.Model(inputs=inputs, outputs=x)

但是,如果我不指定批量大小并平铺我的数组,即以下...

myarr = np.ones((10, 5))
myconst = tf.constant(myarr)

# Model definition
inputs = tf.keras.layers.Input((10,))
x = tf.keras.layers.Embedding(10, 5)(inputs)
x = tf.keras.layers.Concatenate(axis=1)([x, myconst])
model = tf.keras.models.Model(inputs=inputs, outputs=x)

...我收到一个错误,指出[(None, 10, 5), (10, 5)]无法连接形状。有没有办法添加这个None/ batch_size 轴以避免平铺?

提前致谢

标签: pythontensorflowkeras

解决方案


您想将沿批量维度的形状(batch, 10, 5)常数连接到形状的 3D 张量。(10, 5)为此,您的常量必须是 3D。所以你必须重塑它(1, 10, 5)并沿着它重复它axis=0以匹配形状(batch, 10, 5)并操作连接。

Lambda我们在层内执行此操作:

X = np.random.randint(0,10, (100,10))
Y = np.random.uniform(0,1, (100,20,5))

myarr = np.ones((1, 10, 5)).astype('float32')
myconst = tf.convert_to_tensor(myarr)

def repeat_const(tensor, myconst):
    shapes = tf.shape(tensor)
    return tf.repeat(myconst, shapes[0], axis=0)

inputs = tf.keras.layers.Input((10,))
x = tf.keras.layers.Embedding(10, 5)(inputs)
xx = tf.keras.layers.Lambda(lambda x: repeat_const(x, myconst))(x)
x = tf.keras.layers.Concatenate(axis=1)([x, xx])
model = tf.keras.models.Model(inputs=inputs, outputs=x)
model.compile('adam', 'mse')

model.fit(X, Y, epochs=3)

推荐阅读