神经网络入门(python 自建)

本文介绍了使用Python从头构建神经网络的基础过程,包括训练权重参数θ的获取及测试。测试结果显示精度有待提高,该网络为两层结构,简化了偏置项θ0的处理。
摘要由CSDN通过智能技术生成

训练得到θ

import numpy as np
import pandas as pd
from keras.datasets import  mnist

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def top(x,y,o):
    z=x.dot(o)
    h=sigmoid(z)
    j=np.sum(y*np.log(h)+(1-y)*np.log(1-h))
    return x,y,h,o,j # h 为预测,j 为代价函数

def bottom(x,y,h,o,j):
    s=h-y
    o=o-(1/x.shape[0])*(x.T).dot(s)
    return o # o 为修正后的参数

def begin(x,y):
    o=np.zeros((x.shape[1], y.shape[1]))
    return x,y,o


# 数据准备
# x =
# x1
# x2
# x3
#
# y =
# y1
# y2
# y3

(train_data, train_labels), (test_data, test_labels) = mnist.load_data()

x=train_data.reshape(60000,784)
y=np.zeros((60000,10))
for i in range(train_labels.shape[0]):
    y[i,train_labels[i]]=1


# 调用网络,该网络只有输入输出层
b=begin(x,y)
t=top(b[0],b[1],b[2])
o=bottom(t[0],t[1],t[2],t[3],t[4])
for i in range(9):
    t = top(x, y, o)
    o=bottom(t[0],t[1],t[2],t[3],t[4])
    print(o) # 在控制台显示

# 写入到
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值