torch.nn.**和torch.nn.functional.**的区别

torch.nn.**

torch.nn.**是一个继承了torch.nn.Module的类,使用前必须先构造对象,然后再调用。如果直接使用则会报错

例如

bash 复制代码
a = torch.randn(3,4)
print(a)
sigmoid = torch.nn.Sigmoid()
a = sigmoid(a)
print(a)
a = torch.nn.Sigmoid(a)
bash 复制代码
tensor([[ 0.2462, -2.1680, -1.4064, -0.0268],
        [-0.4800, -0.4670,  1.7318,  0.3498],
        [ 0.0137, -2.1080, -0.0825, -0.1350]])
tensor([[0.5612, 0.1027, 0.1968, 0.4933],
        [0.3823, 0.3853, 0.8496, 0.5866],
        [0.5034, 0.1083, 0.4794, 0.4663]])
        Traceback (most recent call last):
        
Traceback (most recent call last):
  File "C:\文件\Llama\tmp.py", line 8, in <module>
    a = torch.nn.Sigmoid(a)
        ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\90929\AppData\Local\conda\conda\envs\lce\Lib\site-packages\torch\nn\modules\module.py", line 485, in __init__
    raise TypeError(
TypeError: Sigmoid.__init__() takes 1 positional argument but 2 were given

torch.nn.functional.**

torch.nn.functional.**是一个纯数学函数,可以直接使用

bash 复制代码
a = torch.randn(3,4)
print(a)
a = torch.nn.functional.sigmoid(a)
print(a)
bash 复制代码
tensor([[-0.1516,  0.5398,  0.3226, -0.4956],
        [-0.2250,  0.6393,  0.4432,  0.4215],
        [-0.5741,  0.0689,  0.3078, -1.5994]])
tensor([[0.4622, 0.6318, 0.5799, 0.3786],
        [0.4440, 0.6546, 0.6090, 0.6039],
        [0.3603, 0.5172, 0.5763, 0.1681]])
相关推荐
DatGuy8 分钟前
Week 26: 深度学习补遗:LSTM 原理与代码复现
人工智能·深度学习·lstm
周杰伦_Jay1 小时前
【 2025年必藏】8个开箱即用的优质开源智能体(Agent)项目
人工智能·机器学习·架构·开源
youngfengying3 小时前
Swin Transformer
人工智能·深度学习·transformer
CNRio5 小时前
人工智能基础架构与算力之3 Transformer 架构深度解析:从注意力机制到算力适配演进
人工智能·深度学习·transformer
qy-ll5 小时前
深度学习——CNN入门
人工智能·深度学习·cnn
青瓷程序设计9 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
F_D_Z9 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
yLDeveloper10 小时前
一只菜鸟学机器学习的日记:入门分布偏移
机器学习·dive into deep learning
阿龙AI日记10 小时前
详解Transformer04:Decoder的结构
人工智能·深度学习·自然语言处理
xier_ran15 小时前
深度学习:生成对抗网络(GAN)详解
人工智能·深度学习·机器学习·gan