torch.nn.Parameter()的用法

1.首先,一定记住:这个是 继承来自 torch.tensor的,所以本质是一个tensor.

        不过,它可以train更新参数

2.参考文章:

(1)这篇文章 和 例子超级 简洁清晰

理解torch.nn.Parameter - 知乎 (zhihu.com)

里面的实例:

import torch
import torch.nn as nn
from torch.optim import Adam

class NN_Network(nn.Module):
    def __init__(self, in_dim, hid, out_dim):
        super(NN_Network, self).__init__()
        self.linear1 = nn.Linear(in_dim, hid)
        self.linear2 = nn.Linear(hid, out_dim)
        self.linear1.weight = torch.nn.Parameter(torch.zeros(in_dim,hid))
        self.linear1.bias = torch.nn.Parameter(torch.ones(hid))
        self.linear2.weight = torch.nn.Parameter(torch.zeros(hid,out_dim))
        self.linear2.bias = torch.nn.Parameter(torch.ones(out_dim))
    def forward(self, input_array):
        h = self.linear1(input_array)
        y_pred = self.linear2(h)
        return y_pred

in_d = 5
hidn = 2
out_d = 3
net = NN_Network(in_d, hidn, out_d)

(2)其他参考文章:

torch.nn.Parameter()函数的讲解和使用-CSDN博客

torch.nn.Parameter()函数 - 知乎 (zhihu.com) 

相关推荐

  1. nc

    2024-03-11 06:02:03       63 阅读
  2. QueryWrapper

    2024-03-11 06:02:03       30 阅读
  3. axios

    2024-03-11 06:02:03       33 阅读
  4. React <> </>

    2024-03-11 06:02:03       32 阅读
  5. pymysql基本

    2024-03-11 06:02:03       60 阅读
  6. css_auto

    2024-03-11 06:02:03       56 阅读
  7. 关于QUOTENAME

    2024-03-11 06:02:03       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-11 06:02:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-11 06:02:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-11 06:02:03       87 阅读
  4. Python语言-面向对象

    2024-03-11 06:02:03       96 阅读

热门阅读

  1. React 第七章 Hooks

    2024-03-11 06:02:03       50 阅读
  2. 数据库基础

    2024-03-11 06:02:03       44 阅读
  3. 伊萨卡训练代码

    2024-03-11 06:02:03       40 阅读
  4. leetcode-hot100-普通数组

    2024-03-11 06:02:03       42 阅读
  5. 我的创作纪念日

    2024-03-11 06:02:03       47 阅读
  6. vue3中使用ref

    2024-03-11 06:02:03       36 阅读
  7. Revit-二开之创建几何形体-拉伸体-(9)

    2024-03-11 06:02:03       39 阅读
  8. 在css中 height: 100vh;与height: 100%;有什么区别?

    2024-03-11 06:02:03       43 阅读