# -*- 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 循环神经网络的更多相关文章

  1. 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)

    # -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...

  2. 用Keras搭建神经网络 简单模版(一)——Regressor 回归

    首先需要下载Keras,可以看到我用的是TensorFlow 的backend 自己构建虚拟数据,x是-1到1之间的数,y为0.5*x+2,可视化出来 # -*- coding: utf-8 -*- ...

  3. 用Keras搭建神经网络 简单模版(六)——Autoencoder 自编码

    import numpy as np np.random.seed(1337) from keras.datasets import mnist from keras.models import Mo ...

  4. 用Keras搭建神经网络 简单模版(四)—— RNN Classifier 循环神经网络(手写数字图片识别)

    # -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) from keras.datasets import mnist fro ...

  5. 用Keras搭建神经网络 简单模版(二)——Classifier分类(手写数字识别)

    # -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...

  6. TensorFlow系列专题(七):一文综述RNN循环神经网络

    欢迎大家关注我们的网站和系列教程:http://panchuang.net/ ,学习更多的机器学习.深度学习的知识! 目录: 前言 RNN知识结构 简单循环神经网络 RNN的基本结构 RNN的运算过程 ...

  7. keras搭建密集连接网络/卷积网络/循环网络

    输入模式与网络架构间的对应关系: 向量数据:密集连接网络(Dense层) 图像数据:二维卷积神经网络 声音数据(比如波形):一维卷积神经网络(首选)或循环神经网络 文本数据:一维卷积神经网络(首选)或 ...

  8. 大话循环神经网络(RNN)

      在上一篇文章中,介绍了 卷积神经网络(CNN)的算法原理,CNN在图像识别中有着强大.广泛的应用,但有一些场景用CNN却无法得到有效地解决,例如: 语音识别,要按顺序处理每一帧的声音信息,有些结果 ...

  9. RNN循环神经网络学习——概述

    循环神经网络(Recurrent Neural NetWork,RNN)是一种将节点定向连接成环的人工神经网络,其内部状态可以展示动态时序行为. 循环神经网络的主要用途是处理和预测序列数据.循环神经网 ...

随机推荐

  1. ubuntu---解决pip安装tf很慢的问题

    ubuntu---解决pip安装tf很慢的问题 [问题] 执行 u@u160406:~$ sudo pip3 install tensorflow-gpu==1.15.0rc2 下载速度真的很慢 Lo ...

  2. 透过字节码生成审视Java动态代理运作机制

    对于动态代理我想应该大家都不陌生,就是可以动态去代理实现某个接口的类来干一些我们自己想要的功能,但是在字节码层面它的表现是如何的呢?既然目前刚好在研究字节码相关的东东,有必要对其从字节码角度来审视一下 ...

  3. 好吧,关于nginx

    今天在看关于nginx的文章,自己也动手试了一下. 我是在本地测试的,在IIS上面绑了两个网站,端口号不同.当我把IIS配置好以后,修改了nginx的conf文件. nginx配置: nginx配置好 ...

  4. ZooKeeper介绍(转载)

    关于ZooKeeper,下面引用了一片文章,原文地址是:http://www.cnblogs.com/wuxl360/p/5817471.html 一.分布式协调技术 在给大家介绍ZooKeeper之 ...

  5. WCF之.NET Remoting通讯

    一.NET Remoting 介绍 简介 .NET Remoting与MSMQ不同,它不支持离线可得,另外只适合.NET平台的程序进行通信.它提供了一种允许对象通过应用程序域与另一个对象进行交互的框架 ...

  6. dpkg: error processing package XXX (--configure) 解决方法 (ubuntu右上角红色警告)

    在 Ubuntu 执行 sudo apt-get upgrade 时,出现了如下的报错: Setting up bluez (4.101-0ubuntu13.1) ... reload: Job is ...

  7. P4160 [SCOI2009]生日快乐 搜索

    思路:无脑搜索 提交:1次 题解: 大力搜索,枚举每个状态\((x,y,l)\)(\(x\)指分配到的长(可能比\(y\)要短),\(y\)指分配到的宽(可能比\(x\)要长),\(l\)指剩余切的次 ...

  8. PhpStorm 使用 Stylus 回车自动缩进的问题

    如图所示,取消勾选即可换行自动缩进,不用再一个个打空格了! 

  9. Security Study

    1.WebGoat http://www.owasp.org.cn/ 学习Web应用漏洞最好的教程----WebGoat http://blog.csdn.net/bill_lee_sh_cn/art ...

  10. 【概率论】3-7:多变量分布(Multivariate Distributions Part I)

    title: [概率论]3-7:多变量分布(Multivariate Distributions Part I) categories: Mathematic Probability keywords ...