# -*- 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. python3 虚拟环境

    一.python中的虚拟环境 1.虚拟环境:局部的,独立的python环境,完全模拟系统全局python环境的使用 二.安装 http://virtualenv.pypa.io/en/latest/u ...

  2. PL/SQL老是自动断开问题处理

    问题背景:情况是这样的,很多开发同事的plsql上班时间开着8个小时,有时候他们出去抽烟后或者中午吃完饭,回来在plsql上面执行就报错无响应,然后卡住了半天动弹不了,非得重新登录plsql才生效,我 ...

  3. [Google Guava] 7-原生类型

    原文链接 译文链接 译者:沈义扬,校对:丁一 概述 Java的原生类型就是指基本类型:byte.short.int.long.float.double.char和boolean. 在从Guava查找原 ...

  4. SQL的左连接与右链接

    1.准备工作 Oracle  外连接(OUTER JOIN)包括以下: 左外连接(左边的表不加限制) 右外连接(右边的表不加限制) 全外连接(左右两表都不加限制) 对应SQL:LEFT/RIGHT/F ...

  5. python manage.py makemigrat Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py

    更新models字段 出现的问题: $ python manage.py makemigrations None You are trying to add a non-nullable field ...

  6. Oracle 审计 部署监控 user DML操作

    1.移动audit表及索引到dbadmin表空间 alter table aud$ move tablespace DBADMIN;alter table AUDIT$ move tablespace ...

  7. setsockopt函数

    #include <sys/socket.h>     int setsockopt( int socket, int level, int option_name,            ...

  8. python 版本及pip安装相关

    python2与python3 由于历史原因,Python有两个大的版本分支,Python2和Python3,又由于一些库只支持某个版本分支,所以需要在电脑上同时安装Python2和Python3,因 ...

  9. cat/tac

    cat -n 显示出行号 -E显示出结束符$ tac cat的反向显示,cat是正序显示内容,tac是倒序显示内容

  10. Codeforces Round #572 (Div. 2)

    目录 Contest Info Solutions A. Keanu Reeves B. Number Circle C. Candies! D1. Add on a Tree D2. Add on ...