Machine learning 值错误-检查目标时出错-LSTM 关于数据集

Machine learning 值错误-检查目标时出错-LSTM 关于数据集,machine-learning,keras,lstm,recurrent-neural-network,Machine Learning,Keras,Lstm,Recurrent Neural Network,以下路透社数据集包含11228条文本,对应于46个类别的新闻。文本的含义是每个单词对应一个整数。我指定我们要使用2000个单词 import tensorflow as tf import matplotlib.pyplot as plt import numpy as np %matplotlib inline num_words = 2000 (reuters_train_x, reuters_train_y), (reuters_test_x, reuters_test_y) = tf.

以下路透社数据集包含11228条文本,对应于46个类别的新闻。文本的含义是每个单词对应一个整数。我指定我们要使用2000个单词

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

num_words = 2000
(reuters_train_x, reuters_train_y), (reuters_test_x, reuters_test_y) = tf.keras.datasets.reuters.load_data(num_words=num_words)

n_labels = np.unique(reuters_train_y).shape[0]
print("labels: {}".format(n_labels))

# This is the first new
print(reuters_train_x[0])
实施LSTM 我需要实现一个带有10个单元的单个LSTM的网络。输入在进入LSTM单元之前需要嵌入10维。最后,需要添加一个密集层,以根据类别的数量调整输出的数量

from keras.models import Sequential
from keras.layers import LSTM, Dense, Embedding
from from tensorflow.keras.utils import to_categorical

reuters_train_y = to_categorical(reuters_train_y, 46)
reuters_test_y = to_categorical(reuters_test_y, 46)

model = Sequential()
model.add(Embedding(input_dim = num_words, 10))
model.add(LSTM(10))
model.add(Dense(46,activation='softmax'))
训练 我得到的错误消息是:

ValueError: Error when checking target: expected dense_2 to have shape (46,) but got array with shape (1,)

您需要对
y
标签进行一次热编码

from tensorflow.keras.utils import to_categorical

reuters_train_y = to_categorical(reuters_train_y, 46)

reuters_test_y = to_categorical(reuters_test_y, 46)

我在
fit
函数中看到的另一个错误是,您正在通过
validation\u data=(路透社测试,路透社训练)
但它应该是
validation\u data=(路透社测试,路透社测试)

你的x是一个不同长度列表的numpy数组。您需要填充序列以获得固定形状的numpy数组

reuters_train_x = tf.keras.preprocessing.sequence.pad_sequences(
    reuters_train_x, maxlen=50
)

reuters_test_x = tf.keras.preprocessing.sequence.pad_sequences(
    reuters_test_x, maxlen=50
)

我已经相应地修改了代码,但是在模型的培训过程中,我仍然会遇到一个错误。ValueError:使用序列设置数组元素。请检查更新的答案,您需要使用一些填充。
reuters_train_x = tf.keras.preprocessing.sequence.pad_sequences(
    reuters_train_x, maxlen=50
)

reuters_test_x = tf.keras.preprocessing.sequence.pad_sequences(
    reuters_test_x, maxlen=50
)