在keras中使用textvectoriation和嵌入层创建子模型抛出:“str”对象在keras中没有“base\u dtype”属性

p4tfgftt  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(276)

我用文本和数值数据做了一个多输入的tensorflow nlp模型。为了创建这个模型,我计划创建两个子模型,一个用于文本,另一个用于数字数据,然后将它们的输出连接到我的主模型中。对于文本子模型,我一直遵循keras的文本矢量化和嵌入指南(https://www.tensorflow.org/tutorials/text/word_embeddings#configure_the_dataset_for_performance 以及https://www.tensorflow.org/api_docs/python/tf/keras/layers/experimental/preprocessing/textvectorization)使用tf-idf加权,索引所有大字图。这是文本矢量化层的代码:


# Instantiate TextVectorization with "tf-idf" output_mode

# (multi-hot with TF-IDF weighting) and ngrams=2 (index all bigrams)

text_vectorizer = preprocessing.TextVectorization(output_mode="tf-idf", ngrams=2)

# Index the bigrams and learn the TF-IDF weights via `adapt()`

text_vectorizer.adapt(df['tweet_punct'].dropna().to_numpy())
print('Size of vocabulary:', len(text_vectorizer.get_vocabulary()))
vocab_size = len(text_vectorizer.get_vocabulary())

当我尝试将向量化层连接到嵌入层时,会出现错误。以下是我一直在使用的脚本:

embedding_layer = Embedding(vocab_size, 100)(text_vectorizer)
LSTM_layer_1 = LSTM(128)(embedding_layer)

根据我能找到的与这个问题相关的另一个问题:“str”对象没有属性“base\u dtype”error tensorflow model层之间相互添加的方式应该是正确的,但是运行这个给我 AttributeError: 'str' object has no attribute 'base_dtype' 在第一行。我如何连接这两层有问题吗?我对tensorflow有点陌生,从来没有尝试过这样做模型,所以我对这里发生的事情有点迷茫。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题