当前位置:网站首页>深度学习与图像识别:原理与实践 笔记Day_15
深度学习与图像识别:原理与实践 笔记Day_15
2022-04-21 13:47:00 【努力卷】
Tensor
Tensor与numpy之间如何互相转换:
import numpy as np
import torch
np_data = np.arange(8).reshape((2,4)) # 定义一个二维数组
torch_data = torch.from_numpy(np_data)
print(np_data)
print(torch_data)
np2_data = torch_data.numpy()
print(np2_data)
运行结果:

Variable
Variable是对Tensor的一种封装。每个Variable包含了三个属性(data,grad,creator),Variable中的Tensor本身通过.data访问,对应Tensor的梯度通过.grad访问,以及创建这个Variable的Function的引用通过.grad_fn来访问。
若我们需要使用Variable。则可在代码中输入如下语句
from torch.autograd import Variable
来看一个简单的小例子:
import numpy as np
import torch
from torch.autograd import Variable
x_tensor = torch.randn(10,5)
# 将Tensor转换为Variable
x = Variable(x_tensor, requires_grad = True) # 默认不需要求梯度,所以通过这个方式求梯度
print(x.data)
print(x.grad)
print(x.grad_fn)
运行结果:

激活函数
实现代码如下:
import torch
from torch.autograd import Variable
import matplotlib.pyplot as plt
tensor = torch.linspace(-6,6,200)
tensor = Variable(tensor)
np_data = tensor.numpy()
# 定义激活函数
y_relu = torch.relu(tensor).data.numpy()
y_sigmoid = torch.sigmoid_(tensor).data.numpy()
y_tanh = torch.tanh(tensor).data.numpy()
# figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
'''
num:图像编号或名称,数字为编号 ,字符串为名称
figsize:指定figure的宽和高,单位为英寸;
dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80 1英寸等于2.5cm,A4纸是 21*30cm的纸张
facecolor:背景颜色
edgecolor:边框颜色
frameon:是否显示边框
'''
plt.figure(1,figsize=(8,6))
# plt.subplot(nrows, ncols, index, **kwargs)
'''
第一个参数:*args 可以使用三个整数,或者三个独立的整数来描述子图的位置信息。
如果三个整数是行数、列数和索引值,子图将分布在行列的索引位置上。索引从1开始,从右上角增加到右下角。
位置是由三个整型数值构成,第一个代表行数,第二个代表列数,第三个代表索引位置。
举个列子:plt.subplot(2, 3, 5) 和 plt.subplot(235) 是一样一样的。需要注意的是所有的数字不能超过10。
'''
plt.subplot(221) # 两行一列的图 所在位置为1
plt.plot(np_data, y_relu, c='r',label='relu')
'''
labels是图例的名称(能够覆盖在plt.plot( )中label参数值)
loc代表了图例在整个坐标轴平面中的位置
'''
plt.legend(loc='best')
plt.subplot(222) # 两行一列的图 所在位置为2
plt.plot(np_data, y_sigmoid, c='r',label='sigmoid')
plt.legend(loc='best')
plt.subplot(223) # 两行一列的图 所在位置为3
plt.plot(np_data, y_tanh, c='r',label='tanh')
plt.legend(loc='best')
plt.show()
运行结果:

版权声明
本文为[努力卷]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq786558544/article/details/120852607
边栏推荐
- How to recover if U disk data is lost? U disk data recovery, two schemes completed
- JVM内存分配机制详解
- Maze walking (BFS)
- [special topic of stack and queue] - Dual queue simulation stack
- BOM development of access (3) BOM expansion
- npm---package. json
- Programmers burst out their salary, with a monthly salary of 15000 before tax
- STM32 drives st7789v2 TFT screen
- New technology is coming again, embrace agp7 0, are you ready to say goodbye to transform?
- Scroll bar style modification
猜你喜欢
![[special topic of stack and queue] - Dual queue simulation stack](/img/5f/241804f418487e779f8bd762df5556.png)
[special topic of stack and queue] - Dual queue simulation stack

International logistics centralized transportation system source code, overseas warehousing cross-border transshipment system source code

北京大学ACM Problems 1009:Edge Detection

If the field lines are the same, they will be merged into another field SQL statement?

Tailwind core concept - responsive design

u盘数据丢失了怎么恢复?u盘数据恢复,2个方案完成

JVM内存模型深度剖析与优化

Reverse Polish notation

滚动条样式修改

暴力匹配阈值的基准细胞检测方案
随机推荐
Nmap usage
Tailwind core concept - responsive design
Open mmlab / mmpose installation and use tutorial
EsgynDB SQL统计模式下各对象个数
JDBC and database connection pool
Machine learning notes - SVD singular value decomposition (3) applying SVD to images
STM32 MCU beginner 5-iic communication drive OLED screen
json-server的使用
颜色渐变(柱子、圆环等)
npm---环境
通过区块划分提高随机生成圆球干涉检查的效率
In office word 2016, omml2mml appears when the formula edited by word's own formula editor is transferred to MathType Solutions to XSL problems
Mushroom array (moving gauge)
Beauty of ASM pile insertion
Longest common subsequence (I) (dynamic gauge)
New technology is coming again, embrace agp7 0, are you ready to say goodbye to transform?
Sqlyog import SQL file
Character sorting in the string (10 points): please write the function fun to sort the strings with a length of 8 characters in descending order.
Review questions and answers of architectural physics and equipment for class I registered architect examination in 2022
Longest ascending subsequence (2) (greedy + dichotomy)