torch.Linear(input_size,output_size, bia = True/Flase)

1.  引入 

import torch

import torch.nn as nn

2. 一个线性层 

mod= nn.Linear(5,3,bias = True)   # input 是5 output 是3

3. 大小为5的随机tensor

x = torch.rand(5)

print (x.size()) 

4. feed x into module

y = mod(x)

5. 查看weight 矩阵  和 bias

print ( mod.weight)  print(mod.bias)

print (mod.weight.size()) mod.bias.size()

6. 修改weight矩阵

mod.weight [0,0] = 1

mod.weight[1,1] = 2

mod.bias[1] = 2

7. mod2 = nn.Linear(3,4,bias = Fasle)

print ( mod2.weight)

print (mod2.bias)

##########################################

import torch
import torch.nn as nn

mod1 = nn.Linear(3,4,bias = True)
mod2 = nn.Linear(3,5,bias = False)

x2 = torch.Tensor([1,1,1])

 

mod1.weight[0,1] = 1

x = torch.rand(3)

y = mod1(x)

print(mod1.weight, mod1.bias)

 

转载于:https://www.cnblogs.com/shaxiaoshaxiao/p/9867844.html

你可能感兴趣的:(人工智能,python)