当前位置: 首页 > news >正文

p2p网站建设要多少钱国外免费建站网站

p2p网站建设要多少钱,国外免费建站网站,江苏核酸检测机构,怎么免费自己做网站简介: 主要介绍一下 sin(x): 为 数据 cos(x): 为对应的label 项目包括两个文件 main.py: 模型的训练,验证,参数保存 lstm.py 模型的构建 目录: lstm.py main.py 一 lstm.py # -*- coding: utf-8 -*- "&q…

简介:

     主要介绍一下  

      sin(x):  为 数据

      cos(x):   为对应的label

      项目包括两个文件

      main.py:

              模型的训练,验证,参数保存

     lstm.py

              模型的构建

目录:

  1.      lstm.py
  2.      main.py

一 lstm.py

   

# -*- coding: utf-8 -*-
"""
Created on Tue Aug  8 14:01:15 2023@author: chengxf2
"""import torch
import torch.nn as nnclass LSTM(nn.Module):def __init__(self, input_dim, hidden_dim, num_lay, b_first):super(LSTM,self).__init__()self.lstm = nn.LSTM(input_size = input_dim, hidden_size = hidden_dim, num_layers = num_lay, batch_first=b_first)self.linear = nn.Linear(hidden_dim, 1)def forward(self, X):#X.shape:[batch_size=1, seq_num=256, input_size=1]output, (hidden, cell) = self.lstm(X)outs =[]seq_num = output.size(1)#output:[batch_size, seq_num, hidden_dim=64]#hidden.shape:[num_layer, batch_size, hiden_size]#print("\n output.shape",output.shape)#print("\n hidden.shape",hidden.shape)for time_step in range(seq_num):#h.shape[batch, hidden_dim]h = output[:,time_step,:]#print("\n h",h.shape)out = self.linear(h)outs.append(out)#沿着一个新维度对输入张量序列进行连接。 #[batch, seq_num, 1]pred = torch.stack(outs, dim=1)return pred

二  main.py

import numpy as np
from matplotlib import pyplot as plt
import torch
from lstm import LSTM
import torch.nn as nn
from torch.nn import functional as F
from torch import optim
import timedef showDiff(pred, label, steps):plt.figure()plt.rcParams['font.family'] = 'SimHei' # 正常显示中文plt.title('预测值 and 真实值', fontsize='18')plt.plot(steps, pred.cpu().data.numpy().flatten(),color='r',label='预测值')plt.plot(steps, label.cpu().data.numpy().flatten(), color='g',label='真实值')plt.legend(loc='best')plt.show()def get_data(epoch):TIME_STEP = 256start, end = epoch*np.pi, epoch*np.pi+2*np.pisteps = np.linspace(start,end,TIME_STEP,dtype=np.float32)sin_x = np.sin(steps)cos_x = np.cos(steps)sinx_torch = torch.from_numpy(sin_x[np.newaxis, :, np.newaxis])if torch.cuda.is_available():sinx_torch = torch.from_numpy(sin_x[np.newaxis,:,np.newaxis]).cuda()# print('sinx_input.shape:',sinx_input.shape)cosx_lable = torch.from_numpy(cos_x[np.newaxis, :, np.newaxis]).cuda()else:sinx_torch = torch.from_numpy(sin_x[np.newaxis, :, np.newaxis])# print('sinx_input.shape:',sinx_input.shape)cosx_lable = torch.from_numpy(cos_x[np.newaxis, :, np.newaxis])# [batch,seq_num,input_size] (1,256,1)return sinx_torch,cosx_lable,stepsdef eval(model):#等同于 self.train(False) 就是评估模式。#在评估模式下,batchNorm层,dropout层等用于优化训练而添加的网络层会被关闭,从而使得评估时不会发生偏移model.eval()test_data,test_label,steps = get_data(2)with torch.no_grad():y_pred = model(test_data)showDiff(y_pred, test_label, steps)def train(model,maxIter,criterion):'''训练模型----------model : lstm 模型.maxIter : 迭代次数.criterion : 损失函数------'''#作用是启用 batch normalization 和 dropoutmodel.train()time_stamp = time.time()for epoch in range(maxIter):sinx_torch,cosx_lable,steps = get_data(epoch)y_pre = model(sinx_torch)   loss = criterion(y_pre,cosx_lable)optimzer.zero_grad()loss.backward()optimzer.step()if epoch%100==0:data_time_interval = time.time() - time_stampprint('epoch: %d loss: %7.3f interval: %6.2f'%(epoch, loss.detach().numpy(),data_time_interval))#torch.save(model.state_dict(), 'model_params.pth') showDiff(y_pre, cosx_lable,steps)if __name__ == '__main__':input_dim =1hidden_dim = 64num_layers =2batch_first = TruemaxIter = 3000model = LSTM(input_dim, hidden_dim, num_layers, batch_first)DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")optimzer = optim.Adam(model.parameters(),lr=0.0001,weight_decay=0.00001)criterion = nn.MSELoss()model.to(DEVICE)criterion.to(DEVICE)train(model,maxIter,criterion)#model.load_state_dict(torch.load('model_params.pth',map_location='cpu'))#eval(model)

 

参考:

pytorch利用rnn通过sin预测cos 利用lstm预测手写数字_pytorch lstm cos_薛定谔的智能的博客-CSDN博客

http://www.hengruixuexiao.com/news/10916.html

相关文章:

  • 网站加载慢图片做延时加载有用在线crm管理系统
  • 做地方分类信息网站需要什么资质吗网站页面分析
  • 凉山州建设局网站免费发布推广信息的b2b
  • 苏州网站排名百度指数的使用
  • 网络维护合同网站seo策划方案案例分析
  • 企业建站服务热线什么是搜索引擎优化seo
  • 学做花蛤的网站正在直播足球比赛
  • 企业网站建设应用研究论文今天国内新闻10条
  • 为公司做网站搜索图片识别
  • 长子网站建设东莞网站自动化推广
  • 临沂做商城网站建设网站开发公司
  • 网站建设策划框架哪里可以接广告
  • 多种手机网站建设百度指数平台官网
  • 集团 投入巨资 做网站百度卖货平台
  • 可以自己做装修效果图的网站想做游戏推广怎么找游戏公司
  • 数码网站建设维护优化问题
  • 全国不动产登记查询系统seo项目经理
  • asp.net网站开发实战青岛优化网站关键词
  • 整站优化加盟长春网站建设模板
  • 网站keyword如何排序核酸检测最新消息
  • vip视频解析网站怎么做线上营销活动方案
  • 如何做网站的订阅360优化大师安卓下载
  • 国外空间网站seo工作
  • 美图秀秀可以做网站吗seo关键词优化的技巧和方法
  • 泰安专业的网站制作seo经典案例分析
  • 绛帐做企业网站怎么开展网络营销推广
  • 做房产应看的网站seosem顾问
  • 江宁营销型网站建设真正永久免费网站建设
  • 网站建设需要岗位重庆seo网站收录优化
  • 电子工程王力卓seo网站自动发布外链工具