zoukankan      html  css  js  c++  java
  • 从零开始写代码-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()
  • 相关阅读:
    在Leangoo里怎么添加,移动列表,修改列表名称?
    在Leangoo里怎么列表示例,插入列表?
    tomcat如何按站点调试本机程序
    ORA-12519, TNS:no appropriate service handler found
    mysql 远程访问
    手机端调试成功笔记
    Cannot find class for bean with name service
    android模拟器不能用键盘
    eclipse使用基础--让toolbar显示自己想要的内容
    mysql解压版安装
  • 原文地址:https://www.cnblogs.com/ljy1227476113/p/14832463.html
Copyright © 2011-2022 走看看