【问题标题】:How to parse the data after Bert embedding?Bert嵌入后如何解析数据?
【发布时间】:2019-11-20 13:36:16
【问题描述】:

我正在对新闻中的标题句子进行二进制分类。 (确定新的是否有政治偏见) 我正在使用来自 https://pypi.org/project/bert-embedding/ 的 Bert 嵌入来在 Dataframes 中嵌入训练句子(一个原始的一个标题句子),然后将矢量化数据输入逻辑回归,但是来自 Bert 嵌入的输出数据形状不支持逻辑回归模型。如何解析它以使其适合逻辑回归模型?

在我使用 tifdVectorizer 之前,它可以完美运行,并且输出是 numpy 数组,如

[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]

每一行都是一个句子的向量化数据,它是一个大小为 1903 的数组 我在训练数据中有 516 个标题。 输出形状为:

train_x.shape: (516, 1903) test_x.shape (129, 1903)
train_y.shape: (516,) test_y.shape (129,)

但是在我切换到 Bert_Embedding 之后 ONE 行的输出向量是 numpy 数组列表,如

[list([array([ 9.79349554e-01, -7.06475616e-01 ...... ]dtype=float32),
 array([ ........ ],dtype=float32), ......................
 array([ ........ ],dtype=float32)]

输出形状如下: train_x.shape: (516, 1) test_x.shape (129, 1) train_y.shape: (516,) test_y.shape (129,)

 def transform_to_Bert(articles_file: str, classified_articles_file: str):
    df = get_df_from_articles_file(articles_file, classified_articles_file)
    df_train, df_test, _, _ = train_test_split(df, df.label, stratify=df.label, test_size=0.2)
    bert_embedding = BertEmbedding()
    df_titles_values=df_train.title.values.tolist()
    result_train = bert_embedding(df_titles_values)
    result_test = bert_embedding(df_test.title.values.tolist())
    train_x = pd.DataFrame(result_train, columns=['A', 'Vector'])
    train_x = train_x.drop(columns=['A'])

    test_x = pd.DataFrame(result_test, columns=['A', 'Vector'])
    test_x=test_x.drop(columns=['A'])
    test_x=test_x.values
    train_x=train_x.values
    print(test_x)
    print(train_x)
    train_y = df_train.label.values
    test_y = df_test.label.values
    return {'train_x': train_x, 'test_x': test_x, 'train_y': train_y, 'test_y': test_y, 'input_length': train_x.shape[1], 'vocab_size': train_x.shape[1]}

A 列是结果中的原始标题字符串。所以我就放弃了。


下面是我使用适用于逻辑模型的 tifd vectoriser 的代码。

def transform_to_tfid(articles_file: str, classified_articles_file: str):
    df = get_df_from_articles_file(articles_file, classified_articles_file)
    df_train, df_test, _, _ = train_test_split(df, df.label, stratify=df.label, test_size=0.2)
    vectorizer = TfidfVectorizer(stop_words='english', )
    vectorizer.fit(df_train.title)
    train_x= vectorizer.transform(df_train.title)
    train_x=train_x.toarray()
    print(type(train_x))
    print(train_x)
    test_x= vectorizer.transform(df_test.title)
    test_x=test_x.toarray()
    print(test_x)
    train_y = df_train.label.values
    test_y = df_test.label.values
    return {'train_x': train_x, 'test_x': test_x, 'train_y': train_y, 'test_y': test_y, 'input_length': train_x.shape[1], 'vocab_size': train_x.shape[1]}


model=LogisticRegression(solver='lbfgs')
model.fit(train_x, train_y)

错误是 ValueError: setting an array element with a sequence。 我期望 Bert 的输出形状:train_x.shape: (516, 1) test_x.shape (129, 1) 与 tifd 的输出形状相似:train_x.shape: (516, 1903) test_x.shape (129, 1903)so 它适合逻辑模型

【问题讨论】:

    标签: python numpy nlp embedding


    【解决方案1】:

    好吧,这是我的错误,或者可能是图书馆作者的错误约定:

    [list([array([ 9.79349554e-01, -7.06475616e-01 ...... ]dtype=float32),
     array([ ........ ],dtype=float32), ......................
     array([ ........ ],dtype=float32)]
    

    其实是:

    [[list([array([ 9.79349554e-01, -7.06475616e-01 ...... ]dtype=float32),
         array([ ........ ],dtype=float32), ......................
         array([ ........ ],dtype=float32)]]
    

    所以你必须索引 0 才能达到那个目标

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2021-05-06
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 2020-04-07
      • 2021-01-16
      相关资源
      最近更新 更多