zoukankan      html  css  js  c++  java
  • 寒假学习进度10:tensorflow2.0 损失函数

    损失函数:预测值y与已知答案y_的差距。

    nn优化的目标:

    loss最小(mse(均方误差),自定义,ce(交叉熵))

    mse(均方误差)

    例题:预测酸奶日销量y,x1,x2是影响日销量因素。

    建模前,应预先采集的数据有:每日x1、 x2和销量y_ (即已知答案, 最佳情况:产量=销量)
    拟造数据集x1,x2,y_ : y_ =x1 + x2(随机生成x1,x2)
    噪声: -0.05~+0.05     拟合可以预测销量的函数

    代码:

    
    
    import tensorflow as tf
    import numpy as np

    SEED = 23455 #种子,可加可不加

    rdm = np.random.RandomState(seed=SEED) # 生成[0,1)之间的随机数
    x = rdm.rand(32, 2) #生成32行,2列的输入特征,包含32组0~1之间的随机数x1,x2
    y_ = [[x1 + x2 + (rdm.rand() / 10.0 - 0.05)] for (x1, x2) in x] #for循环为取出每组x1,x2 # 生成噪声[0,1)/10=[0,0.1); [0,0.1)-0.05=[-0.05,0.05)
    x = tf.cast(x, dtype=tf.float32) #转变数据类型

    w1 = tf.Variable(tf.random.normal([2, 1], stddev=1, seed=1)) #初始化,初始化为2行,1列

    epoch = 15000 #数据集迭代15000次
    lr = 0.002 #学习率为0.002

    for epoch in range(epoch):
    with tf.GradientTape() as tape:
    y = tf.matmul(x, w1) #使用with结构求前向传播计算结果y
    loss_mse = tf.reduce_mean(tf.square(y_ - y)) #求均方误差损失函数loss_mse

    grads = tape.gradient(loss_mse, w1) #损失函数对待训练参数w1求偏导
    w1.assign_sub(lr * grads) #更新参数w1

    if epoch % 500 == 0: #每迭代500轮打印当前参数w1
    print("After %d training steps,w1 is " % (epoch))
    print(w1.numpy(), " ")
    print("Final w1 is: ", w1.numpy())
     

    y=1.0009792*x1+0.9977485*x2,趋近于y=1*x1+1*x2所以对于酸奶日销量的拟合正确。

    自定义损失函数:

    如果预测商品销量,预测多了损失成本,预测少了损失利润,若利润与成本不相等,则mse产生的loss无法实现利益最大化。

    例题预测酸奶日销量,酸奶成本1元,酸奶利润99元,明显预测少了损失大,预测多了损失小,希望生成的预测函数往多了预测。

    import tensorflow as tf
    import numpy as np
    
    SEED = 23455
    COST = 1
    PROFIT = 99
    
    rdm = np.random.RandomState(SEED)
    x = rdm.rand(32, 2)
    y_ = [[x1 + x2 + (rdm.rand() / 10.0 - 0.05)] for (x1, x2) in x]  # 生成噪声[0,1)/10=[0,0.1); [0,0.1)-0.05=[-0.05,0.05)
    x = tf.cast(x, dtype=tf.float32)
    
    w1 = tf.Variable(tf.random.normal([2, 1], stddev=1, seed=1))
    
    epoch = 10000
    lr = 0.002
    
    for epoch in range(epoch):
        with tf.GradientTape() as tape:
            y = tf.matmul(x, w1)
            loss = tf.reduce_sum(tf.where(tf.greater(y, y_), (y - y_) * COST, (y_ - y) * PROFIT))
    
        grads = tape.gradient(loss, w1)
        w1.assign_sub(lr * grads)
    
        if epoch % 500 == 0:
            print("After %d training steps,w1 is " % (epoch))
            print(w1.numpy(), "
    ")
    print("Final w1 is: ", w1.numpy())
    
    # 自定义损失函数
    # 酸奶成本1元, 酸奶利润99元
    # 成本很低,利润很高,人们希望多预测些,生成模型系数大于1,往多了预测

    y=1.1626335*x1+1.1191947*x2  系数相对均方误差来书都偏大,模型在向多的方向预测。

    损失函数(ce):表征两个概率分布之间的距离。

    tensoflow2.0中交叉熵公式

    loss_ce1 = tf.losses.categorical_crossentropy(y_,y)
    标准答案y_,输出结果y
  • 相关阅读:
    Codeforces Round #200 (Div. 2) E. Read Time(二分)
    Codeforces Round #160 (Div. 2) D. Maxim and Restaurant(DP)
    TC SRM 593 DIV1 250
    TC SRM 593 DIV2 1000
    HDU 2825 Wireless Password(AC自动机+DP)
    Codeforces Round #203 (Div. 2)
    TC SRM 591 DIV2 1000
    HDU 4758 Walk Through Squares(AC自动机+DP)
    CF 346B. Lucky Common Subsequence(DP+KMP)
    HDU 4753 Fishhead’s Little Game(DFS)
  • 原文地址:https://www.cnblogs.com/yangqqq/p/14336424.html
Copyright © 2011-2022 走看看