模拟 Tensorflow 神经网络的训练

模拟一个Tensorflow 使用 平方差错误作为 loss 函数的例子,被模拟的原始代码是

https://github.com/cj0012/AI-Practice-Tensorflow-Notes/blob/master/tf.zip 的tf3_6.py

只模拟第一轮的w1。

import tensorflow as tf
import numpy as np
def forwardnn(xa, w1 , w2 , ya) :
    i = 0 
    for  x in xa :
        a1 = np.dot( x , w1 )
        y = np.dot( a1 , w2 ) 
        ya[ i ] = y
        i = i + 1
def  squareError( y_a , ya ) :
    e = 0.0
    yindex = 0 ;
    for  y_    in   y_a  :
        y = ya[yindex]
        e1 = (y_[0]- y[0]) * (y_[0]- y[0])
        e  += e1
    return e 


def  learn( layer ,  w1, w2 , wnew ,  xa , ya , y_a , learnRate , wDelta  ) :
    if layer == 1 :  #w1
        for  r  in   range( len( w1 ) )  :
            for c in  range ( len ( w1[r] ) ) :
                v = w1[r][c]
                forwardnn( xa ,  w1 , w2 , ya )
                e1 = squareError( y_a , ya )
                w1[r][c] += wDelta
                forwardnn( xa ,  w1 , w2 , ya )
                e2 = squareError( y_a , ya )
                learnN = (e2-e1)/wDelta * learnRate # (e2-e1)/wDelta :loss函數的微分 
                #print( r, "," ,c , ":" , v - learnN )
                w1 [r][c] =  v
                wnew[r][c]  =  v- learnN  


def learnMain() :
    BATCH_SIZE = 2
    X = [
        [1.0,2.0] , [2.0,3.0] ,[1.0,4.0],[2.0,5.0] ]
    Y_ = [[1.0], [0.0] ,[0.0], [1.0]] 
    print ("X:\n",X)
    print ("Y_:\n",Y_)

    w1=  [[1.0,1.0,1.0],[2.0,1.0,1.0]]
    w2= [[2.0],[3.0],[5.0]]

    Y = [    [0.0], [0.0] ,[0.0], [0.0] ]  
       
    STEPS = 1
    w1new = [[0.0,0.0,0.0],[0.0,0.0,0.0]]
    for i in range(STEPS):
        start = (i*BATCH_SIZE) % 4
        end =  start + BATCH_SIZE
        xa = X[start:end]
        ya = Y[start:end]
        y_a = Y_[start:end]
        print (w1) 
        learn( 1 ,  w1, w2 , w1new ,  xa , ya , y_a , 0.001 ,0.000001 )

        print ( w1new )

        w1 = w1new[:]

learnMain()



阅读更多
文章标签: python tensorflow
个人分类: python tensorflow
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

模拟 Tensorflow 神经网络的训练

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭