pytorch循环神经网络实现回归预测 代码
pytorch循环神经网络实现回归预测
学习视频:莫烦python
# RNN for classification
import torch
import numpy as np
import torch.nn as nn
import torch.utils.data as Data
import matplotlib.pyplot as plt
import torchvision #hyper parameters
TIME_STEP=10 #run time step
INPUT_SIZE=1
LR=0.02 #learning rate # t=np.linspace(0,np.pi*2,100,dtype=float) #from zero to pi*2, and one hundred point there
# x=np.sin(t)
# y=np.cos(t)
# plt.plot(t,x,'r-',label='input (sin)')
# plt.plot(t,y,'b-',label='target (cos)')
# plt.legend(loc='best')
# plt.show() class RNN_Net(nn.Module):
def __init__(self):
super(RNN_Net,self).__init__()
self.rnn=nn.RNN(
input_size=INPUT_SIZE,
hidden_size=32,
num_layers=1,
batch_first=True,
)
self.out=nn.Linear(32,1) def forward(self,x,h_state):
r_out,h_state=self.rnn(x,h_state)
outs=[]
for time_step in range(r_out.size(1)):
outs.append(self.out(r_out[:,time_step,:]))
return torch.stack(outs,dim=1),h_state # the type of return data is torch, and the return data also include h_state rnn=RNN_Net()
# print(rnn) optimizer=torch.optim.Adam(rnn.parameters(),lr=LR)
loss_func=nn.MSELoss() plt.ion()
h_state=None
for step in range (60):
start,end=step*np.pi,(step+1)*np.pi
#using sin predicts cos
steps=np.linspace(start,end,TIME_STEP,dtype=np.float32)
x_np=np.sin(steps)
y_np=np.cos(steps) x=torch.from_numpy(x_np[np.newaxis,:,np.newaxis]) # np.newaxis means increase a dim
y=torch.from_numpy(y_np[np.newaxis,:,np.newaxis])
predition,h_state=rnn(x,h_state) #the first h_state is None
h_state=h_state.data #?????
loss=loss_func(predition,y)
optimizer.zero_grad()
loss.backward()
optimizer.step() plt.plot(steps,y_np,'r-')
plt.plot(steps,predition.detach().numpy().flatten(),'b-') #flatten() 展平维度
plt.draw()
plt.pause(0.05)
plt.ioff()
plt.show()
pytorch循环神经网络实现回归预测 代码的更多相关文章
- Pytorch循环神经网络LSTM时间序列预测风速
#时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征.这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺序的,同样大 ...
- 『PyTorch』第十弹_循环神经网络
RNN基础: 『cs231n』作业3问题1选讲_通过代码理解RNN&图像标注训练 TensorFlow RNN: 『TensotFlow』基础RNN网络分类问题 『TensotFlow』基础R ...
- Recurrent Neural Networks(RNN) 循环神经网络初探
1. 针对机器学习/深度神经网络“记忆能力”的讨论 0x1:数据规律的本质是能代表此类数据的通用模式 - 数据挖掘的本质是在进行模式提取 数据的本质是存储信息的介质,而模式(pattern)是信息的一 ...
- L26 使用卷积及循环神经网络进行文本分类
文本情感分类 文本分类是自然语言处理的一个常见任务,它把一段不定长的文本序列变换为文本的类别.本节关注它的一个子问题:使用文本情感分类来分析文本作者的情绪.这个问题也叫情感分析,并有着广泛的应用. 同 ...
- 循环神经网络LSTM RNN回归:sin曲线预测
摘要:本篇文章将分享循环神经网络LSTM RNN如何实现回归预测. 本文分享自华为云社区<[Python人工智能] 十四.循环神经网络LSTM RNN回归案例之sin曲线预测 丨[百变AI秀]& ...
- CNN(卷积神经网络)、RNN(循环神经网络)、DNN(深度神经网络)的内部网络结构有什么区别?
https://www.zhihu.com/question/34681168 CNN(卷积神经网络).RNN(循环神经网络).DNN(深度神经网络)的内部网络结构有什么区别?修改 CNN(卷积神经网 ...
- 第十四章——循环神经网络(Recurrent Neural Networks)(第一部分)
由于本章过长,分为两个部分,这是第一部分. 这几年提到RNN,一般指Recurrent Neural Networks,至于翻译成循环神经网络还是递归神经网络都可以.wiki上面把Recurrent ...
- 第十四章——循环神经网络(Recurrent Neural Networks)(第二部分)
本章共两部分,这是第二部分: 第十四章--循环神经网络(Recurrent Neural Networks)(第一部分) 第十四章--循环神经网络(Recurrent Neural Networks) ...
- 深度学习项目——基于循环神经网络(RNN)的智能聊天机器人系统
基于循环神经网络(RNN)的智能聊天机器人系统 本设计研究智能聊天机器人技术,基于循环神经网络构建了一套智能聊天机器人系统,系统将由以下几个部分构成:制作问答聊天数据集.RNN神经网络搭建.seq2s ...
- 通过keras例子理解LSTM 循环神经网络(RNN)
博文的翻译和实践: Understanding Stateful LSTM Recurrent Neural Networks in Python with Keras 正文 一个强大而流行的循环神经 ...
随机推荐
- 安装 deepin linux 系统、linux 使用记录
安装 deepin 深度linux系统 官网:https://www.deepin.org/zh/ 包含了下载ios镜像和安装教程: 最新版本:https://www.deepin.org/zh/do ...
- WDA学习(27):RoadMap使用
1.20 UI Element:RoadMap使用 本实例测试创建RoadMap; 运行结果: 点击2,Input显示输入航班Id 点击3,根据input输入,查询航班信息 1.创建Component ...
- Kubernetes 1.26.0 集群部署Prometheus监控
前言 该存储库收集 Kubernetes 清单.Grafana仪表板和Prometheus 规则,结合文档和脚本,使用Prometheus Operator提供易于操作的端到端 Kubernetes ...
- Unity打包xcode修改工程配置代码
1 using System.IO; 2 using UnityEngine; 3 using UnityEditor; 4 using UnityEditor.iOS.Xcode; 5 using ...
- kunkun
<html> <head> <title>cxk</title> </head> <body><h1>给设计师:字体 ...
- js判断数组的方法
1.实例的__proto__ 属性 非标准ie浏览器不支持 let arr = [1,2,3]; console.log('__proto__',arr.__proto__ === Array.pro ...
- pandas-数据结构转换-纵转横
如下代码,亲测有效,后面会附上入口以及出口数据结构截图 def test_func(file_path): """ 把pandas数据结构-dataframe,横向的索引 ...
- ntpq 详解
Gentoo(也许其他发行版也是?)中 "ntpq -p" 的 man page 只有简短的描述:"打印出该服务器已知的节点列表和它们的状态概要信息." 我还没 ...
- Selenium私房菜系列4 -- Selenium IDE的使用【QQ】
前面说过,Selenium IDE是Firefox的一个插件,是可以进行脚本录制以及案例转换,所以Selenium IDE+Firebug会成为你日后写测试案例的两大助手(IE下可以使用Seleniu ...
- vite实现element-plus按需配置,自定义主题和读取/修改系统主题色
项目地址 vite.config.ts 插件和vite配置 import { defineConfig } from "vite"; import vue from "@ ...