用Keras搭建神经网络 简单模版(五)——RNN LSTM Regressor 循环神经网络
# -*- coding: utf-8 -*-
import numpy as np
np.random.seed(1337)
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import LSTM,TimeDistributed,Dense
from keras.optimizers import Adam BATCH_START = 0
TIME_STEPS = 20
BATCH_SIZE = 50
INPUT_SIZE = 1
OUTPUT_SIZE = 1
CELL_SIZE = 20
LR = 0.006 def get_batch():
global BATCH_START,TIME_STEPS
# xs shape(50,20,)
#xs=np.arange(0,0+20*50).reshape(50,20)
xs = np.arange(BATCH_START,BATCH_START+TIME_STEPS*BATCH_SIZE).reshape((BATCH_SIZE,TIME_STEPS)) / (10*np.pi)
seq = np.sin(xs)
res = np.cos(xs)
BATCH_START += TIME_STEPS
#plt.plot(xs[0,:],res[0,:],'r',xs[0,:],seq[0,:],'b--')
#plt.show()
return [seq[:,:,np.newaxis], res[:,:,np.newaxis],xs] #get_batch()
#exit() model = Sequential() model.add(LSTM(output_dim=CELL_SIZE,
return_sequences=True, # 每一个时间点都输出一个output
batch_input_shape=(BATCH_SIZE,TIME_STEPS,INPUT_SIZE),
stateful = True,# batch和batch之间是否有联系
# 前一个batch的最后一步和后一个batch的第一步是有联系的
)) model.add(TimeDistributed(Dense(OUTPUT_SIZE))) # dense对每一个output连接,对每一个时间点都要计算 adam = Adam(LR)
model.compile(optimizer = adam,
loss = 'mse',) print('Training ------------')
for step in range(501):
# data shape = (batch_num,steps,inputs/output)
X_batch, Y_batch, xs = get_batch()
cost = model.train_on_batch(X_batch, Y_batch)
pred = model.predict(X_batch,BATCH_SIZE)
plt.plot(xs[0,:], Y_batch[0].flatten(),'r',xs[0,:],pred.flatten()[:TIME_STEPS],'b--')
plt.ylim((-1.2,1.2))
plt.draw()
plt.pause(0.5)
if step % 10 == 0:
print('train cost',cost)


用Keras搭建神经网络 简单模版(五)——RNN LSTM Regressor 循环神经网络的更多相关文章
- 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...
- 用Keras搭建神经网络 简单模版(一)——Regressor 回归
首先需要下载Keras,可以看到我用的是TensorFlow 的backend 自己构建虚拟数据,x是-1到1之间的数,y为0.5*x+2,可视化出来 # -*- coding: utf-8 -*- ...
- 用Keras搭建神经网络 简单模版(六)——Autoencoder 自编码
import numpy as np np.random.seed(1337) from keras.datasets import mnist from keras.models import Mo ...
- 用Keras搭建神经网络 简单模版(四)—— RNN Classifier 循环神经网络(手写数字图片识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) from keras.datasets import mnist fro ...
- 用Keras搭建神经网络 简单模版(二)——Classifier分类(手写数字识别)
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...
- TensorFlow系列专题(七):一文综述RNN循环神经网络
欢迎大家关注我们的网站和系列教程:http://panchuang.net/ ,学习更多的机器学习.深度学习的知识! 目录: 前言 RNN知识结构 简单循环神经网络 RNN的基本结构 RNN的运算过程 ...
- keras搭建密集连接网络/卷积网络/循环网络
输入模式与网络架构间的对应关系: 向量数据:密集连接网络(Dense层) 图像数据:二维卷积神经网络 声音数据(比如波形):一维卷积神经网络(首选)或循环神经网络 文本数据:一维卷积神经网络(首选)或 ...
- 大话循环神经网络(RNN)
在上一篇文章中,介绍了 卷积神经网络(CNN)的算法原理,CNN在图像识别中有着强大.广泛的应用,但有一些场景用CNN却无法得到有效地解决,例如: 语音识别,要按顺序处理每一帧的声音信息,有些结果 ...
- RNN循环神经网络学习——概述
循环神经网络(Recurrent Neural NetWork,RNN)是一种将节点定向连接成环的人工神经网络,其内部状态可以展示动态时序行为. 循环神经网络的主要用途是处理和预测序列数据.循环神经网 ...
随机推荐
- jade总结
随着时间的迁移,要跟官方api相匹配 jade的缺点 1.可移植性差 2.调试困难 3.性能不是非常出色(不是为性能设计,可以使用dot, http://olado.github.io/) 选择的 ...
- 2.caffe初解
http://www.cnblogs.com/nwpuxuezha/p/4302024.html 原文链接:caffe.berkeleyvision.org/tutorial/layers.html ...
- Python基本语法变量
Python的语法和其它编程语言的语法有所不同,编写Paython程序之前需要对语法有所了解,才能编写规范的Python程序.本篇介绍Python标识符的同时,也讨论了Python变量的声明和赋值.通 ...
- 201871010111-刘佳华《面向对象程序设计(java)》第十七周学习总结
201871010111-刘佳华<面向对象程序设计(java)>第十七周学习总结 实验十七 线程同步控制 实验时间 2019-12-20 第一部分:理论知识总结 16.Java 的线程调 ...
- Spring整合rabbitmq(转载)
原文地址:https://my.oschina.net/never/blog/140368 1.首先是生产者配置 <?xml version="1.0" encoding=& ...
- XML 命名规范
XML 元素必须遵循以下命名规则: 名称可以含字母.数字以及其他的字符 名称不能以数字或者标点符号开始 名称不能以字符 "xml"(或者 XML.Xml)开始 名称不能包含空格 可 ...
- [hdoj4578][多延迟标记的线段树]
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)T ...
- Mac: ld: library not found for -lgcc_s.10.4
Mac: ld: library not found for -lgcc_s.10.4 Checking for cc... ld: library not found for -lgcc_s.1 ...
- Oracle 进程 前台进程-服务进程
一.什么是服务进程(前台进程) 当用户运行一个应用进程时,系统会为用户运行的应用建立一个用户程序,该进程通过某种方式启动一个服务器进程(前台进程),用于处理连接到该实例的用户进程的请求. 二.服务进程 ...
- adb命令积累
1. 模拟事件全部是通过input命令来实现的,首先看一下input命令的使用: (原文:http://blog.csdn.net/huiguixian/article/details/1192538 ...