pytorch函数

来源:互联网 发布:linux命令行拷贝文件 编辑:程序博客网 时间:2024/06/04 00:35

笔记:

1.标签一般默认为LongTensor

2.pytorch只能处理2维的数据。

字典:

找到一份很好的pytorch中文文档:http://pytorch-cn.readthedocs.io/zh/latest/

1.nn.CrossEntropyLoss()
EXP:loss_func = torch.nn.CrossEntropyLoss()
交叉熵:通常用在分类问题上
目标标签为: y = [0, 0, 0, 1]
实际结果为: out = net(x) = [1,2,3,4]
将输出转发为概率:out = F.softmax(out)=[0.1, 0.2, 0.3, 0.4]
计算二者的误差: loss = loss_func(out, y)

2.nn.Sequential()
EXP:
net = torch.nn.Sequential(
torch.nn.Linear(2,10)
torch.nn.ReLu()
torch.nn.Linear(10,2)
)

一种简单的神经网络构建方式,直接按顺序垒神经层。

3.optim.SGD()
EXP:optimizer = torch.optim,SGD(net.parameters(),lr=0.1)

优化方法采用:随机梯度下降法(Stochastic Gradient Descent,简称SGD)

4.optim.step()
EXP:
loss.backward()
optimizer.step()
一般用在反向传播后,将参数更新。

5..save()
EXP:torch.save(net,”net.pkl”) #保存整个神经网络

EXP:torch.save(net.state_dict(),”net_parameters.pkl”) #保存整个神经网路的parameters
defsave(obj,f,pickle_module=pickle,pickle_protocol=DEFAULT_PROTOCOL):
f:保存到的文件

6..load()
EXP:torch.load(“net.pkl”) #导入神经网络

7..load_state_dict()
EXP:net2 =
net = torch.nn.Sequential(
torch.nn.Linear(2,10)
torch.nn.ReLu()
torch.nn.Linear(10,2)
)
net.load_state_dict(torch.load(net_parameters.pkl))

需要先构建一个结构相同的神经网络,再导入参数,一般比直接导入整个神经网络更快。

8..linspace(start,end,steps=100,out=None)
steps指生成的个数,输出的一维张量应该是递增的

9..logspace(start,end,steps=100,out=None)
生成 10start 到 10end之间的数。

10.torch.unsqueeze()
EXP:X = torch.unsqueeze(torch.linsapce(-1,1.100),dim=1)
torch只能处理二维的数据,用来将数据转为2维的。

11.torchvision.transforms
用于预处理数据
torchvision.transforms.ToTensor()
将PIL Image 或 numpy.ndarry转换位Tensor,范围从[0,255]映射到[0.0,1]

torchvision.transforms.Normalize(mean,std)
EXP:torchvision.transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))
mean 每个channel的均值
std 每个channel到标准差
正则化处理:input[channel] = (input[channel] - mean[channel]) / std[channel]

torchvision.transforms.Compose()
EXP:

transforms.Compose([
transforms.CenterCrop(10),
transforms.ToTensor(),
])
将多个transforms连接在一起

12.torch.index_select
def index_select(input, dim, index, out=None)
切片。官方example:
Example::

        >>> x = torch.randn(3, 4)        >>> x         1.2045  2.4084  0.4001  1.1372         0.5596  1.5677  0.6219 -0.7954         1.3635 -1.2313 -0.5414 -1.8478        [torch.FloatTensor of size 3x4]        >>> indices = torch.LongTensor([0, 2])        >>> torch.index_select(x, 0, indices)         1.2045  2.4084  0.4001  1.1372         1.3635 -1.2313 -0.5414 -1.8478        [torch.FloatTensor of size 2x4]        >>> torch.index_select(x, 1, indices)         1.2045  0.4001         0.5596  0.6219         1.3635 -0.5414        [torch.FloatTensor of size 3x2]

这里indices指要切片的索引的下标,例如[0,2]指待会儿切片为坐标为0和坐标为2的片。而dim指切片的维度,dim=0,指切片为横轴(结合indices,切片为横坐标为0和2的两片),dim=-1或1,指切片为纵轴(切片为纵坐标为0和2的两片)。

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 蚌埠房地产交易网 蚌埠周边旅游景点 蚌埠有什么好玩的地方 蚌埠招聘信息 蚌埠长途汽车站 安徽蚌埠旅游 蚌埠论坛手机版 蚌埠人才人事网 蚌埠周边自驾游 蚌埠房产网二手房 蚌埠哪里好玩 蚌埠人才市场招聘信息 蚌埠人才市场 六安到蚌埠的汽车 蚌埠室内装修 蚌埠会计培训班多少钱 蚌埠中央空调 蚌埠男性医院哪家好 固镇到蚌埠的汽车 蚌埠比较好的医院 蚌埠医学院成人教育学院 蚌埠到安庆的汽车 蚌埠成人用品批发 合肥到蚌埠的火车 中国 安徽 蚌埠 蚌埠妇科哪家医院好 蚌埠哪家婚纱照拍的好 蚌埠哪家美容院好 蚌埠货运公司 蚌埠 淮南 汽车 蚌埠2手车交易市场 蚌埠的于小敏 蚌壳 蚌壳图片 蚌壳的图片 蚌壳读音 巨型蚌壳 蚌壳 珍珠 蚌壳的拼音 梦见蚌壳 蚌壳姑娘花甲粉