【问题标题】:Preprocessing text for siamese network孪生网络的文本预处理
【发布时间】:2021-07-13 12:35:31
【问题描述】:

我想创建一个连体网络来比较两个字符串的相似度。

我正在尝试关注this tutorial。此示例适用于图像,但我想使用字符串表示(在字符级别)并且我被困在文本的预处理中。

假设我有两个输入:

string_a = ["one","two","three"]
string_b = ["four","five","six"]

我需要为输入我的模型做好准备。为此,我需要:

  • 创建分词器
  • 创建一个 tf 数据框
  • 预处理此数据帧(标记化输入)

所以我正在尝试以下方法:

    import tensorflow as tf
    from tensorflow.keras.preprocessing.text import Tokenizer
    from tensorflow.keras.preprocessing.sequence import pad_sequences

    #create a tokenizer
    tok = Tokenizer(char_level=True,oov_token="?")
    tok.fit_on_texts(string_a+string_b)
    char_index = tok.word_index
    maxlen = max([len(x) for x in tok.texts_to_sequences(string_a+string_b)])
    
    #create a dataframe
    dataset_a = tf.data.Dataset.from_tensor_slices(string_a)
    dataset_b = tf.data.Dataset.from_tensor_slices(string_b)
    
    dataset = tf.data.Dataset.zip((dataset_a,dataset_b))
    
    # preprocessing functions
    def tokenize_string(data,tokenizer,max_len):
        """vectorize string with a given tokenizer
        """
        sequence = tokenizer.texts_to_sequences(data)
        return_seq = pad_sequences(sequence,maxlen=max_len,padding="post",truncating="post")
        return return_seq[0]
    
    def preprocess_couple(string_1,string_2):
        """given 2 strings, tokenize them and return an array
        """
        return (
            tokenize_string([string_1], tok, maxlen),
            tokenize_string([string_2], tok, maxlen)
        )
    
    #shuffle and preprocess dataset
    dataset = dataset.shuffle(buffer_size=2)
    dataset = dataset.map(preprocess_couple)

但是我得到一个错误:

AttributeError: in user code:

    <ipython-input-29-b920d389ea82>:29 preprocess_couple  *
        tokenize_string([string_2], tok, maxlen)
    <ipython-input-29-b920d389ea82>:20 tokenize_string  *
        sequence = tokenizer.texts_to_sequences(data)
    C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\embargo_text\lib\site-packages\keras_preprocessing\text.py:281 texts_to_sequences  *
        return list(self.texts_to_sequences_generator(texts))
    C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\embargo_text\lib\site-packages\keras_preprocessing\text.py:306 texts_to_sequences_generator  **
        text = text.lower()
    C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\embargo_text\lib\site-packages\tensorflow\python\framework\ops.py:401 __getattr__
        self.__getattribute__(name)

preprocess_couple函数应用前的数据集状态如下:

(<tf.Tensor: shape=(), dtype=string, numpy=b'two'>, <tf.Tensor: shape=(), dtype=string, numpy=b'five'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'three'>, <tf.Tensor: shape=(), dtype=string, numpy=b'six'>)
(<tf.Tensor: shape=(), dtype=string, numpy=b'one'>, <tf.Tensor: shape=(), dtype=string, numpy=b'four'>)

我认为这个错误来自于字符串被函数from_tensor_slices转换为张量的事实。但是,为输入预处理这些数据的正确方法是什么?

【问题讨论】:

    标签: python tensorflow nlp siamese-network


    【解决方案1】:

    我没有得到你真正想要达到的目标 但如果想将您的文本转换为矢量,这将有所帮助

    def process(data):
        tok = Tokenizer(char_level=True,oov_token="?")
        tok.fit_on_texts(data)
        maxlen = max([len(x) for x in tok.texts_to_sequences(data)])
        data=tok.texts_to_sequences(data)
        data=pad_sequences(data,maxlen=maxlen,padding='post')
        return data
    

    【讨论】:

    • colab 示例遵循以下逻辑:
    • 关注repo
    • 我使用的示例采用文件名,形成一个包含 3 列的数据集(在此示例中我使用 2)。每列都包含一个文件名(在我的例子中只是一个字符串名),它将被输入到网络中。将文件名加载到张量数据帧后,每一行都会使用一个函数处理,该函数获取每一列,然后读取和预处理图像(返回每个单元格的向量)。所以我想做的是类似的。将名称加载到张量数据集中,形成一个包含 3 列的数据集,并将每个单元格变成 vec
    猜你喜欢
    • 1970-01-01
    • 2018-10-10
    • 2013-05-04
    • 2014-09-11
    • 1970-01-01
    • 2019-07-20
    • 2018-10-19
    • 1970-01-01
    相关资源
    最近更新 更多