简体   繁体   English

IMDB 数据集示例中的 Keras.network 错误。 不兼容的形状

[英]Error in Keras network on IMDB dataset example. Incompatible shapes

I believe I have correctly vectorized train and test data, labels, adequate layers, a suitable optimizer, but I cannot understand what is wrong.我相信我已经正确矢量化了训练和测试数据、标签、足够的层、合适的优化器,但我不明白哪里出了问题。 Why am I getting a ValueError for incompatible shapes?为什么我会收到不兼容形状的 ValueError?

My code:我的代码:

from keras.datasets import imdb

(train_data, train_labels),(test_data, test_labels)=imdb.load_data(num_words=10000)

import numpy as np

def vectorize_sequences(sequences, dimension=10000):
  results = np.zeros((len(sequences), dimension))
  for i, sequence in enumerate(sequences):
    results[i, sequence] = 1.
  return results

x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)

def to_one_hot(labels, dimension=46):
  results = np.zeros((len(labels), dimension))
  for i, label in enumerate(labels):
    results[i, label] = 1.
  return results

one_hot_train_labels = to_one_hot(train_labels)
one_hot_test_labels = to_one_hot(test_labels)

from tensorflow.keras.utils import to_categorical

one_hot_train_labels = to_categorical(train_labels)
one_hot_test_labels = to_categorical(test_labels)

from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(46, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

x_val = x_train[:1000]
partial_x_train = x_train[1000:]

y_val = one_hot_train_labels[:1000]
partial_y_train = one_hot_train_labels[1000:]

history = model.fit(partial_x_train, partial_y_train, epochs=20, batch_size=512, validation_data=(x_val, y_val)

My error message:我的错误信息:

ValueError: Shapes (None, 1) and (None, 46) are incompatible ValueError:形状(无,1)和(无,46)不兼容

According to the comment, if your partial_y_train shape is (24000, 1) then that means it has not been one hot encoded correctly.根据评论,如果您的partial_y_train形状是 (24000, 1) 那么这意味着它没有被正确地热编码。 You are using the function to_one_hot() in your code, but I dont know what this code is doing.您在代码中使用 function to_one_hot() ,但我不知道这段代码在做什么。 Using Tensorflow's one_hot function or scikit-learn 's version would be best, then the shapes should match and the error will be removed.最好使用 Tensorflow 的one_hot functionscikit-learn的版本,然后形状应该匹配并且错误将被删除。

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

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