Python Tensorflow文本矢量化在model.summary()中没有任何形状
我正在使用Python Tensorflow文本矢量化在model.summary()中没有任何形状,python,tensorflow,keras,Python,Tensorflow,Keras,我正在使用预处理类中的文本向量化对象中的编码器。然后我调整我的列车数据,如下所示: encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(max_tokens=1000) encoder.adapt(dataset_all.map(lambda text, label: text)) 然后我想运行一个具有密集层的简单神经网络。这是我的模型 model = tf.keras.Sequential([
预处理
类中的文本向量化
对象中的编码器
。然后我调整我的列车数据,如下所示:
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(max_tokens=1000)
encoder.adapt(dataset_all.map(lambda text, label: text))
然后我想运行一个具有密集层的简单神经网络。这是我的模型
model = tf.keras.Sequential([
tf.keras.Input(shape=(1,), dtype=tf.string),
encoder,
tf.keras.layers.Embedding(len(encoder.get_vocabulary())+1
,output_dim=64,mask_zero=True),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(4, activation='softmax')
])
打印摘要时,我会得到以下信息:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
text_vectorization (TextVect multiple 0
_________________________________________________________________
embedding_39 (Embedding) (None, None, 64) 6142528
_________________________________________________________________
dense_74 (Dense) (None, None, 64) 4160
_________________________________________________________________
dense_75 (Dense) (None, None, 4) 260
=================================================================
Total params: 6,146,948
Trainable params: 6,146,948
Non-trainable params: 0
我不明白每个输出DIM中的第二个None
代表什么。此外,当我尝试拟合模型时,我得到一个错误消息(使用sparseCategoricalCrossEntropy
loss):
因为我需要一个2D输出用于最终的稠密层,所以我尝试在嵌入层之后添加一个展平层,但是它不起作用,因为稠密层输入的第二个维度是未指定的
如果我在嵌入层之后添加一个RNN层,网络训练会正确,因为嵌入层的输出是3D的,但是,我不知道如何只拥有密集层。这是因为您没有指定指示
编码器的输出形状的参数,即输出\序列\长度
output\u sequence\u length:如果设置,则输出将填充或截断其时间维度,以精确输出\u sequence\u length值,从而产生形状张量[batch\u size,output\u sequence\u length],而不管拆分步骤产生了多少标记。默认为“无”
如果将其设置为数字,您将看到将定义图层的输出形状:
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(
max_tokens=1000,
output_sequence_length=200
)
之后,可以使用globalaveragepoolg1d
层进行二维输出
阅读嵌入后添加globalaveragepoolg1d
怎么样?
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(
max_tokens=1000,
output_sequence_length=200
)
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
text_vectorization_3 (TextVe (None, 200) 0
_________________________________________________________________
embedding_2 (Embedding) (None, 200, 64) 448
_________________________________________________________________
dense_4 (Dense) (None, 200, 64) 4160
_________________________________________________________________
dense_5 (Dense) (None, 200, 4) 260
=================================================================
Total params: 4,868
Trainable params: 4,868
Non-trainable params: 0
_________________________________________________________________