gpt4 book ai didi

python - ValueError : Error when checking target: expected dense_6 to have shape (46, )但得到形状为(1,)的数组

转载 作者:行者123 更新时间:2023-11-30 09:17:12 25 4
gpt4 key购买 nike

这是印地语 OCR 的代码,图像有 1024 个维度,程序尝试分类为 46 个类别。到目前为止,我正在使用带有 Tensorflow 后端的 Keras,没有任何隐藏层。这是代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder



dataset = pd.read_csv('data.csv')
print(dataset.head())
x = dataset.iloc[:,:-1]
y = dataset.iloc[:,-1]

print(y[0:5])
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(y.values)
y = y.T
print(x.shape)
x_train,x_test,y_train,y_test = train_test_split(x.values,y)
print(y.shape)
print(np.unique(y))

from keras import Sequential
from keras.layers import Dense, Dropout
model = Sequential()
model.add(Dense(1024, activation='relu', input_dim=1024))
model.add(Dense(46, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])

model.fit(x_train, y_train,
epochs=20,
batch_size=128)
score = model.evaluate(x_test, y_test)

这是错误回溯:

--------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) <ipython-input-10-52e9b7f77a5d> in <module>()
2 model.fit(x_train, y_train,
3 epochs=20,
----> 4 batch_size=128)
5 score = model.evaluate(x_test, y_test)

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
948 sample_weight=sample_weight,
949 class_weight=class_weight,
--> 950 batch_size=batch_size)
951 # Prepare validation data.
952 do_validation = False

~\Anaconda3\lib\site-packages\keras\engine\training.py in
_standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
785 feed_output_shapes,
786 check_batch_axis=False, # Don't enforce the batch size.
--> 787 exception_prefix='target')
788
789 # Generate sample-wise weight values given the `sample_weight` and

~\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
135 ': expected ' + names[i] + ' to have shape ' +
136 str(shape) + ' but got array with shape ' +
--> 137 str(data_shape))
138 return data
139

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

最佳答案

您需要使用keras.utils.to_categorical()对标签进行one-hot编码(即y)或者,您可以使用'sparse_categorical_crossentropy'作为损失函数,使其适用于稀疏标签。

关于python - ValueError : Error when checking target: expected dense_6 to have shape (46, )但得到形状为(1,)的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52577355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com