简体   繁体   English

Dask 分区或在 NLP 节预处理中延迟

[英]Dask Partitions or Delayed in a NLP Stanza preocess

I´m working over a NLP process with Stanza.我正在使用 Stanza 处理 NLP 流程。 Stanza takes a long time to run the NLP process and I understand that my problem is quite partitionable. Stanza 需要很长时间才能运行 NLP 进程,我知道我的问题很容易分区。

I use these libraries我使用这些库

pip install stanza
import stanza
stanza.download('es')
nlp = stanza.Pipeline(lang='es')
import pandas as pd
import dask.dataframe as dd
import dask
import datetime

I have the following function我有以下 function

data_text = pd.DataFrame({'text': ["hola mi nombre es juancito y dedico mucho tiempo de mi día a estudiar. Gracias a Dios me gustan mucho las matematicas y las ciencias naturales. Este es un mensaje de ejemplo.",
                                   "hola mi nombre es juancito y dedico mucho tiempo de mi día a estudiar. Gracias a Dios me gustan mucho las matematicas y las ciencias naturales. Este es un mensaje de ejemplo.",
                                   "hola mi nombre es juancito y dedico mucho tiempo de mi día a estudiar. Gracias a Dios me gustan mucho las matematicas y las ciencias naturales. Este es un mensaje de ejemplo.",
                                   "hola mi nombre es juancito y dedico mucho tiempo de mi día a estudiar. Gracias a Dios me gustan mucho las matematicas y las ciencias naturales. Este es un mensaje de ejemplo.",
                                   "hola mi nombre es juancito y dedico mucho tiempo de mi día a estudiar. Gracias a Dios me gustan mucho las matematicas y las ciencias naturales. Este es un mensaje de ejemplo.",
                                   "hola mi nombre es juancito y dedico mucho tiempo de mi día a estudiar. Gracias a Dios me gustan mucho las matematicas y las ciencias naturales. Este es un mensaje de ejemplo."]})

def concept_const_func(data_text_inp):
  beginning = datetime.datetime.now()

  # Data
  data_text_func = data_text_inp.reset_index(drop=True)

  # Consolidation
  df_tw_out = pd.DataFrame({'tw': ["drop"]})

  for i in range(0,len(data_text_func)):
    # Text
    tweet_test = data_text_func["text"][i]

    # NLP
    doc_review = nlp(tweet_test)

    # Principales Definiciones
    print(i)
    for sent in doc_review.sentences:
        for dep in sent.dependencies:
            if dep[1] == 'nsubj':
              df_tw_aux = pd.DataFrame({"tw" : [dep[0].text + " " + dep[2].text]})
              df_tw_out = pd.concat([df_tw_out, df_tw_aux])

  ending = datetime.datetime.now()
  print(ending-beginning)
  return df_tw_out

When i run the code with pandas or dask delayed i get the same result in terms of execution time.当我使用 pandas 或 dask delayed 运行代码时,我在执行时间方面得到了相同的结果。

# Just Pandas
df_pd = concept_const_func(data_text)

# Dask Delayes
df_dd = dask.delayed(concept_const_func)(data_text)
df_dd.compute()

I also tried to solve it with a map_partition() but couldn't get it to work correctly.Mainly because the most time-consuming part of the code is the NLP() and I can't figure out how to use the DASK partition for this process that needs to input a str.我也尝试用 map_partition() 解决它,但无法使其正常工作。主要是因为代码中最耗时的部分是 NLP() 而我不知道如何使用 DASK 分区对于这个需要输入 str 的过程。

Can anyone think of an alternative to solve the problem (reduce the execution times of that code by partitioning the NLP())?谁能想到解决问题的替代方法(通过划分 NLP() 来减少该代码的执行时间)?

Thank you!谢谢!

I understand you're trying to apply a computation row-wise of an input dataframe. In this case, in order to parallelize with Dask, you should do something like:我知道你正在尝试对输入 dataframe 进行逐行计算。在这种情况下,为了与 Dask 并行化,你应该这样做:

def nlp_apply(row):
    # Text
    tweet_test = row["text"]

    # NLP
    doc_review = nlp(tweet_test)

    # Principales Definiciones
    for sent in doc_review.sentences:
        for dep in sent.dependencies:
            if dep[1] == 'nsubj':
              df_tw_aux = pd.DataFrame({"tw" : [dep[0].text + " " + dep[2].text]})

    return df_tw_aux

res = ddf.apply(nlp_apply, axis=1).compute()

There is probably some things to modify about the return result of the nlp_apply function. nlp_apply function 的返回结果可能有一些需要修改的地方。

In order to parallelize correctly, you'll also need to chunk the dask dataframe appropriately (with several partitions, https://docs.dask.org/en/stable/dataframe-create.html ), and you'll may need to use another Dask scheduler than the default one ( https://docs.dask.org/en/stable/scheduling.html ).为了正确并行化,您还需要适当地分块 dask dataframe(有几个分区, https://docs.dask.org/en/stable/dataframe-create.html ),您可能需要使用不同于默认调度程序的另一个 Dask 调度程序 ( https://docs.dask.org/en/stable/scheduling.html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM