PyTorch:解决异或问题XOR

试设计一个前馈神经网络来解决XOR问题

该前馈神经网络具有两个隐藏神经元和一个输出神经元

github地址

github代码地址

代码

# 利用Pytorch解决XOR问题
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np

data = np.array([[1, 0, 1], [0, 1, 1],
                 [1, 1, 0], [0, 0, 0]], dtype='float32')
x = data[:, :2]
y = data[:, 2]


# 初始化权重变量
def weight_init_normal(m):
    classname = m.__class__.__name__
    if classname.find('Linear') != -1:
        m.weight.data.normal_(0.0, 1.)
        m.bias.data.fill_(0.)


class XOR(nn.Module):
    def __init__(self):
        super(XOR, self).__init__()
        self.fc1 = nn.Linear(2, 2)   # 一个隐藏层 2个神经元
        self.fc2 = nn.Linear(2, 1)   # 输出层 1个神经元

    def forward(self, x):
        h1 = F.sigmoid(self.fc1(x))  # 之前也尝试过用ReLU作为激活函数, 太容易死亡ReLU了.
        h2 = F.sigmoid(self.fc2(h1))
        return h2


net = XOR()
net.apply(weight_init_normal)
x = torch.Tensor(x.reshape(-1, 2))
y = torch.Tensor(y.reshape(-1, 1))

# 定义loss function
criterion = nn.BCELoss()  # MSE
# 定义优化器
optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9)  # SGD
# 训练
for epoch in range(1000):
    optimizer.zero_grad()   # 清零梯度缓存区
    out = net(x)
    loss = criterion(out, y)
    print(loss)
    loss.backward()
    optimizer.step()  # 更新

# 测试
test = net(x)
print("input is {}".format(x.detach().numpy()))
print('out is {}'.format(test.detach().numpy()))

效果

在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值