IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> focal loss 多分类问题MNIST手写字学习 -> 正文阅读

[人工智能]focal loss 多分类问题MNIST手写字学习

focal loss参考
https://blog.csdn.net/u011583927/article/details/90716942

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/media/liao/My Passport/MNIST/", one_hot=True)
def multi_category_focal_loss1(y_true, y_pred):
    epsilon = 1.e-7
    gamma = 1.0
    #alpha = tf.constant([[2],[1],[1],[1],[1]], dtype=tf.float32)
    alpha = tf.constant([[1],[1],[1],[1],[1],[1],[1],[1],[1],[1]], dtype=tf.float32)

    y_true = tf.cast(y_true, tf.float32)
    y_pred = tf.clip_by_value(y_pred, epsilon, 1. - epsilon)
    y_t = tf.multiply(y_true, y_pred) + tf.multiply(1-y_true, 1-y_pred)
    ce = -tf.log(y_t)
    weight = tf.pow(tf.subtract(1., y_t), gamma)
    fl = tf.matmul(tf.multiply(weight, ce), alpha)
    loss = tf.reduce_mean(fl)
    return loss
        
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
weight = tf.Variable(tf.zeros([784, 10]))
bias = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, weight) + bias
y_softmax=tf.nn.softmax(y)
#cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
focal_loss=multi_category_focal_loss1(y_,y_softmax)
cross_entropy=focal_loss
#cross_entropy  = -tf.reduce_mean(tf.multiply(weight_x, tf.log(prob)))
#cross_entropy = focal_loss(logits=y, labels=tf.argmax(y_, 1))
train_op = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.Session() as sess:
    tf.global_variables_initializer().run()

    validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels}
    test_feed = {x: mnist.test.images, y_: mnist.test.labels}

    for i in range(30000):
        if i % 1000 == 0:
            validate_accuracy = sess.run(accuracy, feed_dict=validate_feed)
            print("After %d trainging step(s) ,validation accuracy"
                  "using average model is %g%%" % (i, validate_accuracy * 100))
	    f_loss=sess.run(focal_loss,feed_dict=validate_feed)
	    print(f_loss,f_loss.shape)
        xs, ys = mnist.train.next_batch(100)
        sess.run(train_op, feed_dict={x: xs, y_: ys})

    test_accuracy = sess.run(accuracy, feed_dict=test_feed)
    print("After 30000 trainging step(s) ,test accuracy using average"
          " model is %g%%" % (test_accuracy * 100))
After 0 trainging step(s) ,validation accuracyusing average model is 9.14%
(2.1671617, ())
After 1000 trainging step(s) ,validation accuracyusing average model is 59.38%
(7.243082, ())
After 2000 trainging step(s) ,validation accuracyusing average model is 57.86%
(7.5029922, ())
After 3000 trainging step(s) ,validation accuracyusing average model is 62.8%
(7.0280437, ())
After 4000 trainging step(s) ,validation accuracyusing average model is 61.74%
(6.5108142, ())
After 5000 trainging step(s) ,validation accuracyusing average model is 58.18%
(7.4595795, ())
After 6000 trainging step(s) ,validation accuracyusing average model is 59.26%
(7.498939, ())
After 7000 trainging step(s) ,validation accuracyusing average model is 59.78%
(7.1583071, ())
After 8000 trainging step(s) ,validation accuracyusing average model is 60.56%
(7.7969065, ())
After 9000 trainging step(s) ,validation accuracyusing average model is 59.78%
(8.9209671, ())
After 10000 trainging step(s) ,validation accuracyusing average model is 59.32%
(7.4151506, ())
After 11000 trainging step(s) ,validation accuracyusing average model is 50.9%
(10.775988, ())
After 12000 trainging step(s) ,validation accuracyusing average model is 62.74%
(7.0292954, ())
After 13000 trainging step(s) ,validation accuracyusing average model is 57.54%
(7.7753515, ())
After 14000 trainging step(s) ,validation accuracyusing average model is 62.86%
(6.4984117, ())
After 15000 trainging step(s) ,validation accuracyusing average model is 56.66%
(9.2939491, ())
After 16000 trainging step(s) ,validation accuracyusing average model is 56.52%
(7.5239344, ())
After 17000 trainging step(s) ,validation accuracyusing average model is 60.24%
(7.5586343, ())
After 18000 trainging step(s) ,validation accuracyusing average model is 61.98%
(6.5576291, ())
After 19000 trainging step(s) ,validation accuracyusing average model is 61.1%
(7.1839633, ())
After 20000 trainging step(s) ,validation accuracyusing average model is 61.98%
(7.9907374, ())
After 21000 trainging step(s) ,validation accuracyusing average model is 62.2%
(6.2956591, ())
After 22000 trainging step(s) ,validation accuracyusing average model is 58.12%
(6.9647093, ())
After 23000 trainging step(s) ,validation accuracyusing average model is 58.6%
(7.16395, ())
After 24000 trainging step(s) ,validation accuracyusing average model is 55.66%
(7.9839039, ())
After 25000 trainging step(s) ,validation accuracyusing average model is 59.2%
(8.3046751, ())
After 26000 trainging step(s) ,validation accuracyusing average model is 61.86%
(6.7330093, ())
After 27000 trainging step(s) ,validation accuracyusing average model is 62.24%
(6.7317719, ())
After 28000 trainging step(s) ,validation accuracyusing average model is 59.66%
(8.54006, ())
After 29000 trainging step(s) ,validation accuracyusing average model is 61.54%
(6.5660267, ())
After 30000 trainging step(s) ,test accuracy using average model is 60.31%

好像不如原本使用softmax交叉商损失函数的情况,如下

After 0 trainging step(s) ,validation accuracyusing average model is 9.14%
After 1000 trainging step(s) ,validation accuracyusing average model is 77.88%
After 2000 trainging step(s) ,validation accuracyusing average model is 81.52%
After 3000 trainging step(s) ,validation accuracyusing average model is 82.76%
After 4000 trainging step(s) ,validation accuracyusing average model is 79.52%
After 5000 trainging step(s) ,validation accuracyusing average model is 73.92%
After 6000 trainging step(s) ,validation accuracyusing average model is 81.64%
After 7000 trainging step(s) ,validation accuracyusing average model is 83.04%
After 8000 trainging step(s) ,validation accuracyusing average model is 76.26%
After 9000 trainging step(s) ,validation accuracyusing average model is 76.14%
After 10000 trainging step(s) ,validation accuracyusing average model is 73.8%
After 11000 trainging step(s) ,validation accuracyusing average model is 84.16%
After 12000 trainging step(s) ,validation accuracyusing average model is 81.18%
After 13000 trainging step(s) ,validation accuracyusing average model is 82.68%
After 14000 trainging step(s) ,validation accuracyusing average model is 78.26%
After 15000 trainging step(s) ,validation accuracyusing average model is 80.88%
After 16000 trainging step(s) ,validation accuracyusing average model is 76.52%
After 17000 trainging step(s) ,validation accuracyusing average model is 84.26%
After 18000 trainging step(s) ,validation accuracyusing average model is 77.52%
After 19000 trainging step(s) ,validation accuracyusing average model is 71.3%
After 20000 trainging step(s) ,validation accuracyusing average model is 77.08%
After 21000 trainging step(s) ,validation accuracyusing average model is 78.76%
After 22000 trainging step(s) ,validation accuracyusing average model is 84.44%
After 23000 trainging step(s) ,validation accuracyusing average model is 82.98%
After 24000 trainging step(s) ,validation accuracyusing average model is 83.28%
After 25000 trainging step(s) ,validation accuracyusing average model is 80.84%
After 26000 trainging step(s) ,validation accuracyusing average model is 81.56%
After 27000 trainging step(s) ,validation accuracyusing average model is 69.84%
After 28000 trainging step(s) ,validation accuracyusing average model is 83.28%
After 29000 trainging step(s) ,validation accuracyusing average model is 77.96%
After 30000 trainging step(s) ,test accuracy using average model is 80.59%

Process finished with exit code 0

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-03-03 16:13:20  更:2022-03-03 16:19:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 18:44:01-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码