我目前正在解决一个练习,其中涉及阅读TED演讲,根据演讲主题为他们加标签,并在Keras中训练前馈NN,并使用预先训练的词嵌入技术为相应的新演讲加标签。
根据讨论的内容(技术,教育或设计或其中多个主题),可以具有以下标签之一:
labels_dict = {
'txx': 0, 'xex': 1, 'xxd': 2, 'tex': 3, 'txd': 4, 'xed': 5, 'ted': 6, 'xxx': 7
}
我这样加载演讲:
def load_talks(path):
tree = et.parse(path)
root = tree.getroot()
for file in root:
label = ''
keywords = file.find('head').find('keywords').text.lower()
if 'technology' in keywords:
label += 't'
else:
label += 'x'
if 'education' in keywords:
label += 'e'
else:
label += 'x'
if 'design' in keywords:
label += 'd'
else:
label += 'x'
talk = file.find('content').text
talk = process_text(talk)
texts.append(talk)
labels.append(labels_dict[label])
然后我为文本中的令牌计算TF-IDF分数:
tf_idf_vect = TfidfVectorizer()
tf_idf_vect.fit_transform(texts)
tf_idf_vectorizer_tokens = tf_idf_vect.get_feature_names()
然后,我使用令牌生成器将文本中的令牌分配给索引:
t = Tokenizer()
t.fit_on_texts(texts)
vocab_size = len(t.word_index) + 1
encoded_texts = t.texts_to_sequences(texts)
print('Padding the docs')
# pad documents to a max length of 4 words
max_length = max(len(d) for d in encoded_texts)
padded_docs = pad_sequences(encoded_texts, maxlen=max_length, padding='post')
接下来,我计算嵌入矩阵:
def compute_embedding_matrix(word_index, embedding_dim):
embedding_matrix = np.zeros((len(word_index) + 1, embedding_dim))
for word, i in word_index.items():
embedding_vector = word_embeddings.get(word)
if embedding_vector is not None and get_tf_idf_score(word) > TF_IDF_THRESHOLD:
# words not found in embedding index and with a too low tf-idf score will be all-zeros.
embedding_matrix[i] = embedding_vector
return embedding_matrix
embedding_dim = load_word_embeddings('word_embeddings/de/de.tsv') + 1
embedding_matrix = compute_embedding_matrix(t.word_index, embedding_dim)
然后我准备标签并在训练和测试中拆分数据:
labels = to_categorical(np.array(labels))
X_train, X_test, y_train, y_test = train_test_split(padded_docs, labels, test_size=0.1, random_state=0)
以下打印输出:
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
(1647, 6204)
(184, 6204)
(1647, 8)
(184, 8)
然后我像这样准备模型:
e = Embedding(input_dim=vocab_size,
weights=[embedding_matrix],
input_length=max_length,
output_dim=embedding_dim,
trainable=False)
print('Preparing the network')
model = models.Sequential()
model.add(e)
model.add(layers.Flatten())
model.add(layers.Dense(units=100, input_dim=embedding_dim, activation='relu'))
model.add(layers.Dense(len(labels), activation='softmax'))
# compile the model
model.compile(loss='binary_crossentropy', metrics=['acc'])
# summarize the model
print(model.summary())
# fit the model
print('Fitting the model')
model.fit(X_train, y_train, epochs=10, verbose=0, batch_size=500)
# evaluate the model
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print('Accuracy: %f' % (accuracy*100))
这将输出以下错误:
embedding_1 (Embedding) (None, 6204, 301) 47951106
_________________________________________________________________
flatten_1 (Flatten) (None, 1867404) 0
_________________________________________________________________
dense_1 (Dense) (None, 100) 186740500
_________________________________________________________________
dense_2 (Dense) (None, 1831) 184931
=================================================================
Total params: 234,876,537
Trainable params: 186,925,431
Non-trainable params: 47,951,106
_________________________________________________________________
None
Fitting the model
batch_size=batch_size)
File "/Users/tim/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
exception_prefix='target')
File "/Users/tim/anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data
str(data_shape))
ValueError: Error when checking target: expected dense_2 to have shape (1831,) but got array with shape (8,)
Process finished with exit code 1
有人可以为我指出正确的方向,以适应该模型的尺寸吗?
答案 0 :(得分:0)
我发现了问题:
最后一个密集层应该有8个单位,因为我有8个标签。