Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 原因是什么;ValueError:检查目标时出错:预期密集_2有2个维度,但得到形状为(30,1,166)的数组“;?_Python_Tensorflow_Keras_Lstm - Fatal编程技术网

Python 原因是什么;ValueError:检查目标时出错:预期密集_2有2个维度,但得到形状为(30,1,166)的数组“;?

Python 原因是什么;ValueError:检查目标时出错:预期密集_2有2个维度,但得到形状为(30,1,166)的数组“;?,python,tensorflow,keras,lstm,Python,Tensorflow,Keras,Lstm,我尝试将用于识别mnist中数字的代码改编为文本生成任务。我得到一个值错误: ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (30, 1, 166) 如何使最后一层适合此输出形状 我把一些文本数据分成了几个句子x_-train和x_-test是用OCR软件创建的混乱句子,y_-train和y_-test是相同的句子,但需要手动校对和更

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

ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (30, 1, 166)
如何使最后一层适合此输出形状

我把一些文本数据分成了几个句子
x_-train
x_-test
是用OCR软件创建的混乱句子,
y_-train
y_-test
是相同的句子,但需要手动校对和更正。我想训练模型看到常见错误并加以纠正

我在这里和其他网站上寻找了解决这个问题的方法。对于人们来说,最常见的解决方案似乎是使用
loss='sparse\u categorical\u 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)
回溯(最近一次呼叫最后一次):
文件“/Users/adrian/PycharmProjects/WurzburgGlossParser/Rough Work.py”,第80行,在
模型拟合(x_检验[:30],y_检验[:30],历代=5,验证数据=(x_检验[30:40],y_检验[30:40]))
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py”,第952行,适合
批次大小=批次大小)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/keras/engine/training.py”,第789行,在用户数据中
异常(前缀='target')
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/keras/engine/training\u utils.py”,标准化输入数据中的第128行
“带形状”+str(数据形状))
ValueError:检查目标时出错:预期稠密_2有2个维度,但得到了形状为(30,1,166)的数组

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

y_试验=np.挤压(y_试验,轴=1)
y_列=np.挤压(y_列,轴=1)

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

y_试验=np.挤压(y_试验,轴=1)
y_列=np.挤压(y_列,轴=1)

您能发布完整的回溯错误吗?您还能够在IDE中调试代码,这将极大地帮助您。错误指向正在发送的意外数据。是否可以发布完整的回溯错误?您还能够在IDE中调试代码,这将极大地帮助您。该错误指向正在发送的意外数据。我在归一化后包含了该错误,并获得了
ValueError:检查目标时出错:预期密集_2具有形状(1),但获得了具有形状(166,)的数组
。在你调用
model.fit()
(例如
x\u train.shape
)之前,你能在你的问题中添加
x\u train
x\u test
y\u test
的形状吗?没有在我的答案中应用代码。这样我就更容易帮助你了。@Vlad如果你愿意,我可以把它添加到问题中,但它与回溯报告中的内容相同。x_序列和y_序列=(30,1166)和x_测试和y_测试=(10,1166)。这是一行166个字符的30或10个句子。这可能不是格式化它的最佳方式,所以请随时告诉我是否可以做得更好。我对ML很陌生。嵌入句子,对吗?不是类型为
str
的实际字符是的,请在这里发布
x\u列车
y\u列车
。e、 也许你应该把标签转换成一个热编码,这样我就需要看到标签和数据的形状。谢谢。在上面的一个
#使用映射编码列表中,我对句子进行编码,使每个字符都表示为1到37之间的整数,但它们不是一个热编码的。标签和训练数据都是相同的。上面的代码包括了我在拟合之前编辑句子所做的一切。我在归一化后包括了这一点,并得到了
ValueError:Error当检查目标时:预期稠密_2具有形状(1),但得到了具有形状(166)的数组。
。在你调用
model.fit()
(例如
x\u train.shape
)之前,你能在你的问题中添加
x\u train
x\u test
y\u test
的形状吗?没有在我的答案中应用代码。这样我就更容易帮助你了。@Vlad如果你愿意,我可以把它添加到问题中,但它与回溯报告中的内容相同。x_序列和y_序列=(30,1166)和x_测试和y_测试=(10,1166)。这是一行166个字符的30或10个句子。这可能不是格式化它的最佳方式,所以请随时告诉我是否可以做得更好。我对ML很陌生。嵌入句子,对吗?不是类型为
str
的实际字符是的,请在这里发布
x\u列车
y\u列车
。e、 也许你应该把标签转换成一个热编码,这样我就需要看到标签和数据的形状。谢谢。在上面的一个
#使用映射编码列表中,我对句子进行编码,使每个字符都表示为1到37之间的整数,但它们不是一个热编码的。标签和训练数据都是相同的。上面的代码包括我在修改句子之前所做的一切。