gpt4 book ai didi

machine-learning - Model.fit() ValueError : Error when checking model target: expected dense_21 to have shape (None, 1) 但得到了形状为 (1708, 66) 的数组

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

这是我正在处理的代码:

from __future__ import print_function
from keras.models import Sequential
from keras.layers import Dense
from sklearn.cross_validation import train_test_split
import numpy
numpy.random.seed(7)

data_pixels=np.genfromtxt("pixels_dataset.csv", delimiter=',')
classes_dataset=np.genfromtxt("labels.csv",dtype=np.str , delimiter='\t')
x_train, x_test, y_train, y_test = train_test_split(data_pixels, classes_dataset, test_size=0.3

x_train 的形状为 (1708, 3072)

array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
[ 0., 0., 0., ..., 1., 1., 1.],
[ 1., 1., 1., ..., 1., 1., 1.],
...,
[ 0., 0., 0., ..., 1., 1., 1.],
[ 1., 1., 1., ..., 1., 1., 1.],
[ 0., 0., 0., ..., 1., 1., 1.]])

y_train 的形状为 (1708,)

array(['7', 'f', '3', ..., '6', 'o', 'O'], 
dtype='|S5')

y_train的字符是

: , : ; ! è à ä Aa..Zz 0-9

model = Sequential()
model.add(Dense(12, input_dim=3072, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

执行以下命令后出现错误:

model.fit(x_train,y_train, epochs=150, batch_size=10)

错误是

ValueError: could not convert string to float: A

我尝试了以下替代方案:1)

x_train=n.array(x_train)
y_train=n.array(y_train)

2)

 model.fit(x_train,str(y_train), epochs=150, batch_size=10)

但是我遇到了同样的错误然后我尝试了另一种选择

from sklearn.preprocessing import LabelBinarizer
encoder = LabelBinarizer()
y_train = encoder.fit_transform(y_train)

然后我得到一个新错误

ValueError: Error when checking model target: expected dense_21 to have shape (None, 1) but got array with shape (1708, 66)

最佳答案

更改以下代码行:

model.add(Dense(66, activation='softmax'))

和:

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

问题在于您想要预测一个 char ,它被编码为长度为 66 的 one-hot 向量。在这种情况下 - 您正在设置输出达到所需的长度,并且您正在使用 categorical_crossentropy 损失和 softmax 激活。

关于machine-learning - Model.fit() ValueError : Error when checking model target: expected dense_21 to have shape (None, 1) 但得到了形状为 (1708, 66) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43209128/

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