当前位置:网站首页>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))
边栏推荐
- 201803-3 CCF URL映射 满分题解
- Travel notes in 2022 (ongoing)
- 01 knapsack about from two-dimensional optimization to one-dimensional optimization
- What should I pay attention to now? Excuse me, is it safe to open a stock account by mobile phone?
- Reptile practice (10): send daily news
- BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
- Source code compilation pytorch pit
- Arfoundation Getting Started tutorial 7-url dynamically loading image tracking Library
- C language -- 23 two-dimensional array
- Amazfit dial toolbox Online
猜你喜欢

Leetcode deduction topic summary (topic No.: 53, 3, 141, interview question 022, the entry node of the link in the sword finger offer chain, 20, 19, Niuke NC1, 103, 1143, Niuke 127)

Count the list of third-party components of an open source project

LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)

C # use database to bind listview control data

英语高频后缀

Requests library simple method usage notes

2022 electrician (elementary) test question simulation test platform operation

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

Excellent package volume optimization tutorial

2022 question bank and answers of operation certificate examination for main principals of hazardous chemical business units
随机推荐
md
English high frequency suffix
Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
(Video + graphic) introduction series to machine learning - Chapter 2 linear regression
The use and Simulation of string function, character function and memory function
Analysis of zorder sampling partition process in Hudi - "deepnova developer community"
C # use database to bind listview control data
LeetCode刷题(6)
[unity entry program] C # and unity - understand classes and objects
CVPR 2022 | clonedperson: building a large-scale virtual pedestrian data set of real wear and wear from a single photo
文件上传及拓展
The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
1.2.24 fastjson deserialization templatesimpl uses chain analysis (very detailed)
网络原理笔记(五层网络)
How to choose effective keywords
201803-3 Full Score solution of CCF URL mapping
Gutcloud technology restcloud completed the pre-A round of financing of tens of millions of yuan
不同数据库相同字段查不重复数据