"""利用numpy实现一个两层的全连接网络网络结构是:input ->(w1) fc_h -> relu ->(w2) output数据是随机出的
"""
import numpy as np
#维度和大小参数定义
batch_size = 64
input_dim = 1000
output_dim = 10
hidden_dim = 100# 数据虚拟 (x,y)
# 每行是一条数据 输入是64*1000,1000表示有1000维度的特征 输出是64*100
# 训练完参数之后,若对一条数据forward,直接运用w1 w2参数即可
# 使用relu激活函数
x = np.random.randn(batch_size,input_dim)
y = np.random.randn(batch_size,output_dim)#定义要训练的参数 w1(1000*100) w2(100*10)
# 方便起见,不设bisa
w1 = np.random.randn(input_dim,hidden_dim)
w2 = np.random.randn(hidden_dim,output_dim)# lr
lr = 1e-06
#实现
for i in range(500):#迭代500次#前向传播h = x.dot(w1) #隐藏层h_relu = np.maximum(h,0) #relu激活函数y_hat = h_relu.dot(w2)#计算损失loss = np.square(y_hat - y).sum()#计算梯度y_hat_grad = 2.0*(y_hat-y)w2_grad = h_relu.T.dot(y_hat_grad)h_relu_grad = y_hat_grad.dot(w2.T)h_grad = h_relu_grad.copy()h_grad[h < 0] = 0w1_grad = x.T.dot(h_grad)#更新参数w1 = w1 - lr*w1_gradw2 = w2 - lr*w2_grad#print("epoch "+str(i)+" end......")
#print("参数w1:")
#print(w1)
#print("参数w1:")
#print(w2)
"""使用pytorch实现上面的二层神经网络
"""
# pytorch中
## 内积
# tensor.mm(tensor)
## 转置
# tensor.t()
## 乘方运算
# tensor.pow(n)
import torch
device = torch.device('cpu')
# device = torch.device('cuda') # Uncomment this to run on GPU# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10# Create random input and output data
x = torch.randn(N, D_in, device=device)
y = torch.randn(N, D_out, device=device)# Randomly initialize weights
w1 = torch.randn(D_in, H, device=device)
w2 = torch.randn(H, D_out, device=device)learning_rate = 1e-6
for t in range(500):# Forward pass: compute predicted yh = x.mm(w1)h_relu = h.clamp(min=0)y_pred = h_relu.mm(w2)# Compute and print loss; loss is a scalar, and is stored in a PyTorch Tensor# of shape (); we can get its value as a Python number with loss.item().loss = (y_pred - y).pow(2).sum()#print(t, loss.item())# Backprop to compute gradients of w1 and w2 with respect to lossgrad_y_pred = 2.0 * (y_pred - y)grad_w2 = h_relu.t().mm(grad_y_pred)grad_h_relu = grad_y_pred.mm(w2.t())grad_h = grad_h_relu.clone()grad_h[h < 0] = 0grad_w1 = x.t().mm(grad_h)# Update weights using gradient descentw1 -= learning_rate * grad_w1w2 -= learning_rate * grad_w2
"""使用pytorch的自动求导 重新实现
"""
import torchdevice = torch.device('cpu')
# device = torch.device('cuda') # Uncomment this to run on GPU# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10# Create random Tensors to hold input and outputs
x = torch.randn(N, D_in, device=device)
y = torch.randn(N, D_out, device=device)# Create random Tensors for weights; setting requires_grad=True means that we
# want to compute gradients for these Tensors during the backward pass.
w1 = torch.randn(D_in, H, device=device, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, requires_grad=True)learning_rate = 1e-6
for t in range(500):# Forward pass: compute predicted y using operations on Tensors. Since w1 and# w2 have requires_grad=True, operations involving these Tensors will cause# PyTorch to build a computational graph, allowing automatic computation of# gradients. Since we are no longer implementing the backward pass by hand we# don't need to keep references to intermediate values.y_pred = x.mm(w1).clamp(min=0).mm(w2)# Compute and print loss. Loss is a Tensor of shape (), and loss.item()# is a Python number giving its value.loss = (y_pred - y).pow(2).sum()#print(t, loss.item())# Use autograd to compute the backward pass. This call will compute the# gradient of loss with respect to all Tensors with requires_grad=True.# After this call w1.grad and w2.grad will be Tensors holding the gradient# of the loss with respect to w1 and w2 respectively.loss.backward()# Update weights using gradient descent. For this step we just want to mutate# the values of w1 and w2 in-place; we don't want to build up a computational# graph for the update steps, so we use the torch.no_grad() context manager# to prevent PyTorch from building a computational graph for the updateswith torch.no_grad():w1 -= learning_rate * w1.gradw2 -= learning_rate * w2.grad# Manually zero the gradients after running the backward passw1.grad.zero_()w2.grad.zero_()
# 自己定义网络的一层实现# 定义自己Relu类,继承自Function函数
# 必须同时实现forward和backward
# 前向传播时,forward用的自己定义的这个
# 反向传播时,必然会再找自己实现的这个backward,如不写,则no implment errorclass MyReLU(torch.autograd.Function):"""We can implement our own custom autograd Functions by subclassingtorch.autograd.Function and implementing the forward and backward passeswhich operate on Tensors."""@staticmethoddef forward(ctx, x):"""In the forward pass we receive a context object and a Tensor containing theinput; we must return a Tensor containing the output, and we can use thecontext object to cache objects for use in the backward pass."""ctx.save_for_backward(x)return x.clamp(min=0)@staticmethoddef backward(ctx, grad_output):"""In the backward pass we receive the context object and a Tensor containingthe gradient of the loss with respect to the output produced during theforward pass. We can retrieve cached data from the context object, and mustcompute and return the gradient of the loss with respect to the input to theforward function."""x, = ctx.saved_tensorsgrad_x = grad_output.clone()grad_x[x < 0] = 0return grad_xdevice = torch.device('cpu')
# device = torch.device('cuda') # Uncomment this to run on GPU# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10# Create random Tensors to hold input and output
x = torch.randn(N, D_in, device=device)
y = torch.randn(N, D_out, device=device)# Create random Tensors for weights.
w1 = torch.randn(D_in, H, device=device, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, requires_grad=True)learning_rate = 1e-6
for t in range(500):# Forward pass: compute predicted y using operations on Tensors; we call our# custom ReLU implementation using the MyReLU.apply functiony_pred = MyReLU.apply(x.mm(w1)).mm(w2)# Compute and print lossloss = (y_pred - y).pow(2).sum()#print(t, loss.item())# Use autograd to compute the backward pass.loss.backward()with torch.no_grad():# Update weights using gradient descentw1 -= learning_rate * w1.gradw2 -= learning_rate * w2.grad# Manually zero the gradients after running the backward passw1.grad.zero_()w2.grad.zero_()

转载于:https://www.cnblogs.com/duye/p/9879648.html

【Code】numpy、pytorch实现全连接神经网络相关推荐

  1. 第六节:Pytorch实现全连接神经网络

    第六节:Pytorch实现全连接神经网络 前面的五节中,我们讲解了使用PyTorch搭建一个神经网络中需要的需要各种技巧,包括:网络的搭建.选择不同的实践技巧(优化器选择.学习率下降等等)以及可视化训 ...

  2. 【Python学习笔记】b站@同济子豪兄 用pytorch搭建全连接神经网络,对Fashion-MNIST数据集中的时尚物品进行分类

    [Python学习笔记]原作b站@同济子豪兄 用pytorch搭建全连接神经网络,对Fashion-MNIST数据集中的时尚物品进行分类 跟着b站@同济子豪兄的视频自学写的代码,内容是用pytorch ...

  3. Pytorch:全连接神经网络-MLP回归

    Pytorch: 全连接神经网络-解决 Boston 房价回归问题 Copyright: Jingmin Wei, Pattern Recognition and Intelligent System ...

  4. Pytorch 实现全连接神经网络/卷积神经网络训练MNIST数据集,并将训练好的模型在制作自己的手写图片数据集上测试

    使用教程 代码下载地址:点我下载 模型在训练过程中会自动显示训练进度,如果您的pytorch是CPU版本的,代码会自动选择CPU训练,如果有cuda,则会选择GPU训练. 项目目录说明: CNN文件夹 ...

  5. 【神经网络与深度学习】 Numpy 实现全连接神经网络

    1.实验名称 Numpy 实现全连接神经网络实验指南 2.实验要求 用 python 的 numpy 模块实现全连接神经网络. 网络结构为一个输入层.一个隐藏层.一个输出层. 隐藏层的激活函数为 Re ...

  6. 图像识别python cnn_MINIST深度学习识别:python全连接神经网络和pytorch LeNet CNN网络训练实现及比较(一)...

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 全连接神经网络是深度学习的基础,理解它就可以掌握深度学习的核心概念:前向传播.反向误差传递.权重.学习 ...

  7. 基于PyTorch框架的多层全连接神经网络实现MNIST手写数字分类

    多层全连接神经网络实现MNIST手写数字分类 1 简单的三层全连接神经网络 2 添加激活函数 3 添加批标准化 4 训练网络 5 结论 参考资料 先用PyTorch实现最简单的三层全连接神经网络,然后 ...

  8. 深度之眼Pytorch打卡(十三):Pytorch全连接神经网络部件——线性层、非线性激活层与Dropout层(即全连接层、常用激活函数与失活 )

    前言   无论是做分类还是做回归,都主要包括数据.模型.损失函数和优化器四个部分.数据部分在上一篇笔记中已经基本完结,从这篇笔记开始,将学习深度学习模型.全连接网络MLP是最简单.最好理解的神经网络, ...

  9. Pytorch深度学习入门与实战一--全连接神经网络

    全连接神经网络在分类和回归问题中都非常有效,本节对全连接神经网及神经元结构进行介绍,并使用Pytorch针对分类和回归问题分别介绍全连接神经网络的搭建,训练及可视化相关方面的程序实现. 1.全连接神经 ...

  10. PyTorch基础入门五:PyTorch搭建多层全连接神经网络实现MNIST手写数字识别分类

    )全连接神经网络(FC) 全连接神经网络是一种最基本的神经网络结构,英文为Full Connection,所以一般简称FC. FC的准则很简单:神经网络中除输入层之外的每个节点都和上一层的所有节点有连 ...

最新文章

  1. C++ 11 创建和使用 shared_ptr
  2. 发布至今18年,为什么SQLite一定要用C语言来开发?
  3. Groovy 设计模式 -- 保镖模式
  4. 【Android开发】毛玻璃效果
  5. 腾讯、网易回应被约谈:严格落实未成年人防沉迷规定
  6. 依赖倒置原则_设计模式原则之依赖倒置原则
  7. ros发布者退出后重新发布, 订阅者无法收到消息的解决办法
  8. win10 64位 安装TensorFlow
  9. Python学习笔记(正则表达式)
  10. 工大瑞普Dynamips如何保存交换机路由器配置
  11. Deeping Learning学习与感悟——《深度学习工程师》_5
  12. 短视频剪辑的九大技巧分享
  13. 排序之low逼三人组及其python代码实现
  14. 轻量化网络(二)MobileNetV2: Inverted Residuals and Linear Bottlenecks
  15. 自己动手写三层代码生成器学习总结
  16. 视频编解码 — H264结构
  17. fastreport 上一行_市、县人大常委会领导一行对公司环保工作进行督查
  18. 网络并发知识第一天网络通信基本原理
  19. 数据库连接工厂错误原因分析
  20. 转转闲鱼源码搭建教程

热门文章

  1. 怎么在国内创建谷歌账号_如何在Google文档中创建模板
  2. python怎么创建表格_python中如何创建新表格
  3. Elasticsearch系列:番外篇-Fielddata
  4. python isupper_Python isupper()函数判断字符串字符是否都为大写形式
  5. STM8 捕获模式HS0038 红外遥控解码
  6. SI24R1切换收发模式问题调试与解决
  7. Python学习笔记—— python基础 1. 变量的输出
  8. win10关闭触摸板和键盘
  9. 软件测试的目的和原则
  10. the little Redis book