ValueError:检查目标时出错:预期密集_2的形状为(1,),但数组的形状为(27,)

时间:2019-06-02 05:30:41

标签: python keras keras-layer

我正在编写一个程序,根据它经过训练的名称(字符级编码)来预测随机名称。我的输出形状为(44,27),我的密集层设置为提供27类softmax输出。我仍然收到ValueError。

我尝试将轴添加到输出中 (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:检查目标时出错:预期density_10具有   形状(1,),但数组的形状为(27,)

0 个答案:

没有答案