#导入相应的包
import torch
from torch import nn,optim
import torch.nn.functional as f
import torch.utils.data as data
from __future__ import print_function
import loadData
import readAttribute
import numpy as np
#搭建网络
class FModule(nn.Module):
def __init__(self,L1in,current,L2out):
super(FModule,self).__init__()
self.l1 = nn.Linear(L1in,current)
self.tan = nn.Tanh()
self.weight = torch.randn(current,L2out,requires_grad=True)
def forward(self,xin,pin):
f = self.l1(xin)
f = self.tan(f)
m = torch.mm(f,self.weight)
pm = torch.mm(m,pin)
return pm
#自定义损失函数
class lossmoudle(nn.Module):
def forward(self,pm,inI,R,batch,classes):
t = torch.zeros(batch,classes)
for i in range(batch):
for j in range(classes):
l = torch.zeros(2)
l[1] = R - inI[i,j]*pm[i,j]
零样本学习(加代码实现)
最新推荐文章于 2024-08-05 10:01:34 发布
该博客介绍了如何使用PyTorch实现零样本学习。通过搭建FModule网络、定义自定义损失函数lossmoudle,并从数据集中读取样本,进行训练和测试。博主详细展示了代码实现过程,包括数据预处理、模型训练以及结果评估。
摘要由CSDN通过智能技术生成