当前位置: 代码迷 >> 综合 >> NG 神经网络 构建网络基础
  详细解决方案

NG 神经网络 构建网络基础

热度:98   发布时间:2023-12-08 12:22:00.0
import numpy as np
import pandas as pd# 两层神经网络
# sigmoid 函数 输出0-1 分界为0.5
print('-----------------------------两层神经网络-----------------------------------')
def nonlin(x,deriv=False):if(deriv==True):return x*(1-x)return 1/(1+np.exp(-x))X = np.array([  [1,0,0],[1,0,1],[1,1,0],[1,1,1] ])y = np.array([[0,0,0,1]]).T# 为你的随机数设定产生种子是一个良好的习惯
np.random.seed(1)# 下面目的矩阵的值从 0->1 扩充到 -1 ->  1
syn0 = 2*np.random.random((3,1)) - 1
# print(syn0)# for iter in range(10000):
for j in range(100000):l0 = Xl1 = nonlin(np.dot(l0,syn0))l1_error = y - l1# # 误差控制if (j% 1000) == 0:print ("Error:" + str(np.mean(np.abs(l1_error))))#     # 精度控制if (np.mean(np.abs(l1_error))<=0.01):print('迭代次数:',j,' 迭代精度:',np.mean(np.abs(l1_error)))breakl1_delta = l1_error * nonlin(l1,True)# update weightssyn0 += np.dot(l0.T,l1_delta)# print(syn0) #输出权值
l0=np.array([[1,0,1],[1,1,0],[1,0,0],[1,1,1]])
l1 = nonlin(np.dot(l0,syn0))
print(l1)
print(l1[l1<=0.5])
print(l1[l1>0.5])print('\n\n\n-----------------------------三层神经网络-----------------------------------')
# 三层神经网络
import numpy as npdef nonlin(x,deriv=False):if(deriv==True):return x*(1-x)return 1/(1+np.exp(-x))X = np.array([[1,0,0],[1,0,1],[1,1,0],[1,1,0],[1,1,1]])y = np.array([[1],[0],[0],[0],[1]])np.ra
  相关解决方案