当前位置:网站首页>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))
边栏推荐
- 2022 Shandong Province safety officer C certificate work certificate question bank and answers
- Sublime text create page
- Common query optimization technology of data Lake - "deepnova developer community"
- 2022电工(初级)考题模拟考试平台操作
- LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)
- Redis series 3: highly available master-slave architecture
- MySQL error summary
- How does alternates achieve high-performance publish and subscribe?
- 2022 electrician (elementary) test question simulation test platform operation
- 01背包关于从二维优化到一维
猜你喜欢

The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit

优秀的Allegro Skill推荐

Sublime text create page

Requests library simple method usage notes

Gutcloud technology restcloud completed the pre-A round of financing of tens of millions of yuan

Quick sorting (quick sorting) (implemented in C language)

四元数与其在Unity中的简单应用

What is the difference between the pre training model and the traditional method in sorting?

6.2 function-parameters
Notes on network principles (five layer network)
随机推荐
2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
How does alternates achieve high-performance publish and subscribe?
Flowable 基础篇2
LeetCode刷题(6)
文件上传及拓展
What are the backup and recovery methods of gbase 8s database
The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
Collation of ml.net related resources
[C language] DataGridView binding data
Leetcode question brushing (6)
Common query optimization technology of data Lake - "deepnova developer community"
数学建模——聚类
ICMP message analysis
2022 Teddy cup data mining challenge C project and post game summary
A little knowledge [synchronized]
多标签用户画像分析跑得快的关键在哪里?
Classic interview question: = = the difference between equals
Flowable 基础篇1
2022 P cylinder filling test simulation 100 questions simulation test platform operation
State compression DP