zoukankan      html  css  js  c++  java
  • 损失函数及其梯度

    Typical Loss

    • Mean Squared Error

    • Cross Entropy Loss

      • binary
      • multi-class
      • +softmax

    MSE

    • loss=[y(xw+b)]2

    • L2norm=||y(xw+b)||2

    • loss=norm(y(xw+b))2

    Derivative

    • loss=[yfθ(x)]2

    • lossθ=2[yfθ(x)]fθ(x)θ

    MSE Gradient

    import tensorflow as tf
    
    x = tf.random.normal([2, 4])
    w = tf.random.normal([4, 3])
    b = tf.zeros([3])
    y = tf.constant([2, 0])
    

    with tf.GradientTape() as tape:
    tape.watch([w, b])
    prob = tf.nn.softmax(x @ w + b, axis=1)
    loss = tf.reduce_mean(tf.losses.MSE(tf.one_hot(y, depth=3), prob))

    grads = tape.gradient(loss, [w, b])
    grads[0]

    <tf.Tensor: id=92, shape=(4, 3), dtype=float32, numpy=
    array([[ 0.01156707, -0.00927749, -0.00228957],
           [ 0.03556816, -0.03894382,  0.00337564],
           [-0.02537526,  0.01924876,  0.00612648],
           [-0.0074787 ,  0.00161515,  0.00586352]], dtype=float32)>
    
    grads[1]
    
    <tf.Tensor: id=90, shape=(3,), dtype=float32, numpy=array([-0.01552947,  0.01993286, -0.00440337], dtype=float32)>
    

    Softmax

    • soft version of max

    • 大的越来越大,小的越来越小、越密集

    21-损失函数及其梯度-softmax.jpg

    Derivative

    pi=eaik=1Neak

    • i=j

    piaj=eaik=1Neakaj=pi(1pj)

    • ij

    piaj=eaik=1Neakaj=pjpi

    x = tf.random.normal([2, 4])
    w = tf.random.normal([4, 3])
    b = tf.zeros([3])
    y = tf.constant([2, 0])
    

    with tf.GradientTape() as tape:
    tape.watch([w, b])
    logits =x @ w + b
    loss = tf.reduce_mean(
    tf.losses.categorical_crossentropy(tf.one_hot(y, depth=3),
    logits,
    from_logits=True))

    grads = tape.gradient(loss, [w, b])
    grads[0]

    <tf.Tensor: id=226, shape=(4, 3), dtype=float32, numpy=
    array([[-0.38076094,  0.33844548,  0.04231545],
           [-1.0262716 , -0.6730384 ,  1.69931   ],
           [ 0.20613424, -0.50421923,  0.298085  ],
           [ 0.5800004 , -0.22329211, -0.35670823]], dtype=float32)>
    
    grads[1]
    
    <tf.Tensor: id=224, shape=(3,), dtype=float32, numpy=array([-0.3719653 ,  0.53269935, -0.16073406], dtype=float32)>
  • 相关阅读:
    Goroutines和Channels(一)
    数据库索引
    查看文件命令
    Django本地开发,debug模式引用静态文件
    Django部署生产环境,静态文件不能访问404,以及图片不能访问403
    js 捕获浏览器后退事件
    win10,python3.6,django2.0.3,项目基本命令
    win10,配置python3.6,虚拟环境
    c代码,输出i,j,k互不相同的三位数
    c语言关键字的区分
  • 原文地址:https://www.cnblogs.com/abdm-989/p/14123298.html
Copyright © 2011-2022 走看看