Pytorch 03

本文讨论了如何通过引入非线性激活函数,如ReLU,提升机器学习模型的复杂性和逼近能力。通过比较不同结构的神经网络(如添加中间矩阵),文章展示了如何利用非线性使矩阵尺寸增大从而增强模型性能,最终实现更精确的预测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前的模型在两个矩阵上,无论多少矩阵,相乘起来都是线性的模型。

How can we use this simplicity of linear algebra but have advanced models?

The Crux of Machine Learning: This lies in so-called activation functions, which add ever-so-slight non-linearities to a sequence of matrix transformations.

激活函数

  • 激活函数的用途?
    如果不用激励函数(其实相当于激励函数是f(x) = x),在这种情况下你每一层节点的输入都是上层输出的线性函数,很容易验证,无论你神经网络有多少层,输出都是输入的线性组合,与没有隐藏层效果相当,这种情况就是最原始的感知机(Perceptron)了,那么网络的逼近能力就相当有限。正因为上面的原因,我们决定引入非线性函数作为激励函数,这样深层神经网络表达能力就更加强大(不再是输入的线性组合,而是几乎可以逼近任意函数)。
  • 有哪些激活函数,都有什么性质和特点?
    早期研究神经网络主要采用sigmoid函数或者tanh函数,输出有界,很容易充当下一层的输入。近些年Relu函数及其改进型(如Leaky-ReLU、P-ReLU、R-ReLU等)在多层神经网络中应用比较多。

How much better does our model do with this simple adjustment?

class MyNeuralNet2(nn.Module):
    def __init__(self):
        super().__init__()
        self.Matrix1 = nn.Linear(2,8,bias=False)
        self.Matrix2 = nn.Linear(8,1,bias=False)
        self.R = nn.ReLU()"changed"
    def forward(self,x):
        x = self.R(self.Matrix1(x))"changed"
        x = self.Matrix2(x)
        return x.squeeze()

在这里插入图片描述

Slightly better. But the real advantage of this slight non-linearity is that we can make our matrices much larger. Lets make our matrices size 80x2 and 1x80. This only works because of our non-linearity function R(x):

class MyNeuralNet3(nn.Module):
    def __init__(self):
        super().__init__()
        self.Matrix1 = nn.Linear(2,80, bias=False)
        self.Matrix2 = nn.Linear(80,1, bias=False)
        self.R = nn.ReLU()
    def forward(self,x):
        x = self.R(self.Matrix1(x))
        x = self.Matrix2(x)
        return x.squeeze()
x = torch.tensor([[6,2],[5,2],[1,3],[7,6]]).float()
y = torch.tensor([1,5,2,5]).float()
f3 = MyNeuralNet3()

# Train model
f3, losses3 = train_model(x,y,f3, n_epochs=5000)

Closer, but still not exact. ADD BIAS.

在这里插入图片描述

class MyNeuralNet4(nn.Module):
    def __init__(self):
        super().__init__()
        self.Matrix1 = nn.Linear(2,80)"default bias=true"
        self.Matrix2 = nn.Linear(80,1)"default bias=true"
        self.R = nn.ReLU()
    def forward(self,x):
        x = self.R(self.Matrix1(x))
        x = self.Matrix2(x)
        return x.squeeze()

Better, but its still not getting us that close to y, however. What if we add another matrix in the middle?

在这里插入图片描述

class MyNeuralNet5(nn.Module):
    def __init__(self):
        super().__init__()
        self.Matrix1 = nn.Linear(2,80)
        self.Matrix2 = nn.Linear(80,80)"here!"
        self.Matrix3 = nn.Linear(80,1)
        self.R = nn.ReLU()
    def forward(self,x):
        x = self.R(self.Matrix1(x))
        x = self.R(self.Matrix2(x))"and here!"
        x = self.Matrix3(x)
        return x.squeeze()

x = torch.tensor([[6,2],[5,2],[1,3],[7,6]]).float()
y = torch.tensor([1,5,2,5]).float()
f5 = MyNeuralNet5()

# Train model
f5, losses5 = train_model(x,y,f5, n_epochs=5000)

tensor([1., 5., 2., 5.])
tensor([0.9995, 4.9961, 1.9994, 4.9981], grad_fn=)

Its predicting y almost exactly

The “Sequential” Neural Network

在这里插入图片描述

要安装pytorch0.3.0,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Anaconda,因为你要在Anaconda环境下进行安装。如果没有安装Anaconda,请根据你的操作系统下载并安装Anaconda。 2. 打开Anaconda Prompt,创建一个新的虚拟环境。你可以使用以下命令: ``` conda create -n pytorch03 python=3.6 ``` 3. 激活创建的虚拟环境: ``` conda activate pytorch03 ``` 4. 然后,使用以下命令安装pytorch 0.3.0: ``` conda install pytorch=0.3.0 torchvision cudatoolkit=11.6 -c pytorch ``` 这将安装pytorch 0.3.0、对应的torchvision和cuda toolkit 11.6。 5. 安装完成后,你可以在Python脚本中导入pytorch进行使用。 请注意,这里的安装步骤是基于Windows 64位版本和Python 3.6的情况下的示例。如果你的操作系统或Python版本不同,你可能需要根据实际情况进行相应的调整。此外,确保你的计算机安装了对应版本的CUDA和cuDNN,以便与pytorch 0.3.0兼容。 引用提供了一个关于在Anaconda下安装pytorch的参考,你也可以阅读该文档获取更多详细信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [适合从0开始的pytorch安装,比较详细](https://blog.youkuaiyun.com/antonalia_/article/details/129334030)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [pytorch 0.3.0 windows版](https://download.youkuaiyun.com/download/golemz/10216809)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值