keras生成的网络结构如下图:

代码如下:

from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense, Activation
from keras.utils.vis_utils import plot_model
import matplotlib.pyplot as plt
import numpy as np seq = 10
x = np.arange(0, 6 * np.pi, 0.01)
y = np.sin(x) + np.cos(x) * x fig = plt.figure(1)
plt.plot(y, 'r') train = np.array(y).astype(float)
scaler = MinMaxScaler()
train = scaler.fit_transform(train)
data = []
for i in range(len(train) - seq - 1):
data.append(train[i: i + seq + 1])
data = np.array(data).astype('float64') x = data[:, :-1]
y = data[:, -1]
split = int(data.shape[0] * 0.5) train_x = x[: split]
train_y = y[: split] test_x = x # [split:]
test_y = y # [split:] train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
test_x = np.reshape(test_x, (test_x.shape[0], test_x.shape[1], 1)) model = Sequential()
model.add(LSTM(input_dim=1, output_dim=6, return_sequences=True))
model.add(LSTM(100, return_sequences=False))
model.add(Dense(output_dim=1))
model.add(Activation('linear'))
model.summary() model.compile(loss='mse', optimizer='rmsprop') model.fit(train_x, train_y, batch_size=50, nb_epoch=100, validation_split=0.1)
predict_y = model.predict(test_x)
predict_y = np.reshape(predict_y, (predict_y.size,)) predict_y = scaler.inverse_transform([[i] for i in predict_y])
test_y = scaler.inverse_transform(test_y)
fig2 = plt.figure(2)
plt.plot(predict_y, 'g')
plt.plot(test_y, 'r')
plt.show()
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=False)

拟合结果:

【Python】keras使用LSTM拟合曲线的更多相关文章

  1. 基于 Keras 用 LSTM 网络做时间序列预测

    目录 基于 Keras 用 LSTM 网络做时间序列预测 问题描述 长短记忆网络 LSTM 网络回归 LSTM 网络回归结合窗口法 基于时间步的 LSTM 网络回归 在批量训练之间保持 LSTM 的记 ...

  2. Python中利用LSTM模型进行时间序列预测分析

    时间序列模型 时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征.这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺 ...

  3. 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:LSTM网络层详解及其应用

    from keras.layers import LSTM model = Sequential() model.add(embedding_layer) model.add(LSTM(32)) #当 ...

  4. 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:使用TensorFlow和Keras开发高级自然语言处理系统——LSTM网络原理以及使用LSTM实现人机问答系统

    !mkdir '/content/gdrive/My Drive/conversation' ''' 将文本句子分解成单词,并构建词库 ''' path = '/content/gdrive/My D ...

  5. Kesci: Keras 实现 LSTM——时间序列预测

    博主之前参与的一个科研项目是用 LSTM 结合 Attention 机制依据作物生长期内气象环境因素预测作物产量.本篇博客将介绍如何用 keras 深度学习的框架搭建 LSTM 模型对时间序列做预测. ...

  6. 手把手教你用 Keras 实现 LSTM 预测英语单词发音

    1. 动机 我近期在研究一个 NLP 项目,根据项目的要求,需要能够通过设计算法和模型处理单词的音节 (Syllables),并对那些没有在词典中出现的单词找到其在词典中对应的押韵词(注:这类单词类似 ...

  7. Python Keras module 'keras.backend' has no attribute 'image_data_format'

    问题: 当使用Keras运行示例程序mnist_cnn时,出现如下错误: 'keras.backend' has no attribute 'image_data_format' 程序路径https: ...

  8. Keras实现LSTM

    一.先看一个Example 1.描述,输入为一个字母,输出为这个字母的下一个顺序字母 A->B B->C C->D 2.Code import numpy from keras.mo ...

  9. 使用keras的LSTM进行预测----实战练习

    代码 import numpy as np from keras.models import Sequential from keras.layers import Dense from keras. ...

随机推荐

  1. BOM 浏览器对象模型

    1.window对象模型:(操作浏览器) 它既是ECMAScript规定的global对象,又是javascript访问浏览器窗口的一个接口 系统对话框:这些对话框有操作系统/浏览器设置决定,css不 ...

  2. 物体检测,Error: maximum box coordinate value is too large

    使用ssd目标检测,出现error:maximum box coordinate value is larger than 1.100000: ] [1.325] 主要原因在于,用labelImg 标 ...

  3. [,,].length等于几

    分别测试了谷歌.欧朋,火狐,QQ.搜狗,Edge,ie5.7.8.9.10.11 其中ie5,ie7,ie8得到的结果为3 其他均为2:如果最后一个逗号后面为空,则不识别最后一位

  4. jq通过对象获取其ID值,再简单ajax传到后台改值

    <tbody> <tr> <#if scopes?exists> <#list scopes as scopes> <td id='${(scop ...

  5. PHP之高性能I/O框架:Libevent(三)

    Swoole Swoole里也提供了一些直接操作底层epoll/kqueue事件循环的接口,可将其他扩展创建的socket.PHP代码中stream/socket扩展创建的socket等加入到Swoo ...

  6. Java中锁分类

    锁的分类大致如下:公平锁/非公平锁可重入锁/不可重入锁独享锁/共享锁乐观锁/悲观锁分段锁 1.公平锁/非公平锁公平锁就是严格按照线程启动的顺序来执行的,不允许其他线程插队执行的:而非公平锁是允许插队的 ...

  7. visual studio code (vsc)中查看 php 数组的全部元素

    在 vsc 调试 php 时,如果数组元素过多,只能查看前面 30个左右的元素,如果需要看更多的元素,可以配置 xDebugSettings 修改项目中的 .vscode/launch.json 文件 ...

  8. Python sqlalchemy的基本使用

    示例代码 from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base fr ...

  9. k8s升级,HA集群1.12.0~HA集群1.13.2

    k8s升级,此次升级是1.12.0 至1.13.2 准备 # 首先升级master节点的基础组件kubeadm.kubelet.kubectl apt policy kubeadm 找到相应的版本,如 ...

  10. .15-浅析webpack源码之WebpackOptionsApply模块-plugin事件流总览

    总体过了一下后面的流程,发现Compiler模块确实不适合单独讲解,这里继续讲解后面的代码: compiler.options = new WebpackOptionsApply().process( ...