【问题标题】:CNN for Part of Speech tagging Python?用于词性标注 Python 的 CNN?
【发布时间】:2020-04-04 15:26:51
【问题描述】:

我已经尝试使用 CNN 进行词性标注任务。我有一个包含 20 个标签的 4565 个句子的数据集。 word2vec 100 dim 用于词嵌入。现在我尝试使用以下 CNN 算法训练我的分类器。

import pickle
with open('filey.pkl','rb') as f:
    embeddings_index = pickle.load(f)
import numpy as np
MAX_SEQUENCE_LENGTH = 100
EMBEDDING_DIM = 100
TEST_SPLIT = 0.2
VALIDATION_SPLIT =0.2
BATCH_SIZE = 32
with open('data.pkl', 'rb') as f:
    X,y, word2int, int2word, tag2int, int2tag = pickle.load(f)
embedding_matrix = np.random.random((len(word2int) + 1, EMBEDDING_DIM))

for word, i in word2int.items():
    embedding_vector = embeddings_index.get(word)
    if embedding_vector is not None:
        embedding_matrix[i] = embedding_vector
def create_cnn():
    # Add an Input Layer
    input_layer = layers.Input((100, ))

    # Add the word embedding Layer
    embedding_layer = layers.Embedding(len(word2int) + 1, 100, weights=[embedding_matrix], trainable=False)(input_layer)
    embedding_layer = layers.SpatialDropout1D(0.6)(embedding_layer)

    # Add the convolutional Layer
    conv_layer = layers.Convolution1D(100, 3, activation="relu")(embedding_layer)

    # Add the pooling Layer
    pooling_layer = layers.GlobalMaxPool1D()(conv_layer)

    # Add the output Layers
    output_layer1 = layers.Dense(50, activation="relu")(pooling_layer)
    output_layer1 = layers.Dropout(0.25)(output_layer1)
    output_layer2 = layers.Dense(1, activation="sigmoid")(output_layer1)

    # Compile the model
    model = models.Model(inputs=input_layer, outputs=output_layer2)
    model.compile(optimizer=optimizers.Adam(), loss='binary_crossentropy')

    return model

classifier = create_cnn()
y_test = to_categorical(y_test, num_classes=n_tags+1)
test_results = classifier.evaluate(X_test, y_test, verbose=0)
print('TEST LOSS %f \nTEST ACCURACY: %f' % (test_results[0], test_results[1]))

但我得到了错误

Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (201, 100, 21)

有人知道缺少什么或该怎么做吗?任何想法都值得赞赏。

【问题讨论】:

  • 不用把矩阵弄平吗?在最后一个最大池之后。

标签: python machine-learning text classification


【解决方案1】:

Flatten 将矩阵转换为二维(1xn)。

# Add the pooling Layer
pooling_layer = layers.GlobalMaxPool1D()(conv_layer)

# Flatten 
flatten = layers.Flatten()(pooling_layer)

# Add the output Layers
output_layer1 = layers.Dense(50, activation="relu")(flatten)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多