Как решить аргумент «длины» должен быть 1D CPU int64? - PullRequest
0 голосов
/ 01 апреля 2020

Я работаю над моделью LSTM с многотекстовой классификацией, но при предварительной обработке текста я получаю сообщение об ошибке, которого раньше не было. Я думаю, что это было обновление: standfordnlp.


Код где я получаю сообщение об ошибке:

modules:

# StanfordNLP
!pip install stanfordnlp
import stanfordnlp
stanfordnlp.download('es', confirm_if_exists = True, version = 'latest')
stNLP = stanfordnlp.Pipeline(processors='tokenize,mwt,pos,lemma', lang='es', treebank = 'es_ancora', use_gpu=True)

# SpaCy
!spacy download es_core_news_sm # sm md
import spacy
spNLP = spacy.load('es_core_news_sm') #sm md
activated = spacy.prefer_gpu()
spacy.require_gpu()

import pandas as pd
import numpy as np

Избавление от стоп-слов:

def get_stop_words(): 
  # Getting in a list all the stopwords of the dataframe with is_stop() from SpaCy
  spacy_stop_words = list(dict.fromkeys([str(i) for i in spNLP(' '.join([elem for elem in new_df['descripcion']])) if i.is_stop == True]))

  stop_words = stopwords.words('spanish') # defining the language
  stop_words.extend(spec_stopwords) # extending the specific stopwords
  stop_words.extend(spacy_stop_words) # extending the spacy stopwords
  stop_words = set(stop_words)
  return stop_words

stop_words = get_stop_words() # defining the stop_words set in a variable to better understanding whe applying on the dataframe

# Applying stopwords on the dataframe
new_df['descripcion'] = new_df['descripcion'].apply(lambda x: ' '.join([word for word in x.split() if word not in stop_words]))

Лемматизация:

def stanford_lemma(text):
  doc = stNLP(text)
  return ' '.join([word.lemma for sent in doc.sentences for word in sent.words])

# Lemmatization of dataframe
new_df['descripcion'] = new_df['descripcion'].apply(lambda x: stanford_lemma(x))

# Getting new stop_words after lemmatization
get_stop_words()

# applying new stop_words on the dataframe
new_df['descripcion'] = new_df['descripcion'].apply(lambda x: ' '.join(
    [word for word in x.split() if word not in stop_words]))

Traceback :

RuntimeError                              Traceback (most recent call last)
<ipython-input-18-60972fc225b2> in <module>()
----> 1 new_df['descripcion'] = new_df['descripcion'].apply(lambda x: stanford_lemma(x))
      2 
      3 # Getting new stop_words after lemmatization (Lemmatizing: personalidades, personlidad = stopword)
      4 get_stop_words()
      5 

9 frames
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

/usr/local/lib/python3.6/dist-packages/torch/nn/utils/rnn.py in pack_padded_sequence(input, lengths, batch_first, enforce_sorted)
    231 
    232     data, batch_sizes = \
--> 233         _VF._pack_padded_sequence(input, lengths, batch_first)
    234     return PackedSequence(data, batch_sizes, sorted_indices, None)
    235 

RuntimeError: 'lengths' argument should be a 1D CPU int64 tensor

Обновление: с использованием новой библиотеки Stanza, возникает та же проблема. Проблема не исчезнет, ​​даже если я попытаюсь использовать пример Lemma :

!pip install stanza
import stanza

stanza.download('es', package='ancora', processors='tokenize,mwt,pos,lemma', verbose=True)
stNLP = stanza.Pipeline(processors='tokenize,mwt,pos,lemma',
                        lang='es',
                        use_gpu=True)

doc = nlp('Barack Obama nació en Hawaii.')
print(*[f'word: {word.text+" "}\tlemma: {word.lemma}' for sent in doc.sentences for word in sent.words], sep='\n')

Запросы: Набор данных (@Crystina) : new_df


Будут рассмотрены любые предложения по улучшению вопроса.


...