- import matplotlib.pyplot as plt
- import torch
- import torch.nn as nn
- from torch.utils.data import Dataset,DataLoader
- import torch.optim as optim
- import numpy as np
- random_seed=1000
- # np.random.seed(random_seed)
- torch.manual_seed(random_seed)
-
- #自定义损失函数
- class my_loss(nn.Module):
- def __init__(self):
- super().__init__()
-
- def forward(self, x, y):
- # loss = nn.L1Loss()(x,y)
- # loss=nn.MSELoss()(x,y)
- # total_sum=torch.sum(torch.pow(x,2)+torch.pow(y,2))
- # total_sum = torch.sum(torch.pow(x, 2))
- # loss=torch.div(loss,total_sum)
- # loss=torch.mean(torch.sub(y,x))
- loss=torch.mean(torch.pow(torch.abs(torch.sub(y,x)),1))
- return loss
- # 定义模型
- class TheModelClass(nn.Module):
- def __init__(self):
- hidden=15
- super(TheModelClass, self).__init__()
- self.fc1 = nn.Linear(1,hidden)
- # self.relu=nn.Sigmoid()