ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(10,1) [英] ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (10, 1)

查看:743
本文介绍了ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(10,1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决LSTM input_shape问题. 在这里,我做了一个简单的LSTM网络,应该对其进行训练,以使输入增加一倍.

I am struggling with the LSTM input_shape thing. Here I made a simple LSTM network that should be trained, to double the Input.

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np

X = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])

data_dim = 1
timesteps = 8

model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=(timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

model.fit(X,y, batch_size=10, epochs=1000)

但是总会出现以下错误消息: ValueError:检查输入时出错:预期lstm_1_input具有3维,但数组的形状为(10,1) 我究竟做错了什么?有人可以解释一下input_shape吗. 亲切的问候.尼克拉斯

But there comes always this error message: ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (10, 1) What am I doing wrong? Can someone explain me the input_shape thing. Kind regards. Niklas

推荐答案

您的代码有很多错误.

There are a number of things that are wrong with your code.

1)您想遇到回归问题.在最后一层,softmax会将数字压缩到0到1的范围内.您需要进行线性激活.

1) You want to have a regression problem. At the last layer, softmax will squash numbers to range 0 and 1. You need a linear activation.

2)因此,损失函数应为mean_square_error.

2) Consequently, the loss function should be mean_square_error.

3)目标y的形状指示每个时间步长的输出层大小应为1,而不是10.

3) The shape of your target y dictates that the size of the output layer at each time step should be 1 and not 10.

4)LSTM层的输入和输出数组的形状应为(batch_size,time_step,dim).

4) Shape of input and output arrays for an LSTM layer should be (batch_size, time_step, dim).

5)LSTM层中定义的时间步长和输入数据的时间步长应该相同.

5) Time steps defined in LSTM layer and that of the input data should be the same.

我将这些更改合并到您的代码中

I incorporated these changes in your code:

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np

X = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])

X = X.reshape(1,10,1)
y = y.reshape(1,10,1)

data_dim = 1
timesteps = 10

model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=(timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True))
model.add(Dense(1, activation='linear'))

print(model.summary())

model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])

model.fit(X,y, batch_size=1, epochs=1000)

这篇关于ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(10,1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆