首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在进行顺序模型时,tensorflow,lstm形状应该是什么?

在进行顺序模型时,tensorflow,lstm形状应该是什么?
EN

Stack Overflow用户
提问于 2019-08-07 05:12:57
回答 1查看 35关注 0票数 0

我的数据总是出现形状错误。我想训练一个lstm来预测正弦波,所以我生成了数据

代码语言:javascript
复制
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个数据点(目前还不担心批量大小,因为它很小,但我希望最终进行批量训练)

然后试着预测它

代码语言:javascript
复制
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,但似乎都不起作用。

我是不是漏掉了什么?

EN

回答 1

Stack Overflow用户

发布于 2019-08-07 07:38:36

我将使用重写示例中的注释来向您展示错误

代码语言:javascript
复制
# 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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57384076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档