PyTorch-Linear Model
- 构造线性模型
code:
import torch
import numpy as np
import torchvision #torch的视觉包
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import PIL.Image as Image
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
import torch.nn as nn
import torch.nn.functional as F
class Network(nn.Module):
def __init__(self): #对类进行初始化
super().__init__() #调用父类的初始化函数
self.layer=None #对层次进行声明
def forward(self,t):
t.self.layer(t) #输入是t输出也是t
return t
network=Network()
print(network)
class Model(nn.Module):
def __init__(self): #初始化
super(Model, self).__init__() #调用父类
self.conv1 = nn.Conv2d(1, 20, 5) #卷积层
self.conv2 = nn.Conv2d(20, 20, 5) #卷积层
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
network=Model()
print(network)
d=torch.ones(1,1,10,10)
print(d)
out=network(d)
print(out.size())
class Model1(nn.Module):
def __init__(self): #初始化
super(Model1, self).__init__() #调用父类
self.fc = nn.Linear(28*28, 10) #线性层
def forward(self, x):
x = self.fc(x)
return x
model2=Model1()
print(model2)
model2.fc.in_features
model2.fc.weight
model2.fc.weight.shape
class Model2(nn.Module):
def __init__(self): #初始化
super(Model2, self).__init__() #调用父类
self.fc = nn.Linear(4, 3) #线性层 输入4 输出3
def forward(self, x):
x = self.fc(x)
return x
model3=Model2()
print(model3)
in_features=torch.tensor([1,2,3,4],dtype=torch.float32)
out=model3(in_features) #这个时候权值是随机分配的
print(out)
in_features=torch.tensor([1,2,3,4],dtype=torch.float32)
weight_matrix=torch.tensor([
[1,2,3,4],
[2,3,4,5],
[3,4,5,6]
],dtype=torch.float32)
model3.fc.weight=nn.Parameter(weight_matrix)
out=model3(in_features) #这个时候权值是随机分配的
print(out)
print(model3)
print(model3.fc.bias)
class Model3(nn.Module):
def __init__(self): #初始化
super(Model3, self).__init__() #调用父类
self.fc = nn.Linear(4, 3,bias=False) #线性层 输入4 输出3
def forward(self, x):
x = self.fc(x)
return x
model4=Model3()
print(model4)
in_features=torch.tensor([1,2,3,4],dtype=torch.float32)
weight_matrix=torch.tensor([
[1,2,3,4],
[2,3,4,5],
[3,4,5,6]
],dtype=torch.float32)
model4.fc.weight=nn.Parameter(weight_matrix)
out=model4(in_features) #这个时候权值是随机分配的
print(out)
class Model4(nn.Module):
def __init__(self): #初始化
super(Model4, self).__init__() #调用父类
self.fc = nn.Linear(4, 3,bias=False) #线性层 输入4 输出3
def forward(self, x):
x = self.fc(x)
x=F.softmax(x) #可以输出概率值
return x
model5=Model4()
print(model5)
in_features=torch.tensor([1,2,3,4],dtype=torch.float32)
weight_matrix=torch.tensor([
[1,2,3,4],
[2,3,4,5],
[3,4,5,6]
],dtype=torch.float32)
model5.fc.weight=nn.Parameter(weight_matrix)
out=model5(in_features) #这个时候权值是随机分配的
print(out)
in_features=torch.tensor([[1,2,3,4],[1,2,3,4]],dtype=torch.float32)
out=model5(in_features) #这个时候权值是随机分配的
print(out)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

转载请注明出处,欢迎讨论和交流!

浙公网安备 33010602011771号