pytorch

作者: 雨宝_f737 | 来源:发表于2018-11-28 16:49 被阅读0次

    1.numpy and torch 

    numpy中是array torch中是tensor

    torch.from_numpy(numpy.array)

    torch.numpy()获得numpy

    np.sin() == torch.sin()

    对于array对象,*和np.multiply函数代表的是数量积,如果希望使用矩阵的乘法规则,则应该调用np.dot和np.matmul函数。

    对于matrix对象,*直接代表了原生的矩阵乘法,而如果特殊情况下需要使用数量积,则应该使用np.multiply函数。

    np.matmul == torch.mm

    array.dot()!=torch.dot()

    2.Variable

    torch.autograd中的Variable

    tensor放到Variable中加入计算的图中,获得Variable中的tensor要用variable.data()

    Pytorch的Variable相当于一个Wraper,如果你想将数据传送到Pytorch构建的图中,就需要先将数据用Variable进行包装,包装后的Variable有三个attribute:data,creater,grad,其中data就是我们被包裹起来的数据,creator是用来记录通过何种计算得到当前的variable,grad则是在进行反向传播的时候用来记录数据的梯度的。

    3.activation

    torch.nn.functional中有softplus

    torch中有relu/tanh/sigmoid

    4.regression

    unsqueeze(input, dim)在指定的维度增加一维

    5.classification

    cat((x0, x1),0)把另一个矩阵放在下面按行,1为放在右边按列

    torch.max()0为返回每一列中最大值的那个值,返回值有两个,一个是最大值一个是最大值的索引

    使用crossentropy的话y的值必须是一维的不然会出现错误multi-target not supported at/

    6.快速搭建神经网络

    使用torch.nn.Sequential(放进去网络的各层即可)

    7.save and load

    保存整个网络的时候使用torch.save和torch.load

    当只需要保存网络的参数的时候只需要torch.save(net.state_dict()) net.load_state_dict(torch.load())

    plt画图的时候subplot多个小图之后在最后一个小图后面加上plt.show()

    8.batch

    首先需要将X, Y放入到Data.TensorDataset中去

    然后需要创建batch下载器,即Data.DataLoader(dataset=, batch_size=, shuffle=True, num_workers=)

    for (batch_x, batch_y) in loader就可以获取batch_x以及batch_y。

    9.optimizer

    如果loader中的num_workers不等于0,那么这个程序要在main函数下运行,不然会出现错误。因为原因:多进程需要在main函数中运行,解决方法1:加main函数,在main中调用 解决方法2:

    num_workers改为0,单进程加载

    10.CNN

    使用torchvision.datasets.MNIST数据集获取train_data和test_data,获取的数据是tensor类型,以前DataLoader数据集是使用Data.TensorDataset(x, y)创建现在直接使用torchvision获取的数据集。

    使用x.view()来改变tensor大小,实现flatten。

    figure是指一个图,axes是指图上的一个画图区域,一个图上可以有多个画图区域。

    使用TSNE降维度,使用plot_with_label画图画全连接层前面那层的神经元状态。

    numpy中ndarray的属性很多都是不是函数,type(a)数组类型,a.dtype数组元素类型,a.size数组的尺寸数组元素综述,a.shape数组形状,a.ndim数组的维度。

    11.RNN

    使用LSTM数据需要变成batch_size*time_step*input_size

    cnn中使用的是batch_size*height*length*width

    numpy中包含的newaxis即np.newaxis可以给原数组增加一个维度

    np.newaxis放的位置不同,产生的新数组也不同

    12.nan

    tensor中只要有一个是nan,那么计算结果肯定有nan,有时不是学习率其他的问题。

    相关文章

      网友评论

          本文标题:pytorch

          本文链接:https://www.haomeiwen.com/subject/npkzqqtx.html