我的数据总是出现形状错误。我想训练一个lstm来预测正弦波,所以我生成了数据
x_size = 100
xxx = [np.sin(5*np.pi*i/x_size)+.1*np.random.rand() for i in range(x_size)]
xxx = np.array(xxx)这是一组100个样本,每个样本都是一维的。因此每个时期将有100个数据点(目前还不担心批量大小,因为它很小,但我希望最终进行批量训练)
然后试着预测它
model = tf.keras.Sequential()
model.add(layers.LSTM(128, activation='relu',))
model.add(layers.Dense(1, activation='relu'))
model.compile(loss='mean_squared_error',
optimizer='sgd',
metrics=['accuracy'])
model.fit(xxx, xxx)但我不能让它运行fit步骤。我尝试过用不同的方法重塑xxx,但似乎都不起作用。
我是不是漏掉了什么?
发布于 2019-08-07 07:38:36
我将使用重写示例中的注释来向您展示错误
# it should have a sample size N and a feature size (M, 1)
# thus x has shape = (N, M, 1)
# y as label shape = (N, 1) for the output size of your dense layer is 1
# x_size = 100
N = 100
M = 1
xxx = np.random.rand(N, M, 1)
y = np.random.randint(0, 1, size = (N, 1))
# xxx = [np.sin(5*np.pi*i/x_size)+.1*np.random.rand() for i in range(x_size)]
# xxx = np.array(xxx)
model = Sequential()
model.add(LSTM(128, activation='relu',))
model.add(Dense(1, activation='relu'))
model.compile(loss='mean_squared_error',
optimizer='sgd',
metrics=['accuracy'])
# if you choose accuracy as metric, output feature size is normally 1
model.fit(xxx, y)https://stackoverflow.com/questions/57384076
复制相似问题