Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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具有形状(1),但获得具有形状(27)的数组_Python_Keras_Keras Layer - Fatal编程技术网

Python ValueError:检查目标时出错:预期稠密_2具有形状(1),但获得具有形状(27)的数组

Python ValueError:检查目标时出错:预期稠密_2具有形状(1),但获得具有形状(27)的数组,python,keras,keras-layer,Python,Keras,Keras Layer,我正在写一个程序,根据它训练过的名字(字符级编码)预测一个随机名字。我的输出形状是(44,27),我的密集层设置为提供27类softmax输出。我仍然得到一个错误 我已尝试将轴添加到输出 (Y\u-train\u-oh=np.展开(Y\u-train\u-oh,轴=2)) 错误消息: ValueError:检查目标时出错:预期密集_10具有 形状(1),但得到了形状为(27)的数组 发现了问题。给出了稀疏的、分类的、不适用于一个热数据的损失交叉熵,找出了问题所在。给出了不适用于一个热数据的稀疏分

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

我已尝试将轴添加到输出
(Y\u-train\u-oh=np.展开(Y\u-train\u-oh,轴=2))

错误消息:

ValueError:检查目标时出错:预期密集_10具有 形状(1),但得到了形状为(27)的数组


发现了问题。给出了稀疏的、分类的、不适用于一个热数据的损失交叉熵,找出了问题所在。给出了不适用于一个热数据的稀疏分类交叉熵
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')