如何解决siamese网络的文本预处理
我想创建一个孪生网络来比较两个字符串的相似度。
我正在关注this tutorial。此示例适用于图像,但我想使用字符串表示(在字符级别)并且我一直在处理文本。
假设我有两个输入:
string_a = ["one","two","three"]
string_b = ["four","five","six"]
我需要准备它以输入我的模型。为此,我需要:
所以我正在尝试以下操作:
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],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],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=(),numpy=b'five'>)
(<tf.Tensor: shape=(),numpy=b'three'>,numpy=b'six'>)
(<tf.Tensor: shape=(),numpy=b'one'>,numpy=b'four'>)
我认为这个错误来自于字符串被函数 from_tensor_slices 转换为张量的事实。但是,为输入预处理这些数据的正确方法是什么?
解决方法
我没有得到你真正想要达到的目标 但如果想将您的文本转换为矢量,这将有所帮助
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。