• 从零开始写代码-python解深度学习神经网络题目


    题目来源:华为2020软挑热身赛

    题目源文件:https://github.com/CatKungfu/huawei2020-LR

    神经网络思路分析请看上一个解说视频:https://www.bilibili.com/video/BV1CK4y1A7jS/

    本期博客视频版:https://www.bilibili.com/video/BV1xg411G7Dx/

     

     

     

     

     

     

     

    #date:2021-05-31
    #author:会武术的白猫
    #theme:神经网络
    
    import numpy as np
    
    def read_train_data():#读取txt数据并整理成可以处理类型
        with open("data/train_data.txt",'r')as f:
            content=f.readlines()
        result_x=[]
        answer_Y=[]
        for item in content:
            jtem=item.split(',')
            answer_Y.append(int(jtem[-1]))
            jtem.pop(-1)
            result_x.append([float(i) for i in jtem])
        return [result_x,answer_Y]
    
    def read_predict_data():#同上
        with open("data/test_data.txt",'r')as f:
            content=f.readlines()
        result_x=[]
        for item in content:
            jtem=item.split(',')
            result_x.append([float(i) for i in jtem])
        return result_x
    
    def read_answer_data():#同上
        with open("data/answer.txt",'r')as f:
            content=f.readlines()
        answer_Y=[int(i) for i in content]
        return answer_Y
    
    def sigmod(x):#激活函数计算
        return 1/(1+np.exp(-x))
    
    def der_sigmod(x):#激活函数的导数计算
        return sigmod(x)*(1-sigmod(x))
    
    def train():#训练函数,算出k权值矩阵和w权值矩阵
        res=read_train_data()
        result_x=res[0]
        answer_Y=res[1]
        n=len(result_x[0])
        error_min=0.001
        study=0.1
        nerve_num=8
        weight_k=np.random.random(size=(n,4))
        weight_w=np.random.random(size=(4,1))
        for i in range(len(result_x)):
            item=np.array(result_x[i])
            nerve_a=np.dot(item,weight_k).reshape(1,4)
            nerve_o=np.array([sigmod(j) for j in nerve_a]).reshape(1,4)
            y1=np.dot(nerve_o,weight_w)
            y2=sigmod(y1)
            L=(answer_Y[i]-y2)*(answer_Y[i]-y2)/float(2)
            if L<error_min:break
            mid=(y2-answer_Y[i])*der_sigmod(y1)
            weight_w-=study*mid*nerve_o.T
            mid_w=weight_w*(np.array([sigmod(j) for j in nerve_a]).T)
            weight_k-=study*mid*np.dot(mid_w,item.reshape(1,n)).T
            #return [weight_k,weight_w]
        return [weight_k,weight_w]
    
    def predict():#读取数据给出预测二分类,计算正确率
        result_x=read_predict_data()
        res=train()
        weight_k=res[0]
        weight_w=res[1]
        nerve_a=np.dot(np.array(result_x),weight_k)
        nerve_o=np.array([sigmod(nerve_a[i][j]) for i in range(len(result_x)) for j in range(4)]).reshape(len(result_x),4)
        y1=np.dot(nerve_o,weight_w)
        y2=[sigmod(i) for i in y1]
        answer_Y=read_answer_data()
        num=0
        for i in range(len(answer_Y)):
            if (y2[i]>=0.5 and answer_Y[i]==1) or (y2[i]<0.5 and answer_Y[i]==0):
                num+=1
        result=num/float(len(result_x))
        print("正确率为{}%".format(result*100))
    
    #read_train_data()
    #read_predict_data()
    #read_answer_data()
    #train()
    predict()
  • 相关阅读:
    求菲波那契数列的第n个数
    一个球,初始高度100,每次落下回弹一半高度,求第n次落下球走的距离
    MySQL优化
    linux常用命令2
    win7安装ANT
    win7配置java环境变量
    kvm虚拟机磁盘文件读取小结
    kvm linux虚拟机在线扩展磁盘
    binlog2sql
    linux上 查看mysql的binglog日志
  • 原文地址:https://www.cnblogs.com/ljy1227476113/p/14832463.html
Copyright © 2020-2023  润新知