tensorflow学习笔记(3)前置数学知识

              tensorflow学习笔记(3)前置数学知识

首先是神经元的模型

接下来是激励函数

神经网络的复杂度计算

层数:隐藏层+输出层

总参数=总的w+b

下图为2层

如下图

w为3*4+4个   b为4*2+2

接下来是损失函数

主流的有均分误差,交叉熵,以及自定义

这里贴上课程里面的代码

# -*- coding: utf-8 -*-
"""
Created on Sat May 26 18:42:08 2018

@author: Administrator
"""

import tensorflow as tf
import numpy as np
BATCH_SIZE=8
seed=23455

#基于seed产生随机数
rdm=np.random.RandomState(seed)
#初始化特征值为32个样本*2个特征值
#初始化标签
X=rdm.rand(32,2)
Y_=[[x1+x2+(rdm.rand()/10.0-0.05)] for (x1,x2) in X]


#定义输入,参数和输出和传播过程
x=tf.placeholder(tf.float32,shape=(None,2))
y_=tf.placeholder(tf.float32,shape=(None,1))
w1=tf.Variable(tf.random_normal([2,1],stddev=1,seed=1))
y=tf.matmul(x,w1)


#定义损失函数以及反向传播方法
loss_mse=tf.reduce_mean(tf.square(y_-y))
train_step=tf.train.GradientDescentOptimizer(0.01).minimize(loss_mse)

#会话训练
with tf.Session() as sess:
    init_op=tf.global_variables_initializer()
    sess.run(init_op)
    STEPS=20000
    for i in range(STEPS):
        start=(i*BATCH_SIZE)%32
        end=(i*BATCH_SIZE)%32+BATCH_SIZE
        #每次训练抽取start到end的数据
        sess.run(train_step,feed_dict={x:X[start:end],y_:Y_[start:end]})
        #每500次打印一次参数
        if i%500==0:
            print("在%d次迭代后,参数为"%(i))
            print(sess.run(w1))
    #输出训练后的参数
    print("\n")
    print("FINAL w1 is:",sess.run(w1))
    

自定义损失函数

loss=tf.reduce_sum(tf.where(tf.greater(y,y_),COST(y-y_),PROFIT(y_-y)))

中间的where是判断y是否大于y_

如图

猜你喜欢

转载自www.cnblogs.com/DJC-BLOG/p/9094771.html