- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试学习 LSTM。已经参加了这个网络类(class),阅读了这本书(https://machinelearningmastery.com/lstms-with-python/)和很多博客......但是,我完全被困住了。我的兴趣是多元 LSTM,我已经阅读了所有我能找到的内容,但仍然无法理解。不知道是我傻还是什么...
如果这个确切的问题和一个好的答案已经存在,那么我很抱歉重复发布,但我已经看过但没有找到...
因为我想真正了解基础知识,所以我在 excel 中创建了一个虚拟数据集,其中每个“y”都取决于每个输入 x1 和 x2 的总和,但也会随着时间的推移而变化。据我了解,这是一个多对一的场景。伪代码:
x1(t) = sin(A(t))
x2(t) = cos(A(t))
tmp(t) = x1(t) + x2(t) (dummy variable)
y(t) = tmp(t) + tmp(t-1) + tmp(t-2) (i.e. sum over the last three steps)
(基本上我想预测 y(t) 给定 x1 和 x2 三个时间步长)
然后将其导出到包含 x1、x2、y 列的 csv 文件
我已经尝试在下面编写代码,但显然行不通。
我读取数据并将其分成 80/20 测试和训练集 X_train、y_train、X_test、y_test,尺寸为 (217,2)、(217,1)、(54,2)、(54/1)
我真正没有掌握的是时间步长和样本到底是什么,以及在 reshape 和 input_shape 中的使用。在我看过的许多代码示例中,它们只是使用数字而不是定义的变量,这使得理解正在发生的事情变得非常困难,尤其是当您想更改某些内容时。例如,在我参加的一门类(class)中, reshape 是这样编码的……
X_train = np.reshape(X_train, (1257, 1, 1))
这没有提供太多信息...
无论如何,当我运行下面的代码时它说
ValueError: cannot reshape array of size 434 into shape (217,3,2)
所以,我知道导致错误的原因,但不知道我需要做什么来修复它。如果我设置 look_back=1 它会起作用,但这不是我想要的。
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# Load data
data_set = pd.read_csv('../Data/LSTM_test.csv',';')
"""
data loaded have three columns:
col 0, col 1: features (x)
col 2: y
"""
# Train/test and variable split
split = 0.8 # 80% train, 20% test
split_idx = int(data_set.shape[0]*split)
# ...train
X_train = data_set.values[0:split_idx,0:2]
y_train = data_set.values[0:split_idx,2]
# ...test
X_test = data_set.values[split_idx:-1,0:2]
y_test = data_set.values[split_idx:-1,2]
# Model setup
look_back = 3 # as that is how y was generated (i.e. sum last three steps)
num_features = 2 # in this case: 2 features x1, x2
output_dim = 1 # want to predict 1 y value
nb_hidden_neurons = 32 # assume something to start with
nb_epoch = 2 # assume something to start with
# Reshaping
nb_samples = len(X_train) # in this case 217 samples in the training set
X_train_reshaped = np.reshape(X_train,(nb_samples, look_back, num_features))
# Create model
model = Sequential()
model.add(LSTM(nb_hidden_neurons, input_shape=(look_back,num_features)))
model.add(Dense(units=output_dim))
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
model.fit(X_train_reshaped, y_train, batch_size = 32, epochs = nb_epoch)
print(model.summary())
谁能解释一下我做错了什么?
正如我所说,我已经阅读了很多博客、问题、教程等,但如果有人有特别好的信息来源,我也很乐意查看。
最佳答案
我之前也有这个疑问。在更高的层次上,在 (samples, time steps, features)
samples
是数据的数量,或者说你的数据集中有多少行time step
是模型或LSTM
features
是每个样本的列数对我来说,我认为一个更好理解的例子是,在NLP
中,假设你有一个句子要处理,那么这里的样本是1,这意味着要阅读1个句子, time step
是该句子中的单词数,在模型读取所有单词并获得该句子的整个上下文之前,您逐字输入句子,features
这里是每个词的维度,因为在像 word2vec
或 glove
这样的词嵌入中,每个词都由具有多个维度的向量解释。
Keras
中的input_shape
参数只有(time_steps, num_features)
,更多可以引用this .
而你的问题是,当你reshape data时,每个维度的乘积应该等于原始数据集维度的乘积,其中434不等于217*3*2。
当你实现LSTM
时,你应该非常清楚它的特征是什么,你希望模型在每个时间步读取的元素是什么。有一个非常相似的案例here一定能帮到你。例如,如果您尝试使用 t-1
和 t-2
预测时间 t
的值,您可以选择馈送将两个值作为一个元素来预测 t
,其中 (time_step, num_features)=(1, 2)
,或者您可以在 2 个时间步长中输入每个值,其中 (time_step, num_features)=(2, 1)
。
这就是我的基本理解,希望为您解释清楚。
关于python - LSTM:了解时间步长、样本和特征,尤其是在 reshape 和 input_shape 中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45435049/
我开始在 Ethereum blockchain 上了解如何开发智能合约以及如何写 web-script用于与智能合约交互(购买、销售、统计......)我得出了该怎么做的结论。我想知道我是否正确理解
我正在 UIView 中使用 CATransform3DMakeRotation,并且我正在尝试进行 45º,变换就像向后放置一样: 这是我拥有的“代码”,但显然没有这样做。 CATransform3
我目前正在测试 WebRTC 的功能,但我有一些脑逻辑问题。 WebRTC 究竟是什么? 我只读了“STUN”、“P2P”和其他...但是在技术方面什么是正确的 WebRTC(见下一个) 我需要什么
我在看 DelayedInit在 Scala in Depth ... 注释是我对代码的理解。 下面的 trait 接受一个非严格计算的参数(由于 => ),并返回 Unit .它的行为类似于构造函数
谁能给我指出一个用图片和简单的代码片段解释 WCF 的资源。我厌倦了谷歌搜索并在所有搜索结果中找到相同的“ABC”文章。 最佳答案 WCF 是一项非常复杂的技术,在我看来,它的文档记录非常少。启动和运
我期待以下 GetArgs.hs打印出传递给它的参数。 import System.Environment main = do args main 3 4 3 :39:1: Coul
private int vbo; private int ibo; vbo = glGenBuffers(); ibo = glGenBuffers(); glBindBuffer(GL_ARRAY_
我正在尝试一个 for 循环。我添加了一个 if 语句以在循环达到 30 时停止循环。 我见过i <= 10将运行 11 次,因为循环在达到 10 次时仍会运行。 如果有设置 i 的 if 语句,为什
我正在尝试了解 WSGI 的功能并需要一些帮助。 到目前为止,我知道它是一种服务器和应用程序之间的中间件,用于将不同的应用程序框架(位于服务器端)与应用程序连接,前提是相关框架具有 WSGI 适配器。
我是 Javascript 的新手,我正在尝试绕过 while 循环。我了解它们的目的,我想我了解它们的工作原理,但我在使用它们时遇到了麻烦。 我希望 while 值自身重复,直到两个随机数相互匹配。
我刚刚偶然发现Fabric并且文档并没有真正说明它是如何工作的。 我有根据的猜测是您需要在客户端和服务器端都安装它。 Python 代码存储在客户端,并在命令运行时通过 Fabric 的有线协议(pr
我想了解 ConditionalWeakTable .和有什么区别 class ClassA { static readonly ConditionalWeakTable OtherClass
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
我还没有成功找到任何可以引导我理解 UIPickerView 和 UIPickerView 模型的好例子。有什么建议吗? 最佳答案 为什么不使用默认的 Apple 文档示例?这是来自苹果文档的名为 U
我在看foldM为了获得关于如何使用它的直觉。 foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a 在这个简单的例子中,我只返回 [Just
答案What are _mm_prefetch() locality hints?详细说明提示的含义。 我的问题是:我想要哪一个? 我正在处理一个被重复调用数十亿次的函数,其中包含一些 int 参数。
我一直在读这个article了解 gcroot 模板。我明白 gcroot provides handles into the garbage collected heap 然后 the handle
提供了一个用例: 流处理架构;事件进入 Kafka,然后由带有 MongoDB 接收器的作业进行处理。 数据库名称:myWebsite集合:用户 并且作业接收 users 集合中的 user 记录。
你好 我想更详细地了解 NFS 文件系统。我偶然发现了《NFS 图解》这本书,不幸的是它只能作为谷歌图书提供,所以有些页面丢失了。有人可能有另一个很好的资源,这将是在较低级别上了解 NFS 的良好开始
我无法理解这个问题,哪个更随机? rand() 或: rand() * rand() 我发现这是一个真正的脑筋急转弯,你能帮我吗? 编辑: 凭直觉,我知道数学答案是它们同样随机,但我忍不住认为,如果您
我是一名优秀的程序员,十分优秀!