ValueError:检查目标时出错:预期密集的\u 2具有形状(1,),但获得的数组具有形状(27,)

2024-09-20 06:56:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个程序来预测一个随机名称的基础上,它已经训练的名称(字符级编码)。我的输出形状是(44,27),我的密集层设置为提供27类softmax输出。我还是有个错误

我尝试过在输出中添加一个轴 (Y_train_oh = np.expand_dims(Y_train_oh, axis=2))

def model1(vocab_len):
    model = Sequential()
    model.add(LSTM(128,input_shape=(buff_length, vocab_len)))
    model.add(Dense(units=60, activation='relu'))
    model.add(Dense(units=vocab_len, activation='softmax'))
    model.summary()
    return model

def one_hot(Y, char2idx, vocablen):
    Ty = len(Y)
    Yoh = np.zeros((Ty, vocablen))
    for idx in range(Ty):
        Yoh[idx, char2idx[Y[idx]]] = 1
    return Yoh

def trainer(X, vocab, char2idx, no_epochs=1, batch_size=10):
    model = model1(len(vocab))
    model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    for epn in range(no_epochs):
        np.random.seed(1 + epn)
        Tx = len(X)
        indices = np.random.randint(0, Tx, batch_size)
        X_train = []
        Y_train = []
        for index in indices:
            name = str(X[index])
            for chIndex in range(len(name) - 1): 
                if chIndex >= buff_length - 1:
                    X_train.append(name[chIndex - buff_length + 1: chIndex + 1])
                    Y_train.append(name[chIndex + 1])

        for i in range(len(X_train)):
            print ((X_train[i] + ' : '+ Y_train[i]) )

        X_train_oh = np.copy(one_hot_buffer(X_train, char2idx, len(vocab)))
        Y_train_oh = np.copy(one_hot(Y_train, char2idx, len(vocab)))

        print(X_train_oh.shape,':',Y_train_oh.shape)
        model.fit(x=X_train_oh, y=Y_train_oh)

    model.save('name_model.h5')

错误消息:

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


Tags: nameinaddformodellendefnp