人工神经网络实验:第一次上机

pratice03:

1.问题描述:

就是通过设置只有一个hidden层的神经网络,对一个平面的灰面和白面进行分类

2.完整代码:

# 可提供解题和验证代码,自行产生数据

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import tensor
from torch.nn import Parameter


class Act_abs(nn.Module):
    def __init__(self):
        super(Act_abs, self).__init__()

    def forward(self, x):
        x[abs(x) >= 1] = 1
        x[abs(x) < 1] = 0
        return x

class Act_v(nn.Module):
    def __init__(self):
        super(Act_v, self).__init__()

    def forward(self, x):
        x[x < 1] = 0
        x[x >= 1] = 1
        return x

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.lin1 = nn.Linear(2,2,bias =False)
        self.lin2 = nn.Linear(2,1,bias =False)
        self.lin1.weight = Parameter(tensor([[ 1.0,  0.0], [0.0, 1.0]]))
        self.lin2.weight = Parameter(tensor([[ 1.0,1.0]]))
        self.act1 = Act_abs()
        self.act2 = Act_abs()

    def forward(self, x):
        y = self.lin1(x)
        y = self.act1(y)
        y = self.lin2(y)
        y = self.act2(y)
        return y

if __name__ == '__main__':
    net = Net()
    # 第一类
    dotIn = [tensor([0.0,0.0]),tensor([0.2,0.2]),tensor([-0.2,-0.2]),tensor([0.2,-0.2]),tensor([-0.2,0.2])]
    # 边界点(第二类)
    dotEdge = [tensor([1.0,1.0]),tensor([-1.0,1.0]),tensor([1.0,-1.0]),tensor([-1.0,-1.0]),tensor([1.0, 0.2]), tensor([-1.0, 0.2]), tensor([1.0, -0.2]), tensor([-1.0, -0.2])]
    # 第二类
    dotOut = [tensor([1.5, 0.2]), tensor([-1.5, 0.2]), tensor([1.5, -0.2]), tensor([-1.5, -0.2])]
    print('----------内部点--------------')
    for x in dotIn:
        pre = net(x)
        if pre[0] == 0.0:
            print('第一类')
        else:
            print('第二类')
    print('----------边界点--------------')
    for x in dotEdge:
        pre = net(x)
        if pre[0] == 0.0:
            print('第一类')
        else:
            print('第二类')
    print('----------外部点--------------')
    for x in dotOut:
        pre = net(x)
        if pre[0] == 0.0:
            print('第一类')
        else:
            print('第二类')

    print('-----------------生成随机数据------------------')
    acc = 0
    rand_num = 10**4
    for i in range(rand_num):
        X = (torch.rand(2) - 0.5) * 4  # [-2, 2]
        if abs(X[0]) > 1 or abs(X[1]) > 1:
            y = tensor([1])
        else:
            y = tensor([0])

        pre = net(X)

        if pre == y:
            acc+=1

    print('识别准确率Acc: '+ str((acc/rand_num)*100) + '%')

3.代码分析过程:

(1)

这里做的事情就是,定义了一个Act_abs类,需要的参数是nn.Module类型的变量,里面定义了一个激活函数forward,需要传递一个数值参数x,forward就是x的绝对值大于1取1,否则取0的函数

(2)

定义一个Act_v的类,需要的参数也是nn.Module,和上面那个类基本相同,唯一的区别就是这里是x是x的真实值大于1才取值1

(3)

这里面定义了一个神经网络的结构:

所谓的Linear层,就是用来将输入向量转化为另一个输出向量,

这里定义了2个线性层,后面设置了对应的线性系数weight1和weight2,

其中使用了2个相同的abs的激活函数层,

最终的神经网络结构如下:

因为是二分类的问题,所以激活函数的选择就是 一个类似bool函数的函数,要么0要么1作为输出,而且之所以要使用abs绝对值,就是因为这个正方形处于4个象限,需要绝对值进行判断是在正方形内部还是外部

好了。终于发现,我就是一个憨皮,

其实,这里根本就没有什么train和test什么的,这里其实根本没有gradient descend什么的,因为根本就不会进行什么参数调整,

仔细看这个结构,就会发现,它相当于实现了c++里面的:

if(abs(x)>=1 && abs(y)>=1) return 1

这个效果,所以,我感觉它在玩我,

可能就是想模仿一下之后“真正”的深度学习模型吧,

反正这里就很简单

猜你喜欢

转载自blog.csdn.net/xiao_ZHEDA/article/details/132608890