当前位置:网站首页>A super simple neural network code with 5 coordinates for one layer node training
A super simple neural network code with 5 coordinates for one layer node training
2022-07-26 20:28:00 【Xiao help】
ILearnDeepLearning.py/Numpy deep neural network.ipynb at master · SkalskiP/ILearnDeepLearning.py · GitHub I extract a layer of node training according to the code in this article 5 Super simple neural network code of coordinates The second quadrant point is 0 class , The first quadrant is 1 class import numpy as np def sigmoid(x): return 1.0/(1+np.exp(-x)) def sigmoid_backward( Z): sig = sigmoid(Z) return sig * (1 - sig) # The negative number of the first coordinate is 0 class , The first coordinate with positive number is 1 class X = np.array( [ [3.,5.], [4.,2.], [1.,1.], [-5.,4.], [-4.,2.] ] ) Y = np.array( [ 1,1,1,0,0 ] ) if __name__ == '__main__': n_samples = X.shape[0] print( X.T.shape ) W = np.array([[1.0, 1.0]]) b = np.array([[1.0]]) for i in range(100): # Forward propagation Z = np.dot( W, X.T) + b A = sigmoid(Z) Y_hat = A # Back propagation dA = 2*( Y_hat - Y ) dZ = dA * sigmoid_backward( Z ) dW = np.dot(dZ, X) / n_samples db = np.sum(dZ, axis=1, keepdims=True) / n_samples #0.01 It's the learning rate , I think it's the rate of convergence W -= 0.01 * dW b -= 0.01 * db #print( dA ) continue # Test data The first is 1 class , The second is 0 class The first coordinate number is too close 0, The easier it is to make mistakes X = np.array([ [30., 5.], # The first is 1 class [-40, 2.] ]) # The second is 0 class Z = np.dot(W, X.T) + b A = sigmoid(Z) Y_hat = A print(Y_hat)
There are differences between programming expression and mathematical formula expression , It is difficult to get the correct prediction results if programming purely according to mathematical formulas 1、 if Z'(A)dA=dZ(A); When programming gradient descent ,dA Expressed as Z'(A),dZ(A) As a 1, Don't subtract the marked value from the predicted value :(Z^-Z) namely dA=Z'(A)*1, Finally, multiply it by the learning rate . also dA Do not express as dZ(A)/Z'(A), Do not express as (Z^-Z)/Z'(A), Do not express as Z'(A)*(Z^-Z) 2、 if W*X+B=Z, be dW=dZ*X.T,X.T yes X The transpose matrix of .X It's usually n That's ok 1 Column .
Here is what I look like after correcting my code , Discovery use △ by 0.000001 A similar prediction can also be obtained by replacing the derivative function with the calculated slope value of :
import numpy as np
def sigmoid(x):
return 1.0/(1+np.exp(-x))
def sigmoid_backward( Z):
sig = sigmoid(Z)
return np.multiply( sig , (1 - sig) )
def square(x):
return np.multiply( x, x )
def derivative( func , x, d=0.000001):
return (func(x+d)-func(x)) / d
x= [ [[3.],[5.]], [[4.],[2.]], [[2.],[3.]], [[1.],[1.]], [[-1.],[5.]], [[-5.],[4.]], [[-4.],[2.]], [[-3.2],[2.5]] ]
type= [ [[1.],[0.]], [[1.],[0.]], [[1.],[0.]], [[1.],[0.]], [[0.],[1.]], [[0.],[1.]], [[0.],[1.]], [[0.],[1.]] ]
x= [ [[3.],[5.]], [[-1.],[5.]] ]
type= [ [[1.],[0.]], [[0.],[1.]] ]
A=[ [1.,1.], [1.,1.] ]
B=[ [1.],[1.] ]
if __name__ == '__main__':
count = len(x)
xList= x
yList= type
for i in range(10000):
oList = []
ypList = []
for x in xList:
x = np.matrix(x)
o = np.matrix(A)*(x)+B
oList.append( o )
yp= sigmoid(o)
ypList.append( yp )
E = 0
M = 0
for index ,yp in enumerate(ypList):
k = np.multiply( derivative(square, yp - yList[index], 0.0001), #2*(yp - yList[index]) , #derivative(square, yp - yList[index], 0.0001),
derivative( sigmoid, oList[index], 0.0001 ) ) #sigmoid_backward( oList[index] ) )
#derivative( sigmoid, oList[index], 0.0001 ))
m = k #np.multiply(k, yList[index] - yp)
M = M + m
E = E + ( m*( np.matrix(xList[index]).T ) )
E = E/count
M = M/count
A -= np.multiply( 0.1, E )
B -= np.multiply( 0.1, M )
#print( ypList )
oList = []
ypList = []
for x in xList:
x = np.matrix(x)
o = A * x + B
oList.append(o)
yp = sigmoid(o)
ypList.append(yp)
print( ypList )边栏推荐
- 密室逃脱、剧本杀加强监管 重点加强消防安全和未成年人保护
- 为什么 ThreadLocal 可以做到线程隔离?
- ES6 method & Class array into real array & method of judging array
- Read the four service types of kubernetes!
- The Sandbox 和艺术家 Alec Monopoly 达成合作
- Is flush reliable? I just started to learn financial management. Is it safe to open a securities account?
- 小场景带来大提升!百度飞桨EasyDL助力制造业流水线AI升级
- Task 1 report
- numpy.put()
- 小公司小而美的产品,如何突围?
猜你喜欢

Parallel execution (II). Multiprocessing

Read the four service types of kubernetes!

The typing competition is over!

聊天软件项目开发2

内网渗透学习(二)信息收集

用 QuestPDF操作生成PDF更快更高效!

Week 6 Convolutional Neural Networks (CNNs)

SQL injection

20220725 tree array introspection

Introduction to component functions of blueprism process business object Chapter 3 of RPA
随机推荐
Definition and use of one-dimensional array
Is flush reliable? I just started to learn financial management. Is it safe to open a securities account?
Shell script basic programming commands
20220725树状数组入门反思
使用百度飞桨 EasyDL 完成垃圾分类
解决AttributeError: module ‘win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu
three. Two methods of making Earth annotation with JS
EtherCAT 同步模式
MySQL InnoDB engine (V)
I tried many report tools and finally found a report based on Net 6
Principle and application of one click login of local number (glory Collection Edition)
this指向-超经典面试题
Three implementation methods of C # client program calling external program
使用请求头认证来测试需要授权的 API 接口
When there are many query fields, you can add ordinary query and advanced query
猿辅导的科技硬实力:让AI从读懂孩子作业开始
如何实现一个能处理海量数据的异步任务队列系统(至尊典藏版)
There is an Oolong incident in open source. Maybe you don't know these five open source protocols
有点酷,使用 .NET MAUI 探索太空
STM32F103有源蜂鸣器驱动程序