- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在尝试练习如何在 Keras 和所有参数(样本、时间步长、特征)中使用 LSTM。 3D 列表让我感到困惑。
所以我有一些股票数据,如果列表中的下一个项目高于 5 的阈值(即 +-2.50),则它会买入或卖出,如果它位于所持有的阈值的中间,则这些是我的标签:我的Y。
对于我的 X 特征,我的 500 个样本的数据帧为 [500, 1, 3],每个时间步长为 1,因为每个数据都是 1 小时增量,3 个特征为 3。但我收到此错误:
ValueError: Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (500, 3)
我该如何修复此代码以及我做错了什么?
import json
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
"""
Sample of JSON file
{"time":"2017-01-02T01:56:14.000Z","usd":8.14},
{"time":"2017-01-02T02:56:14.000Z","usd":8.16},
{"time":"2017-01-02T03:56:15.000Z","usd":8.14},
{"time":"2017-01-02T04:56:16.000Z","usd":8.15}
"""
file = open("E.json", "r", encoding="utf8")
file = json.load(file)
"""
If the price jump of the next item is > or < +-2.50 the append 'Buy or 'Sell'
If its in the range of +- 2.50 then append 'Hold'
This si my classifier labels
"""
data = []
for row in range(len(file['data'])):
row2 = row + 1
if row2 == len(file['data']):
break
else:
difference = file['data'][row]['usd'] - file['data'][row2]['usd']
if difference > 2.50:
data.append((file['data'][row]['usd'], 'SELL'))
elif difference < -2.50:
data.append((file['data'][row]['usd'], 'BUY'))
else:
data.append((file['data'][row]['usd'], 'HOLD'))
"""
add the price the time step which si 1 and the features which is 3
"""
frame = pd.DataFrame(data)
features = pd.DataFrame()
# train LSTM
for x in range(500):
series = pd.Series(data=[500, 1, frame.iloc[x][0]])
features = features.append(series, ignore_index=True)
labels = frame.iloc[16000:16500][1]
# test
#yt = frame.iloc[16500:16512][0]
#xt = pd.get_dummies(frame.iloc[16500:16512][1])
# create LSTM
model = Sequential()
model.add(LSTM(3, input_shape=features.shape, activation='relu', return_sequences=False))
model.add(Dense(2, activation='relu'))
model.add(Dense(1, activation='relu'))
model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
model.fit(x=features.as_matrix(), y=labels.as_matrix())
"""
ERROR
Anaconda3\envs\Final\python.exe C:/Users/Def/PycharmProjects/Ether/Main.py
Using Theano backend.
Traceback (most recent call last):
File "C:/Users/Def/PycharmProjects/Ether/Main.py", line 62, in <module>
model.fit(x=features.as_matrix(), y=labels.as_matrix())
File "\Anaconda3\envs\Final\lib\site-packages\keras\models.py", line 845, in fit
initial_epoch=initial_epoch)
File "\Anaconda3\envs\Final\lib\site-packages\keras\engine\training.py", line 1405, in fit
batch_size=batch_size)
File "\Anaconda3\envs\Final\lib\site-packages\keras\engine\training.py", line 1295, in _standardize_user_data
exception_prefix='model input')
File "\Anaconda3\envs\Final\lib\site-packages\keras\engine\training.py", line 121, in _standardize_input_data
str(array.shape))
ValueError: Error when checking model input: expected lstm_1_input to have 3 dimensions, but got array with shape (500, 3)
"""
谢谢。
最佳答案
这是我在这里发表的第一篇文章,我希望它有用,我会尽力做到最好
首先,您需要创建 3 维数组以在 keras 中使用 input_shape 您可以在 keras 文档中或以更好的方式观看此内容:从 keras.models 导入顺序顺序的?线性堆叠层。
layers: list of layers to add to the model.
# 注意 第一层传递给顺序模型 应该有一个定义的输入形状。那是什么 意思是它应该收到一个 input_shape
或batch_input_shape参数, 或某些类型的层(循环层、密集层……) input_dim
参数。
```python
model = Sequential()
# first layer must have a defined input shape
model.add(Dense(32, input_dim=500))
# afterwards, Keras does automatic shape inference
model.add(Dense(32))
# also possible (equivalent to the above):
model = Sequential()
model.add(Dense(32, input_shape=(500,)))
model.add(Dense(32))
# also possible (equivalent to the above):
model = Sequential()
# here the batch dimension is None,
# which means any batch size will be accepted by the model.
model.add(Dense(32, batch_input_shape=(None, 500)))
model.add(Dense(32))
之后如何将 2 维数组转换为 3 维数组检查 np.newaxis
有用的命令对您的帮助超出您的预期:
最佳
关于machine-learning - Keras LSTM 输入特征和错误的维度数据输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46437058/
基本上,我的问题是,由于无监督学习是机器学习的一种,是否需要机器“学习”的某些方面并根据其发现进行改进?例如,如果开发了一种算法来获取未标记的图像并找到它们之间的关联,那么它是否需要根据这些关联来改进
生成模型和判别模型似乎可以学习条件 P(x|y) 和联合 P(x,y) 概率分布。但从根本上讲,我无法说服自己“学习概率分布”意味着什么。 最佳答案 这意味着您的模型要么充当训练样本的分布估计器,要么
是否有类似于 的 scikit-learn 方法/类元成本 在 Weka 或其他实用程序中实现的算法以执行常量敏感分析? 最佳答案 不,没有。部分分类器提供 class_weight和 sample_
是否Scikit-learn支持迁移学习?请检查以下代码。 型号 clf由 fit(X,y) 获取 jar 头型号clf2在clf的基础上学习和转移学习 fit(X2,y2) ? >>> from s
我发现使用相同数据的两种交叉验证技术之间的分类性能存在差异。我想知道是否有人可以阐明这一点。 方法一:cross_validation.train_test_split 方法 2:分层折叠。 具有相同
我正在查看 scikit-learn 文档中的这个示例:http://scikit-learn.org/0.18/auto_examples/model_selection/plot_nested_c
我想训练一个具有很多标称属性的数据集。我从一些帖子中注意到,要转换标称属性必须将它们转换为重复的二进制特征。另外据我所知,这样做在概念上会使数据集稀疏。我也知道 scikit-learn 使用稀疏矩阵
我正在尝试在 scikit-learn (sklearn.feature_selection.SelectKBest) 中通过卡方方法进行特征选择。当我尝试将其应用于多标签问题时,我收到此警告: 用户
有几种算法可以构建决策树,例如 CART(分类和回归树)、ID3(迭代二分法 3)等 scikit-learn 默认使用哪种决策树算法? 当我查看一些决策树 python 脚本时,它神奇地生成了带有
我正在尝试在 scikit-learn (sklearn.feature_selection.SelectKBest) 中通过卡方方法进行特征选择。当我尝试将其应用于多标签问题时,我收到此警告: 用户
有几种算法可以构建决策树,例如 CART(分类和回归树)、ID3(迭代二分法 3)等 scikit-learn 默认使用哪种决策树算法? 当我查看一些决策树 python 脚本时,它神奇地生成了带有
有没有办法让 scikit-learn 中的 fit 方法有一个进度条? 是否可以包含自定义的类似 Pyprind 的内容? ? 最佳答案 如果您使用 verbose=1 初始化模型调用前 fit你应
我正在使用基于 rlglue 的 python-rl q 学习框架。 我的理解是,随着情节的发展,算法会收敛到一个最优策略(这是一个映射,说明在什么状态下采取什么行动)。 问题 1:这是否意味着经过若
我正在尝试使用 grisSearchCV 在 scikit-learn 中拟合一些模型,并且我想使用“一个标准错误”规则来选择最佳模型,即从分数在 1 以内的模型子集中选择最简约的模型最好成绩的标准误
我正在尝试离散数据以进行分类。它们的值是字符串,我将它们转换为数字 0,1,2,3。 这就是数据的样子(pandas 数据框)。我已将数据帧拆分为 dataLabel 和 dataFeatures L
每当我开始拥有更多的类(1000 或更多)时,MultinominalNB 就会变得非常慢并且需要 GB 的 RAM。对于所有支持 .partial_fit()(SGDClassifier、Perce
我需要使用感知器算法来研究一些非线性可分数据集的学习率和渐近误差。 为了做到这一点,我需要了解构造函数的一些参数。我花了很多时间在谷歌上搜索它们,但我仍然不太明白它们的作用或如何使用它们。 给我带来更
我知道作为功能 ordinal data could be assigned arbitrary numbers and OneHotEncoding could be done for catego
这是一个示例,其中有逐步的过程使系统学习并对输入数据进行分类。 它对给定的 5 个数据集域进行了正确分类。此外,它还对停用词进行分类。 例如 输入:docs_new = ['上帝就是爱', '什么在哪
我有一个 scikit-learn 模型,它简化了一点,如下所示: clf1 = RandomForestClassifier() clf1.fit(data_training, non_binary
我是一名优秀的程序员,十分优秀!