【问题标题】:Error when checking target: expected dense_1 to have shape (1,) but got array with shape (12,)检查目标时出错:预期 dense_1 的形状为 (1,) 但得到的数组的形状为 (12,)
【发布时间】:2019-07-10 07:49:02
【问题描述】:

我正在尝试创建 LSTM 模型。我的数据形状 (23931, 7)。 我为我的模型标题['Train'] 和标题['Label'] 选择了两列。 我正在关注两个教程 这是a link, a link.

请帮助我理解为什么这不起作用。

当我运行它时,我得到以下错误:

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

X_train_pad.shape (2839, 24) t_train_pad.shape(2839, 24, 14968)

import pandas as pd
import numpy as np
import gensim,logging
from nltk import word_tokenize
import string
import re

from tensorflow.python.keras.preprocessing.text import Tokenizer
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense, Embedding, LSTM, GRU, Dropout
from keras.layers.embeddings import Embedding
from keras.models import model_from_yaml
from keras.utils import plot_model, to_categorical
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
title = pd.read_excel('../file_name.xlsx')

x_train = title.loc[:2838,'Train']
y_train = title.loc[:2838,'Label']

x_test = title.loc[2839:,'Train']
y_test = title.loc[2839:,'Label']
x_train = x_train.apply(clean_text)
x_train = x_train.apply(word_tokenize)

def clean_text(text):
    text = text.lower()
    text = text.translate(string.punctuation)
    text = text.strip()
    text = re.sub(r'[?|!|\'|"|#]',r'',text)
    text = re.sub(r'[.|,|)|(|\|/]',r' ',text)
    return text

x_train = x_train.apply(clean_text)
x_train = x_train.apply(word_tokenize)

x_test = x_test.apply(clean_text)
x_test = x_test.apply(word_tokenize)

y_train = y_train.apply(clean_text)
y_train = y_train.apply(word_tokenize)

tockenizer = Tokenizer()
max_length = max([len(s.split()) for s in title['Название АСНА']])
tockenizer.fit_on_texts(title['Название АСНА'])
vocab_size = len(tockenizer.word_index) + 1

X_train_tokens = tockenizer.texts_to_sequences(x_train)
X_test_tokens = tockenizer.texts_to_sequences(x_test)

X_train_pad = pad_sequences(X_train_tokens, maxlen = max_length, padding='post')
X_test_pad = pad_sequences(X_test_tokens, maxlen = max_length, padding='post')

y_train_tokens = tockenizer.texts_to_sequences(y_train)
y_train_pad = pad_sequences(y_train_tokens, maxlen = max_length, padding='post')
y_train_label = to_categorical(y_train_pad, num_classes=vocab_size)

model = Sequential()
model.add( Embedding(vocab_size,EMBEDDING_DIM, input_length=max_length))
model.add(LSTM(256))
model.add(Dropout(0.1))
model.add(Dense(vocab_size, activation='sigmoid'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
print('Train...')
model.fit(X_train_pad, y_train_pad, batch_size=128, epochs=100, verbose=2)

【问题讨论】:

  • 您的输出尺寸不匹配,您的目标 (y_train) 的形状为 (1,),但您的最后一层(密集)的输出形状为 (12,)。检查尺寸以确保您没有在某处犯错字或小错误。

标签: tensorflow machine-learning keras deep-learning lstm


【解决方案1】:

如果您使用 sparse_categorical_crossentropy 损失,那么您需要提供整数标签(不是 one-hot 编码的标签)。由于您已经对标签进行 one-hot 编码,因此您应该改用 categorical_crossentropy 损失:

model.compile(loss='categorical_crossentropy', optimizer='adam')

【讨论】:

    猜你喜欢
    • 2019-01-10
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2019-08-02
    • 2019-07-07
    • 2020-09-03
    相关资源
    最近更新 更多