当前位置:网站首页>Parameter initialization
Parameter initialization
2022-07-29 09:05:00 【Salty salty】
Use numpy initialization
import numpy as np
import torch
from torch import nn
# Define a Sequential Model
net1=nn.Sequential(
nn.Linear(30,40),
nn.ReLU(),
nn.Linear(40,50),
nn.ReLU(),
nn.Linear(50,10)
)
w1=net1[0].weight # The parameters of the first layer
b1=net1[0].bias
print(w1)

net1[0].weight.data=torch.from_numpy(np.random.uniform(3,5,size=(40,30))) # Try to change the parameters of the first layer
print(net1[0].weight)

for layer in net1: # Yes Sequential Parameter initialization
if isinstance(layer,nn.Linear):
param_shape=layer.weight.shape
layer.weight.data = torch.from_numpy(np.random.normal(0, 0.5,
size=param_shape))
about Moudule Parameter initialization , In fact, it's very simple , If you want to initialize one of these layers , Can be directly like Sequential The same for its Tensor To redefine , The only difference is , If you want to access... In a circular way , Two properties need to be introduced ,children and modules, Here are some examples to illustrate .
class sim_net(nn.Module):
def __init__(self):
super(sim_net, self).__init__()
self.l1 = nn.Sequential(
nn.Linear(30, 40),
nn.ReLU()
)
self.l1[0].weight.data = torch.randn(40, 30) # ?????????
self.l2 = nn.Sequential(
nn.Linear(40, 50),
nn.ReLU()
)
self.l3 = nn.Sequential(
nn.Linear(50, 10),
nn.ReLU()
)
def forward(self, x):
x = self.l1(x)
x = self.l2(x)
x = self.l3(x)
return x
net2=sim_net()
for i in net2.children():
print(i)

for i in net2.modules():
print(i)
Through the example above , We can see that , children Only the first level of model definition will be accessed , Because the above model defines Sequential, So only three... Will be visited Sequential, and Modules Will access the final structure , Take the example above ,moudles Not only visited sequential, Also visited Sequential Inside , This is very convenient for us to initialize
for layer in net2.modules():
if isinstance(layer, nn.Linear):
param_shape = layer.weight.shape
layer.weight.data = torch.from_numpy(np.random.normal(0, 0.5,
size=param_shape))
边栏推荐
- Solve the problem of false Base64 character in Base64
- No duplicate data in the same field of different databases
- Arfoundation Getting Started tutorial 7-url dynamically loading image tracking Library
- Flowable UI制作流程图
- 【Unity入门计划】常用学习网址收藏
- 01 knapsack about from two-dimensional optimization to one-dimensional optimization
- Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql
- ICMP message analysis
- Simple unit testing idea
- 2022 Shandong Province safety officer C certificate work certificate question bank and answers
猜你喜欢

6.3 references
![A little knowledge [synchronized]](/img/4d/4a8beee749328b5867b59740fd7e78.png)
A little knowledge [synchronized]

C language -- 23 two-dimensional array

One click automated data analysis! Come and have a look at these treasure tool libraries

(视频+图文)机器学习入门系列-第2章 线性回归

英语高频后缀

Flowable UI制作流程图

English high frequency suffix

Mathematical modeling - Differential Equations

数学建模——聚类
随机推荐
Leetcode: interview question 08.14. Boolean operation
Design of distributed (cluster) file system
No duplicate data in the same field of different databases
A little knowledge [synchronized]
2022电工(初级)考题模拟考试平台操作
Floweable advanced
Transaction management in SQL Server
Rocky基础之编译安装apache
2022 R2 mobile pressure vessel filling test question simulation test platform operation
Opencv cvcircle function
(Video + graphic) introduction series to machine learning - Chapter 2 linear regression
Outlook tutorial, how to create an electronic signature in outlook?
(视频+图文)机器学习入门系列-第2章 线性回归
Amazfit dial Kit
Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
Sudoku (DFS)
How to choose effective keywords
Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
Flowable 基础篇2
正则表达式校验版本号