【Python】keras使用LSTM拟合曲线
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拟合曲线的更多相关文章
- 基于 Keras 用 LSTM 网络做时间序列预测
目录 基于 Keras 用 LSTM 网络做时间序列预测 问题描述 长短记忆网络 LSTM 网络回归 LSTM 网络回归结合窗口法 基于时间步的 LSTM 网络回归 在批量训练之间保持 LSTM 的记 ...
- Python中利用LSTM模型进行时间序列预测分析
时间序列模型 时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征.这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺 ...
- 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:LSTM网络层详解及其应用
from keras.layers import LSTM model = Sequential() model.add(embedding_layer) model.add(LSTM(32)) #当 ...
- 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:使用TensorFlow和Keras开发高级自然语言处理系统——LSTM网络原理以及使用LSTM实现人机问答系统
!mkdir '/content/gdrive/My Drive/conversation' ''' 将文本句子分解成单词,并构建词库 ''' path = '/content/gdrive/My D ...
- Kesci: Keras 实现 LSTM——时间序列预测
博主之前参与的一个科研项目是用 LSTM 结合 Attention 机制依据作物生长期内气象环境因素预测作物产量.本篇博客将介绍如何用 keras 深度学习的框架搭建 LSTM 模型对时间序列做预测. ...
- 手把手教你用 Keras 实现 LSTM 预测英语单词发音
1. 动机 我近期在研究一个 NLP 项目,根据项目的要求,需要能够通过设计算法和模型处理单词的音节 (Syllables),并对那些没有在词典中出现的单词找到其在词典中对应的押韵词(注:这类单词类似 ...
- Python Keras module 'keras.backend' has no attribute 'image_data_format'
问题: 当使用Keras运行示例程序mnist_cnn时,出现如下错误: 'keras.backend' has no attribute 'image_data_format' 程序路径https: ...
- Keras实现LSTM
一.先看一个Example 1.描述,输入为一个字母,输出为这个字母的下一个顺序字母 A->B B->C C->D 2.Code import numpy from keras.mo ...
- 使用keras的LSTM进行预测----实战练习
代码 import numpy as np from keras.models import Sequential from keras.layers import Dense from keras. ...
随机推荐
- webpack通过postcss-loader添加浏览器前缀
在webpack中,我们可以很方便的使用autoprefixer来为css3属性添加不同的浏览器前缀. 首先,需要安装autoprefixer不用多说了,其次是安装postcss-loader(npm ...
- [原]Windows Azure开发之Linux虚拟机
Windows Azure是微软的云服务集合,用来提供云在线服务所需要的操作系统与基础存储与管理的平台,是微软的云计算的核心组成组件之一.其中windows azure提供的最重要的一项服务就是虚 ...
- Spring 源码分析之 bean 实例化原理
本次主要想写spring bean的实例化相关的内容.创建spring bean 实例是spring bean 生命周期的第一阶段.bean 的生命周期主要有如下几个步骤: 创建bean的实例 给实例 ...
- ABP实践(3)-ASP.NET Core 2.x版本(从创建实体到输出api)简单实现商品列表及增删改
项目基于前两篇文章. 本章创建一个简单版的商品管理后台api,用到EF Core用code fist迁移数据创建数据库. 创建Goods实体 在领域层xxx.Core项目[新建文件夹Goods;文件夹 ...
- 课程五(Sequence Models),第一 周(Recurrent Neural Networks) —— 0.Practice questions:Recurrent Neural Networks
[解释] It is appropriate when every input should be matched to an output. [解释] in a language model we ...
- 使用okHttp3 的坑!!
1.使用同步阻塞调用: 需要自己创建线程,否则会报主线程使用网络的error: 2.使用非阻塞异步调用: 1.返回的Response,response.body().string()获取json只能读 ...
- C#开发必会
1.字符串的各种属性和方法: 2.数组的各种熟悉和方法: 3.public.private.protect.internal.protect internal 4.构造函数 5.属性 6.ADO.NE ...
- Qt5——从零开始的学生管理系统
Qt教程——从零开始的学生管理系统(文件) 一.项目设计 1.需求分析 记录并处理学生成绩信息. 1)添加新的学生数据: 2)根据学号对已有的学生数据进行修改: 3)根据学号删除已存在的学生信息: 4 ...
- PHP序列化变量的4种方法
序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize ...
- JavaWeb学习(一)———JavaWeb入门
一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...