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]])
相关推荐
Rock_yzh17 小时前
AI学习日记——参数的初始化
人工智能·python·深度学习·学习·机器学习
拆房老料18 小时前
Transformer推理优化全景:从模型架构到硬件底层的深度解析
深度学习·ai·自然语言处理·transformer
Learn Beyond Limits21 小时前
Mean Normalization|均值归一化
人工智能·神经网络·算法·机器学习·均值算法·ai·吴恩达
byzy21 小时前
【论文笔记】VisionPAD: A Vision-Centric Pre-training Paradigm for Autonomous Driving
论文阅读·深度学习·计算机视觉·自动驾驶
ACERT33321 小时前
5.吴恩达机器学习—神经网络的基本使用
人工智能·python·神经网络·机器学习
Aaplloo21 小时前
【无标题】
人工智能·算法·机器学习
可触的未来,发芽的智生1 天前
触摸未来2025.10.06:声之密语从生理构造到神经网络的声音智能革命
人工智能·python·神经网络·机器学习·架构
动能小子ohhh1 天前
AI智能体(Agent)大模型入门【6】--编写fasteAPI后端请求接口实现页面聊天
人工智能·python·深度学习·ai编程
我是Feri1 天前
机器学习之线性回归的特征相关性:避免“双胞胎特征“干扰模型
人工智能·机器学习
拉姆哥的小屋1 天前
深度学习图像分类实战:从零构建ResNet50多类别分类系统
人工智能·深度学习·分类