模型搭建练习1_用numpy和tensor、variable实现前后向传播、实现激活函数
用numpy实现搭建一个简单的forward和backward
import numpy as np
N, D_in, H, D_out = 64, 1000, 100, 10
x = np.random.randn(N, D_in) # (64, 1000)
y = np.random.randn(N, D_out) # (64, 10)
w1 = np.random.randn(D_in, H) # (1000, 100)
w2 = np.random.randn(H, D_out) # (100, 10)
learning_rate = 1e-6 for t in range(2):
# Forward pass: compute predicted y
h = x.dot(w1) # (64, 100)
h_relu = np.maximum(h, 0) # (64, 100) 实现relu函数功能
y_pred = h_relu.dot(w2) # (64, 10) loss = np.square(y_pred - y).sum() # sum()所有元素求和
# Backprop to compute gradients of w1 and w2 with respect to loss
grad_y_pred = 2.0 * (y_pred - y)
grad_w2 = h_relu.T.dot(grad_y_pred)
grad_h_relu = grad_y_pred.dot(w2.T)
grad_h = grad_h_relu.copy() # (64, 100)
grad_h[h < 0] = 0 # 在h中负元素对应位置处grad_h中置0 -> 实现relu函数功能
grad_w1 = x.T.dot(grad_h) # .T是转置 (1000, 100) # Update weights
w1 -= learning_rate * grad_w1 # (1000, 100)
w2 -= learning_rate * grad_w2
用tensor实现搭建一个简单的forward和backward
import torch dtype = torch.FloatTensor
# dtype = torch.cuda.FloatTensor # 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 x = torch.randn(N, D_in).type(dtype)
y = torch.randn(N, D_out).type(dtype) # Randomly initialize weights
w1 = torch.randn(D_in, H).type(dtype)
w2 = torch.randn(H, D_out).type(dtype) learning_rate = 1e-6
for t in range(500):
# Forward pass: compute predicted y
h = x.mm(w1) # 与numpy对比,dot点乘
h_relu = h.clamp(min=0)
y_pred = h_relu.mm(w2) loss = (y_pred - y).pow(2).sum()
# Backprop to compute gradients of w1 and w2 with respect to loss
grad_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] = 0
grad_w1 = x.t().mm(grad_h) # Update weights using gradient descent
w1 -= learning_rate * grad_w1
w2 -= learning_rate * grad_w2
用variable实现forward和backward
# use PyTorch Variables and autograd to implement our two-layer network;
# now we no longer need to manually implement the backward pass through the network import torch
from torch.autograd import Variable dtype = torch.FloatTensor
N, D_in, H, D_out = 64, 1000, 100, 10 # Setting requires_grad=False indicates that we do not need to compute gradients with respect to these Variables during the backward pass.
x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False) # Setting requires_grad=True indicates that we want to compute gradients with respect to these Variables during the backward pass.
w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True) learning_rate = 1e-6
for t in range(2):
# Forward pass: we do not need to keep references to intermediate values since we are not implementing the backward pass by hand
y_pred = x.mm(w1).clamp(min=0).mm(w2) # Now loss is a Variable of shape (1,) and loss.data is a Tensor of shape (1,); loss.data[0] is a scalar value holding the loss.
loss = (y_pred - y).pow(2).sum()
# print(loss) # [torch.FloatTensor of size 1]
# print(loss.size()) # torch.Size([1])
# print(loss.data) # [torch.FloatTensor of size 1]
print(loss.data[0]) loss.backward() w1.data -= learning_rate * w1.grad.data
w2.data -= learning_rate * w2.grad.data w1.grad.data.zero_()
w2.grad.data.zero_()
用variable实现relu函数
import torch
from torch.autograd import Variable class MyReLU(torch.autograd.Function):
def forward(self, input):
self.save_for_backward(input)
return input.clamp(min=0) def backward(self, grad_output):
input, = self.saved_tensors
grad_input = grad_output.clone()
grad_input[input < 0] = 0
return grad_input dtype = torch.FloatTensor
N, D_in, H, D_out = 64, 1000, 100, 10 x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False) w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True) learning_rate = 1e-6
for t in range(2):
relu = MyReLU() # Forward pass
y_pred = relu(x.mm(w1)).mm(w2) loss = (y_pred - y).pow(2).sum()
loss.backward() w1.data -= learning_rate * w1.grad.data
w2.data -= learning_rate * w2.grad.data w1.grad.data.zero_()
w2.grad.data.zero_()
模型搭建练习1_用numpy和tensor、variable实现前后向传播、实现激活函数的更多相关文章
- 入门项目数字手写体识别:使用Keras完成CNN模型搭建(重要)
摘要: 本文是通过Keras实现深度学习入门项目——数字手写体识别,整个流程介绍比较详细,适合初学者上手实践. 对于图像分类任务而言,卷积神经网络(CNN)是目前最优的网络结构,没有之一.在面部识别. ...
- 一周总结:AutoEncoder、Inception 、模型搭建及下周计划
一周总结:AutoEncoder.Inception .模型搭建及下周计划 1.AutoEncoder: AutoEncoder: 自动编码器就是一种尽可能复现输入信号的神经网络:自动编码器必须捕 ...
- [开发技巧]·TensorFlow中numpy与tensor数据相互转化
[开发技巧]·TensorFlow中numpy与tensor数据相互转化 个人主页–> https://xiaosongshine.github.io/ - 问题描述 在我们使用TensorFl ...
- 【Python秘籍】numpy到tensor的转换
在用pytorch训练神经网络时,我们常常需要在numpy的数组变量类型与pytorch中的tensor类型进行转换,今天给大家介绍一种它们之间互相转换的方法. 一.numpy到tensor 首先我们 ...
- TensorFlow中numpy与tensor数据相互转化
numpy与tensor数据相互转化: *Numpy2Tensor 虽然TensorFlow网络在输入Numpy数据时会自动转换为Tensor来处理,但是我们自己也可以去显式的转换: data_ten ...
- Darknet_Yolov3模型搭建
Darknet_Yolov3模型搭建 YOLO(You only look once)是目前流行的目标检测模型之一,目前最新已经发展到V3版本了,在业界的应用也很广泛.YOLO的特点就是"快 ...
- 【python学习小知识】求绝对值和numpy和tensor的相互转换
一.python求绝对值的三种方法 1.条件判断 2.内置函数abs() 3.内置模块 math.fabs 1.条件判段,判断大于0还是小于0,小于0则输出相反数即可 # 法1:使用条件判断求绝对值 ...
- Python 学习之中的一个:在Mac OS X下基于Sublime Text搭建开发平台包括numpy,scipy
1 前言 Python有许多IDE能够用,官方自己也带了一个,Eclipse也能够. 但我在使用各种IDE之后,发现用Sublime Text是最好用的一个.因此.我都是用Sublime Text来编 ...
- TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
报错原因:numpy不能读取CUDA tensor 需要将它转化为 CPU tensor. 所以如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tenso ...
随机推荐
- 极简配置phpstorm+xdebug进行断点调试
以前调试的时候各种var_dump()就能得到结果,现在入手别人开发的工作,由于不了解业务和代码逻辑,又要去修改bug,就造成了修改bug效率低,所以又拾起来了xdbug,顺便总结了一下phpstor ...
- MySQL时间字段究竟使用INT还是DateTime
今天解析DEDECMS时发现deder的MYSQL时间字段,都是用 `senddata` ) unsigned '; 随后又在网上找到这篇文章,看来如果时间字段有参与运算,用int更好,一来检索时不用 ...
- 『编写高质量代码Web前端开发修炼手册』读书笔记--高质量的CSS
1.怪异模式和DTD 标准模式:浏览器根据规范表现页面 怪异模式:模拟老浏览器行为防止老站点无法工作(为了兼容老式浏览器的代码),如果漏写DTD(Document Type Definition文档定 ...
- IE hasLayout详解
hasLayout定义 haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分.在Internet Explorer中,一个元素要么自己对自身的内容进行计算 ...
- day05_10 作业
bug1.0版本 #购物车程序 salary = int(input("请输入你的工资")) item = ['iphone6s','macbook','coffee','pyth ...
- c++ 吕凤翥 第五章 类对象一
一 类的声明和实现 1. class tdate //声明部分 { public: void setdate(int y,int m,int d); int isleapyear(); voi ...
- 将数据缓存到sessionStorage中
//获取侧边栏 if (sessionStorage.getItem(`${env}${empId}leftMenu`)) { const leftMenu = JSON.parse(sessionS ...
- BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)
[ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在 ...
- Cmake——CMake+SVN或Hg生成版本号
CMake+SVN或Hg生成版本号 原来的CMake需要用shell脚本生成SVN版本号,再作为cmake参数传入.CMake调用脚本示例: #!/bin/sh # cmake.sh ServerCo ...
- 《挑战程序设计竞赛》P196 铺砖问题
题意:给定n*m格子,每个格子被染成了黑色或者白色,现在要用1*2的砖块覆盖这些格子,块与块不得重叠,且覆盖所有的白色格子,但不覆盖任意一个黑色格子,求一共有多少种覆盖方法. 思路:书上给的思路太巧妙 ...