gpt4 book ai didi

python - keras中填充整数和字符串的区别

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:41 25 4
gpt4 key购买 nike

我正在尝试为 seq2seq 模型填充文本。

from keras_preprocessing.sequence import pad_sequences

x=[["Hello, I'm Bhaskar", "This is Keras"], ["This is an", "experiment"]]
pad_sequences(sequences=x, maxlen=5, dtype='object', padding='pre', value="<PAD>")

我遇到以下错误:

ValueError: `dtype` object is not compatible with `value`'s type: <class 'str'>
You should set `dtype=object` for variable length strings.

但是,当我尝试对整数执行相同操作时,效果很好。

x=[[1, 2, 3], [4, 5, 6]]
pad_sequences(sequences=x, maxlen=5, padding='pre', value=0)

Output:
array([[0, 0, 1, 2, 3],
[0, 0, 4, 5, 6]], dtype=int32)

我希望得到的输出为:

[["<PAD>", "<PAD>", "<PAD>", "Hello, I'm Bhaskar", "This is Keras"], ["<PAD>", "<PAD>","<PAD>", "This is an", "experiment"]]

最佳答案

根据错误的建议,将dtype更改为object(不是字符串,而是对象本身),它会为您完成这项工作。

from keras.preprocessing.sequence import pad_sequences

x=[["Hello, I'm Bhaskar", "This is Keras"], ["This is an", "experiment"]]
pad_sequences(sequences=x, maxlen=5, dtype=object, padding='pre', value="<PAD>")

输出

array([['<PAD>', '<PAD>', '<PAD>', "Hello, I'm Bhaskar", 'This is Keras'],
['<PAD>', '<PAD>', '<PAD>', 'This is an', 'experiment']],
dtype=object)

关于python - keras中填充整数和字符串的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55220072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com