【发布时间】:2021-09-21 11:42:25
【问题描述】:
我想将嵌入层的输出与自定义张量 (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 轴来避免平铺?
提前致谢
【问题讨论】:
标签: python tensorflow keras