如何进行torchkeras的原理分析
发表于:2024-10-25 作者:千家信息网编辑
千家信息网最后更新 2024年10月25日,如何进行torchkeras的原理分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。torchkeras 是在pytorch上实现的
千家信息网最后更新 2024年10月25日如何进行torchkeras的原理分析
如何进行torchkeras的原理分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
torchkeras 是在pytorch上实现的仿keras的高层次Model接口。有了它,你可以像Keras那样,对pytorch构建的模型进行summary,compile,fit,evaluate , predict五连击。一切都像行云流水般自然。
听起来,torchkeras的功能非常强大。但实际上,它的实现非常简单,全部源代码不足300行。如果你想理解它实现原理的一些细节,或者修改它的功能,不要犹豫阅读和修改项目源码。
安装它仅需要运行:
pip install torchkeras
公众号后台回复关键词:torchkeras。获取项目git源代码和本文全部源码!
下面是一个使用torchkeras来训练模型的完整范例。我们设计了一个3层的神经网络来解决一个正负样本按照同心圆分布的分类问题。
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import Dataset,DataLoader,TensorDataset
from torchkeras import Model,summary #Attention this line!
一,准备数据
构造按照同心圆分布的正负样本数据。
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
#number of samples
n_positive,n_negative = 2000,2000
#positive samples
r_p = 5.0 + torch.normal(0.0,1.0,size = [n_positive,1])
theta_p = 2*np.pi*torch.rand([n_positive,1])
Xp = torch.cat([r_p*torch.cos(theta_p),r_p*torch.sin(theta_p)],axis = 1)
Yp = torch.ones_like(r_p)
#negative samples
r_n = 8.0 + torch.normal(0.0,1.0,size = [n_negative,1])
theta_n = 2*np.pi*torch.rand([n_negative,1])
Xn = torch.cat([r_n*torch.cos(theta_n),r_n*torch.sin(theta_n)],axis = 1)
Yn = torch.zeros_like(r_n)
#concat positive and negative samples
X = torch.cat([Xp,Xn],axis = 0)
Y = torch.cat([Yp,Yn],axis = 0)
#visual samples
plt.figure(figsize = (6,6))
plt.scatter(Xp[:,0],Xp[:,1],c = "r")
plt.scatter(Xn[:,0],Xn[:,1],c = "g")
plt.legend(["positive","negative"]);
# split samples into train and valid data.
ds = TensorDataset(X,Y)
ds_train,ds_valid = torch.utils.data.random_split(ds,[int(len(ds)*0.7),len(ds)-int(len(ds)*0.7)])
dl_train = DataLoader(ds_train,batch_size = 100,shuffle=True,num_workers=2)
dl_valid = DataLoader(ds_valid,batch_size = 100,num_workers=2)
二,构建模型
我们通过对torchkeras.Model进行子类化来构建模型,而不是对torch.nn.Module的子类化来构建模型。实际上 torchkeras.Model是torch.nn.Moduled的子类。
class DNNModel(Model): ### Attention here
def __init__(self):
super(DNNModel, self).__init__()
self.fc1 = nn.Linear(2,4)
self.fc2 = nn.Linear(4,8)
self.fc3 = nn.Linear(8,1)
def forward(self,x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
y = nn.Sigmoid()(self.fc3(x))
return y
model = DNNModel()
model.summary(input_shape =(2,))
三,训练模型
我们需要先用compile将损失函数,优化器以及评估指标和模型绑定。然后就可以用fit方法进行模型训练了。
关于如何进行torchkeras的原理分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
模型
问题
原理
分析
子类
训练
功能
同心圆
实际
实际上
数据
方法
更多
样本
正负
源代码
源码
项目
帮助
解答
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库sql复习题
软件开发外部延期
银行软件开发岗位裁员情况
系统功能实现数据库发展
什么是数据库安全系统
放开那三国最新服务器
汽车仪表软件开发的不同
初始化数据库
服务器能换win7吗
odbc接数据库
院网络技术基础试卷答案
数据库设置密码后怎么解除
朝阳网络安全大队长
惠普服务器 检测报告
百度网络安全中心
上海智能软件开发使用方法
软件开发三个要素
mqtt服务器网站全站
台州易之悦网络技术
东方通为什么要配置数据库
二手浪潮服务器硬盘
监狱系统网络安全管理办法
生态环境绘画软件开发
网络安全报告 法院
关于网络安全解决方案的书
分布式服务器怎么进入
运城市网络安全宣传周活动
eora数据库账户
荆州网络安全等级评测
闵行区什么是网络技术创新服务