【问题标题】:What is causing "ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (30, 1, 166)"?是什么导致“ValueError:检查目标时出错:预期的 dense_2 有 2 个维度,但得到了形状为 (30, 1, 166) 的数组”?
【发布时间】:2019-04-28 02:06:38
【问题描述】:

我尝试将用于识别 mnist 中的数字的代码改编为文本生成任务。我收到一个值错误:

ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (30, 1, 166)

如何使最后一层适合这个输出形状?

我已将一些文本数据分成句子。 x_trainx_test 是使用 OCR 软件创建的杂乱句子,y_trainy_test 是相同的句子,但需要手动校对和更正。我想训练模型以查看常见错误并纠正它们。

我已在此处和其他网站上寻找此问题的解决方案。似乎对人们有用的最常见的解决方案是使用loss='sparse_categorical_crossentropy',但我已经在使用它了。

这是我正在使用的代码:

# Import test and train sets
test_in = open("test.pkl", "rb")
test_set = pickle.load(test_in)
train_in = open("train.pkl", "rb")
train_set = pickle.load(train_in)
x_test, y_test = test_set[0], test_set[1]
x_train, y_train = train_set[0], train_set[1]

# Map all characters in both sets
chars = sorted(list(set("".join(x_train + y_train + x_test + y_test))))
chardict = dict((c, i + 1) for i, c in enumerate(chars))
rchardict = dict((i + 1, c) for i, c in enumerate(chars))

# Encode lists using mapping
temp_list = list()
for gloss in x_test:
    encoded_gloss = [chardict[char] for char in gloss]
    temp_list.append(encoded_gloss)
x_test = temp_list
temp_list = list()
for gloss in y_test:
    encoded_gloss = [chardict[char] for char in gloss]
    temp_list.append(encoded_gloss)
y_test = temp_list
temp_list = list()
for gloss in x_train:
    encoded_gloss = [chardict[char] for char in gloss]
    temp_list.append(encoded_gloss)
x_train = temp_list
temp_list = list()
for gloss in y_train:
    encoded_gloss = [chardict[char] for char in gloss]
    temp_list.append(encoded_gloss)
y_train = temp_list

# Pad all sentences
max_len = max([len(x) for x in x_train + y_train + x_test + y_test])
x_test = np.array(pad_sequences(x_test, maxlen=max_len, padding='post'))
x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))
y_test = np.array(pad_sequences(y_test, maxlen=max_len, padding='post'))
y_test = np.reshape(y_test, (y_test.shape[0], 1, y_test.shape[1]))
x_train = np.array(pad_sequences(x_train, maxlen=max_len, padding='post'))
x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
y_train = np.array(pad_sequences(y_train, maxlen=max_len, padding='post'))
y_train = np.reshape(y_train, (y_train.shape[0], 1, y_train.shape[1]))

# Normalise to improve training speed
x_test = x_test/37.0
x_train = x_train/37.0

# Define the model
model = Sequential()
model.add(LSTM(128, input_shape=(x_test.shape[1:]), activation='relu', return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
opt = Adam(lr=1e-3, decay=1e-5)

# Compile and fit the model
model.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.fit(x_test, y_test, epochs=5, validation_data=(x_train, y_train))

我希望能够训练模型,以便我可以在看不见的句子上尝试它,看看它是否过度拟合,但我无法克服这个障碍。

编辑以包含完整的回溯:

Traceback (most recent call last):
  File "/Users/adrian/PycharmProjects/WurzburgGlossParser/Rough Work.py", line 80, in <module>
    model.fit(x_test[:30], y_test[:30], epochs=5, validation_data=(x_test[30:40], y_test[30:40]))
  File"/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 952, in fit
    batch_size=batch_size)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 789, in _standardize_user_data
    exception_prefix='target')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training_utils.py", line 128, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (30, 1, 166)

【问题讨论】:

  • 你能发布完整的回溯错误吗?您还能够在 IDE 中调试代码,这将对您有很大帮助。该错误指向正在发送的意外数据。

标签: python tensorflow keras lstm


【解决方案1】:

您需要从标签中删除尺寸为 1 的尺寸:

y_test = np.squeeze(y_test, axis=1)
y_train = np.squeeze(y_train, axis=1)

【讨论】:

  • 我在标准化后包含了这个并得到了ValueError: Error when checking target: expected dense_2 to have shape (1,) but got array with shape (166,)。这段代码应该放在哪里来修复错误?
  • @AdeDoyle 您能否在您致电model.fit() 之前立即将x_trainy_trainx_testy_test 的形状添加到您的问题中(例如x_train.shape)?并且没有在我的答案中应用代码。这样我帮你就容易多了。
  • @Vlad 如果您愿意,我可以将其添加到问题中,但这与回溯报告中的内容相同。 x_train 和 y_train = (30, 1, 166) 和 x_test 和 y_test = (10, 1, 166)。那是一行 166 个字符的 30 或 10 个句子。这可能不是格式化它的最佳方式,所以请随时告诉我是否可以做得更好。我对 ML 很陌生。
  • 嵌入式句子,对吧?不是str 类型的实际字符。 + 是的,请在此处发布x_trainy_train。例如也许您应该将标签转换为一次性编码,因此我需要查看标签和数据的形状。谢谢。
  • 在上面的 # Encode lists using mapping 步骤之一中,我对句子进行编码,以便每个字符都表示为 1 到 37 之间的 int,但它们不是一次性编码的。标签和训练数据都是一样的。上面的代码包括我在拟合之前为编辑句子所做的一切。
猜你喜欢
  • 2019-03-25
  • 2018-04-13
  • 2020-09-26
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 2020-03-19
相关资源
最近更新 更多