美文网首页
Pytorch初学者实例教程

Pytorch初学者实例教程

作者: FireMoonStar | 来源:发表于2019-10-31 17:54 被阅读0次

@[Daniel](Pytorch初学者实例教程)

# Pytorch初学者实例教程

大家好,这里将通过一个具体的实战项目,帮助大家迅速了解如何使用Pytorch来构建自己的神经网络架构。本项目的数据取自"深度学习原理与Pytorch实战"第三章,代码也参考其书中的代码,但是做了大量的解释和少量的变动。同时封装了**方法类**,训练好了**模型**。不是有很多硬件资源的同学们可以**直接调用模型来预测**。

## 数据集的简介

数据集使用的是国外一家共享单车开源数据集,为了避免不必要的麻烦,我把[数据集](https://github.com/TangxzNLP/dispatch_pytorch_demo/tree/master/dispatch_pytorch_demo)放在了我的github仓库中,欢迎大家点击和star。更多关于数据集的简介放在了[dispatch_self_function.py](https://github.com/TangxzNLP/dispatch_pytorch_demo/blob/master/dispatch_pytorch_demo/dispatch_self_function.py)文件中。

## 主要代码简介

1. [dispatch_self_function.py](https://github.com/TangxzNLP/dispatch_pytorch_demo/blob/master/dispatch_pytorch_demo/dispatch_self_function.py)是训练模型的代码,github上富有非常详细的说明,这里只放一部分源码。

```python

a, 手写用Tensor运算的神经网路

    已知的数据:X.shape[]=(16875, 56), Y.shape = (16875, 1)

    构建网络的参数:

                input_size = X.shape[1],也就是一组特征向量的纬度,这里是56.

                hidden_size = 10, 设置10隐含节点

                output_size = 1, 1个输出节点

                batch_size = 128, 设置批处理大小为 128组向量

                则(三层神经网络:输入,隐含,输出):

                    weights.shape = (56, 10)

                    biases.shape = (,10)

                    weights2.shape = (10, 1)

"""

input_size = X.shape[1]

hidden_size = 10

output_size = 1

batch_size = 128

weights = torch.randn([input_size, hidden_size], dtype = torch.double, requires_grad = True)

biases = torch.randn([hidden_size], dtype = torch.double, requires_grad = True)

weights2 = torch.randn([hidden_size, output_size], dtype = torch.double, requires_grad = True)

def neu(x):

    hidden = x.mm(weights) + biases.expand(x.size()[0], hidden_size)

    hidden = torch.sigmoid(hidden)

    output = hidden.mm(weights2)

    return output

def cost(x, y):

    error = torch.mean((x-y)**2)

    return error

def optimizer_step(learning_rate):

    weights.data.add_(-learning_rate * weights.grad.data)

    biases.data.add_(-learning_rate * biases.grad.data)

    weights2.data.add_(-learning_rate * weights2.grad.data)

def zero_grad():

    if weights.grad is not None and biases.grad is not None and weights2.grad is not None:

        weights.grad.data.zero_()

        biases.grad.data.zero_()

        weights2.grad.data.zero_()

# 神经网络的训练

losses = []

```

2. 模型评估[dispatch_evaluate.py](https://github.com/TangxzNLP/dispatch_pytorch_demo/blob/master/dispatch_pytorch_demo/dispatch_evaluate.py)

```python

# 用训练好的神经网络在测试集上进行预测

targets = test_targets['cnt']

targets = targets.values.reshape([len(targets), 1])

targets = targets.astype('float')

x = torch.tensor(test_features.values, dtype = torch.double, requires_grad = True)

y = torch.tensor(targets, dtype = torch.double, requires_grad = True)

# 用神经网络进行预测

predict = neu(x)

predict = predict.data.numpy()

print(predict)

```

3. 模型[model.pkl](https://github.com/TangxzNLP/dispatch_pytorch_demo/blob/master/dispatch_pytorch_demo/model.pkl)

4. 代码方法类[model_function.py](https://github.com/TangxzNLP/dispatch_pytorch_demo/blob/master/dispatch_pytorch_demo/model_function.py)

## 代码网址

[https://github.com/TangxzNLP/dispatch_pytorch_demo/](https://github.com/TangxzNLP/dispatch_pytorch_demo/)帮助到您,可以帮我点个star.欢迎留言交流讨论。

相关文章

网友评论

      本文标题:Pytorch初学者实例教程

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