0

好的,所以我正在尝试将 keras 作为项目的一部分,但出现以下错误,我似乎无法解决: ValueError: Error when checking input: expected input_1 to have shape (205087,) but got array with shape (1,) 我的代码如下:

import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import Input, Dense, Concatenate
from tensorflow.keras.models import Model

def iterdata():
    while(True):
        for d in data: #data is already defined in my script
            val=tf.keras.utils.to_categorical(d, num_classes=vocab_size) #this is the one_hot vector that causes troubles
            yield val, 0
word = Input(shape=(vocab_size,))
encode = Dense(1, activation=None)
encoded = shared_encode(word)

model = Model(inputs=word, outputs=encoded)
model.compile(loss='mean_squared_error',
              optimizer='rmsprop',
              metrics=['accuracy'])
H = model.fit_generator(iterdata(), 
        epochs=10,
        steps_per_epoch=10) #according to the Traceback, error happens while running this line

跑步 :

for d in iterdata():
    print(d[0].shape)

正确打印(205087,) 所以我不知道该怎么处理它,我确实尝试过屈服np.asarray([val]),但仍然遇到同样的错误。关于如何解决这个问题的任何想法?

4

1 回答 1

1

我找到了解决办法。如果您遇到这种情况,您需要知道 keras 将输入作为批次接收,因此您的数组需要更深一步。在我的情况下,重塑它以(1,205807)解决问题,因为它与生成它的事实或任何其他原因无关,格式与预期不符。

例如,如果您的模型接受以下数据: [1,2,3]您需要输入[[1,2,3]]

于 2019-02-21T19:47:39.017 回答