Python Keras数据集对于训练集和测试集具有不同的向量长度
我正在尝试使用来自Python Keras数据集对于训练集和测试集具有不同的向量长度,python,list,keras,dataset,numpy-ndarray,Python,List,Keras,Dataset,Numpy Ndarray,我正在尝试使用来自keras.datasets的路透社和imdb数据集。标准呼叫是: (x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz", num_words=None, skip_top=0,
keras.datasets
的路透社和imdb数据集。标准呼叫是:
(x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz",
num_words=None,
skip_top=0,
maxlen=None,
seed=113,
start_char=1,
oov_char=2,
index_from=3)
当我检查维度时,train数据集给出了(2500010922),这很有意义。但测试给出了(25000,)。如果转储单个测试数据集元素,如
x\u test[0]
,它将给出一个列表,而不是numpy.array
。问题在于,每行的列表维度都会发生变化,并且始终与列向量维度不同。您应该如何将其用作测试数据?正如您所提到的,x\u train
和x\u test
中的每个元素都是一个列表。该列表包含一个句子(或一个段落,或在本例中是一篇评论)的单词索引,由于句子可能有不同数量的单词,因此相应的表示也有不同的长度。让我们解码其中一个句子,看看它是什么样子,并更加熟悉数据集:
from keras.datasets import imdb
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)
# a mapping from words to their indices, for example `human`: 403
word_index = imdb.get_word_index()
# create the reverse mapping i.e. from indices to words
rev_word_index = {idx:w for w,idx in word_index.items()}
def decode_sentence(s):
# index 0 to 2 are reserved for things like padding, unknown word, etc.
decoded_sent = [rev_word_index.get(idx-3, '[RES]') for idx in s]
return ' '.join(decoded_sent)
print(decode_sentence(x_train[100]))
以人类可读的形式输出所选审查:
[RES]我是大卫·林奇的超级粉丝,拥有他所拥有的一切
用dvd制作,酒店房间除外2小时双峰电影,所以什么时候
我发现了这件事,我立刻抓住了它,这是什么
这是一组由[分辨率]绘制的黑白卡通,声音洪亮
满嘴脏话,毫无幽默感也许我不知道什么好,但也许这个
只是一堆废话,在公众的名义下
大卫·林奇也赚了几块钱让我说清楚我没有
关心粗话部分,但必须保留声音
因为我的邻居们可能对这一切都很有兴趣
令人失望的发布,很可能刚刚被留在[RES]
我强烈建议你不要花你的钱
在这方面,十分之二
这些数据的使用完全取决于您和您试图解决的问题。你可以把句子按原样输入一个网络,这个网络可以处理长度可变的句子。通常由1D卷积层或LSTM层或两者的混合组成。另一种方法是通过将所有句子编码为固定长度编码,使它们具有相同的长度。下面是一个示例,其中一个热编码将每个句子编码为0和1的向量,所有句子的长度固定:
from keras.datasets import imdb
import numpy as np
# you can limit the vocabulary size by passing `num_words` argument
# to ignore rare words and make data more manageable
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)
def encode_sentences(sentences, dim):
encodings = np.zeros((len(sentences), dim))
for idx, sentence in enumerate(sentences):
encodings[idx, sentence] = 1.0
return encodings
x_train = encode_sentences(x_train, 10000)
x_test = encode_sentences(x_test, 10000)
print(x_train.shape)
print(x_test.shape)
哪些产出:
(25000, 10000)
(25000, 10000)
(25000, 500)
(25000, 500)
所有句子都已编码为长度为10000的向量,其中该向量的第i个元素表示索引为i
的单词是否存在于相应的句子中
另一种方法是截断或填充句子,使其长度相同。以下是一个例子:
from keras.datasets import imdb
from keras import preprocessing
n_feats = 10000 # maximum number of words we want to consider in our vocabulary
max_len = 500 # maximum length of each sentence (i.e. truncate those longer
# than 500 words and pad those shorter than 500 words)
# you can limit the vocabulary size by passing `num_words` argument
# to ignore rare words and make it more manageable
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=n_feats)
# preprocess the sequences (i.e. truncate or pad)
x_train = preprocessing.sequence.pad_sequences(x_train, maxlen=max_len)
x_test = preprocessing.sequence.pad_sequences(x_test, maxlen=max_len)
print(x_train.shape)
print(x_test.shape)
哪些产出:
(25000, 10000)
(25000, 10000)
(25000, 500)
(25000, 500)
现在,所有25000个句子的长度都相同,可以随时使用
我强烈建议阅读。我猜可能测试列表只是没有频率的单词索引?我得到了火车和测试的
(25000,)
形状datasets@bhomass我的回答有什么模棱两可的地方吗?是的,你是对的。有一行我忽略了。x_-train=np.array([np.bincount(doc,minlength=V)表示x_-train中的doc])谢谢,示例中参数值的选择使得理解数据集的行为更加困难。