Keras LSTM - 'Error when checking model target: expected no data, but got:' 数据

Keras LSTM - 'Error when checking model target: expected no data, but got:' data

我在做简单的序列预测。我的模型和数据如下所示:

def generate_rnn(input_shape):
    In = Input(shape=(input_shape[1], 1))
    x = LSTM(4)(In)
    x = Flatten()(x) # I tried both with and without flatten, same results
    Out = Dense(1)(x)
    
    model = Model([In, Out])
    model.compile(optimizer=Adam(), loss='MSE', metrics=['mse'])
    
    return model

X = np.random.rand(100, 5)
y = np.random.rand(100, 1)
X = X.reshape(X.shape[0], X.shape[1], 1)

rnn = generate_rnn(X.shape)
rnn.fit(X, y, epochs=10)

我第一次 运行 程序,在函数调用 rnn.fit() 时,我收到以下错误消息:

File "C:\Users\achib\Anaconda3\envs\deep\lib\site-packages\tensorflow_core\python\keras\backend.py", line 1237, in dtype return x.dtype.base_dtype.name

AttributeError: 'NoneType' object has no attribute 'dtype'

如果我在控制台中再次 运行 rnn.fit(),我会收到以下错误消息:

ValueError: ('Error when checking model target: expected no data, but got:',

然后打印我的 y 变量。我以前在 Keras 中使用过 LSTM 网络,但这是我第一次遇到这样的问题吗?有帮助吗?

这很可能是因为您的变量名称是 InOut。如果您使用 ipython,例如在 PyCharm、Jupyter Notebooks 或 Spyder 中,这些是非正式的“保留”关键字。

当您 运行 一些代码并查看时:

Out[2]: array([1, 2, 3, 4])

Out 是过去输出的字典:

Out
{2: array([1, 2, 3, 4])}

In 也是如此,它是过去输入的字典。因此,当您将变量分配给 InOut___ 时,它可能会以奇怪的方式运行,因为 ipython 可能会更改这些变量。

tl;dr 将变量重命名为 InOut 以外的名称,然后重试

模型的文档 class ->https://keras.io/api/models/model/#model-class

据此,模型 class 必须将输入和输出作为不同的参数接收,而不是作为同一列表的一部分。这就是-

ValueError: ('Error when checking model target: expected no data, but got:',

意思是。模型的目标变量 (y) 为空。所以去掉方括号应该会有帮助。

希望对您有所帮助。

愿原力与你同在