python - 如何将张量连接到批次中的 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 轴以避免平铺?
提前致谢
解决方案
您想将沿批量维度的形状(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)
推荐阅读
- jenkins - 参数化远程触发器正在阻止作业,直到远程作业被触发
- c++ - C ++在没有其他库的位图图像上绘制文本
- azure-cosmosdb - Azure Cosmos + EF Core 3.1 问题与 LocaView.Remove 和后续 Context.SaveChangeAsync()
- php - 检查字符串是否包含 2 个数组中的值
- flutter - 如何从可拖动的孩子触发拖动事件
- java - 尝试 Catch Exception 未按预期工作
- html - 无法将光标放在内容可编辑 div 中的非内容可编辑 div 前面
- android - 新的 Android 应用可以替换 Play 商店中的现有应用吗?
- sql - 使用正则表达式在红移中用逗号和引号分割字符串
- c++ - 要求用户输入一个整数,如果它不是整数,要求用户重新输入值 c++