单隐层前馈网络求最小值

import torch
import torch.nn as nn


def min_loss(x, y):
    return (x-y)**2


class funcnet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(4, 20)
        self.relu1 = torch.tanh
        self.fc2 = nn.Linear(20, 1)
        # self.relu2 = nn.LeakyReLU(negative_slope=0.1)

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu1(x)
        x = self.fc2(x)
        # x = self.relu2(x)
        return x


model = funcnet()
model.load_state_dict(torch.load('minnet.pth'))
pred = model(torch.tensor([10., 2.,3., 15]))
optimizer = torch.optim.Adam(model.parameters(), 0.001)
for epoch in range(100):
    for n in range(1000):
        data = 10*abs(torch.randn(4))
        label = torch.min(data)
        pred = model(data)
        train_loss = min_loss(pred, label)
        optimizer.zero_grad()
        train_loss.backward()
        optimizer.step()
    print(f'loss:{train_loss.item()}')
torch.save(model.state_dict(), 'minnet.pth')
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值