最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

TensorFlow繪制loss/accuracy曲線的實(shí)例

 更新時(shí)間:2020年01月21日 15:12:56   作者:The_Thinker_QChen  
今天小編就為大家分享一篇TensorFlow繪制loss/accuracy曲線的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

1. 多曲線

1.1 使用pyplot方式

import numpy as np
import matplotlib.pyplot as plt
 
x = np.arange(1, 11, 1)
 
plt.plot(x, x * 2, label="First")
plt.plot(x, x * 3, label="Second")
plt.plot(x, x * 4, label="Third")
 
plt.legend(loc=0, ncol=1)  # 參數(shù):loc設(shè)置顯示的位置,0是自適應(yīng);ncol設(shè)置顯示的列數(shù)
 
plt.show()

1.2 使用面向?qū)ο蠓绞?/strong>

import numpy as np
import matplotlib.pyplot as plt
 
x = np.arange(1, 11, 1)
 
fig = plt.figure()
ax = fig.add_subplot(111)
 
 
ax.plot(x, x * 2, label="First")
ax.plot(x, x * 3, label="Second")
 
ax.legend(loc=0)
# ax.plot(x, x * 2)
# ax.legend([”Demo“], loc=0)
 
plt.show()

2. 雙y軸曲線

雙y軸曲線圖例合并是一個(gè)棘手的操作,現(xiàn)以MNIST案例中l(wèi)oss/accuracy繪制曲線。

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time
import matplotlib.pyplot as plt
import numpy as np
 
x_data = tf.placeholder(tf.float32, [None, 784])
y_data = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x_data, [-1, 28, 28, 1])
 
# convolve layer 1
filter1 = tf.Variable(tf.truncated_normal([5, 5, 1, 6]))
bias1 = tf.Variable(tf.truncated_normal([6]))
conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding='SAME')
h_conv1 = tf.nn.sigmoid(conv1 + bias1)
maxPool2 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
 
# convolve layer 2
filter2 = tf.Variable(tf.truncated_normal([5, 5, 6, 16]))
bias2 = tf.Variable(tf.truncated_normal([16]))
conv2 = tf.nn.conv2d(maxPool2, filter2, strides=[1, 1, 1, 1], padding='SAME')
h_conv2 = tf.nn.sigmoid(conv2 + bias2)
maxPool3 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
 
# convolve layer 3
filter3 = tf.Variable(tf.truncated_normal([5, 5, 16, 120]))
bias3 = tf.Variable(tf.truncated_normal([120]))
conv3 = tf.nn.conv2d(maxPool3, filter3, strides=[1, 1, 1, 1], padding='SAME')
h_conv3 = tf.nn.sigmoid(conv3 + bias3)
 
# full connection layer 1
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 120, 80]))
b_fc1 = tf.Variable(tf.truncated_normal([80]))
h_pool2_flat = tf.reshape(h_conv3, [-1, 7 * 7 * 120])
h_fc1 = tf.nn.sigmoid(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
 
# full connection layer 2
W_fc2 = tf.Variable(tf.truncated_normal([80, 10]))
b_fc2 = tf.Variable(tf.truncated_normal([10]))
y_model = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
 
cross_entropy = - tf.reduce_sum(y_data * tf.log(y_model))
 
train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy)
 
sess = tf.InteractiveSession()
correct_prediction = tf.equal(tf.argmax(y_data, 1), tf.argmax(y_model, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.global_variables_initializer())
 
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
 
fig_loss = np.zeros([1000])
fig_accuracy = np.zeros([1000])
 
start_time = time.time()
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(200)
  if i % 100 == 0:
    train_accuracy = sess.run(accuracy, feed_dict={x_data: batch_xs, y_data: batch_ys})
    print("step %d, train accuracy %g" % (i, train_accuracy))
    end_time = time.time()
    print("time:", (end_time - start_time))
    start_time = end_time
    print("********************************")
  train_step.run(feed_dict={x_data: batch_xs, y_data: batch_ys})
  fig_loss[i] = sess.run(cross_entropy, feed_dict={x_data: batch_xs, y_data: batch_ys})
  fig_accuracy[i] = sess.run(accuracy, feed_dict={x_data: batch_xs, y_data: batch_ys})
print("test accuracy %g" % sess.run(accuracy, feed_dict={x_data: mnist.test.images, y_data: mnist.test.labels}))
 
 
# 繪制曲線
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
lns1 = ax1.plot(np.arange(1000), fig_loss, label="Loss")
# 按一定間隔顯示實(shí)現(xiàn)方法
# ax2.plot(200 * np.arange(len(fig_accuracy)), fig_accuracy, 'r')
lns2 = ax2.plot(np.arange(1000), fig_accuracy, 'r', label="Accuracy")
ax1.set_xlabel('iteration')
ax1.set_ylabel('training loss')
ax2.set_ylabel('training accuracy')
# 合并圖例
lns = lns1 + lns2
labels = ["Loss", "Accuracy"]
# labels = [l.get_label() for l in lns]
plt.legend(lns, labels, loc=7)
plt.show()

注:數(shù)據(jù)集保存在MNIST_data文件夾下

其實(shí)就是三步:

1)分別定義loss/accuracy一維數(shù)組

fig_loss = np.zeros([1000])
fig_accuracy = np.zeros([1000])
# 按間隔定義方式:fig_accuracy = np.zeros(int(np.ceil(iteration / interval)))

2)填充真實(shí)數(shù)據(jù)

 fig_loss[i] = sess.run(cross_entropy, feed_dict={x_data: batch_xs, y_data: batch_ys})
 fig_accuracy[i] = sess.run(accuracy, feed_dict={x_data: batch_xs, y_data: batch_ys})

3)繪制曲線

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
lns1 = ax1.plot(np.arange(1000), fig_loss, label="Loss")
# 按一定間隔顯示實(shí)現(xiàn)方法
# ax2.plot(200 * np.arange(len(fig_accuracy)), fig_accuracy, 'r')
lns2 = ax2.plot(np.arange(1000), fig_accuracy, 'r', label="Accuracy")
ax1.set_xlabel('iteration')
ax1.set_ylabel('training loss')
ax2.set_ylabel('training accuracy')
# 合并圖例
lns = lns1 + lns2
labels = ["Loss", "Accuracy"]
# labels = [l.get_label() for l in lns]
plt.legend(lns, labels, loc=7)

以上這篇TensorFlow繪制loss/accuracy曲線的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python基于tkinter模塊實(shí)現(xiàn)的改名小工具示例

    Python基于tkinter模塊實(shí)現(xiàn)的改名小工具示例

    這篇文章主要介紹了Python基于tkinter模塊實(shí)現(xiàn)的改名小工具,結(jié)合實(shí)例形式分析了tkinter模塊操作文件后綴名的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • python3利用venv配置虛擬環(huán)境及過程中的小問題小結(jié)

    python3利用venv配置虛擬環(huán)境及過程中的小問題小結(jié)

    這篇文章主要介紹了python3利用venv配置虛擬環(huán)境及過程中的小問題小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • 使用Python實(shí)現(xiàn)兩組數(shù)據(jù)縱向排序

    使用Python實(shí)現(xiàn)兩組數(shù)據(jù)縱向排序

    在數(shù)據(jù)處理和分析中,排序是一項(xiàng)非?;A(chǔ)且重要的操作,本文將詳細(xì)介紹如何使用Python對(duì)兩組數(shù)據(jù)進(jìn)行縱向排序,即每一列分別進(jìn)行排序,同時(shí)保持?jǐn)?shù)據(jù)的對(duì)應(yīng)關(guān)系,需要的可以參考下
    2024-12-12
  • Python實(shí)現(xiàn)在數(shù)字中添加千位分隔符的方法小結(jié)

    Python實(shí)現(xiàn)在數(shù)字中添加千位分隔符的方法小結(jié)

    在數(shù)據(jù)處理和數(shù)據(jù)可視化中,經(jīng)常需要對(duì)大數(shù)值進(jìn)行格式化,其中一種常見的需求是在數(shù)字中添加千位分隔符,本文為大家整理了三種常見方法,希望對(duì)大家有所幫助
    2024-01-01
  • Python2及Python3如何實(shí)現(xiàn)兼容切換

    Python2及Python3如何實(shí)現(xiàn)兼容切換

    這篇文章主要介紹了Python2及Python3如何實(shí)現(xiàn)兼容切換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Python圖像處理Pillow庫(kù)的基礎(chǔ)使用

    Python圖像處理Pillow庫(kù)的基礎(chǔ)使用

    Pillow庫(kù)是Python中最流行的圖像處理庫(kù)之一,它是PIL(Python Imaging Library)的一個(gè)分支,提供了豐富的圖像處理功能,使圖像處理變得簡(jiǎn)單而高效,在這篇文章中,我們將探討Pillow庫(kù)的一些基本功能,感興趣的朋友可以參考下
    2023-09-09
  • Python Numpy,mask圖像的生成詳解

    Python Numpy,mask圖像的生成詳解

    今天小編就為大家分享一篇Python Numpy,mask圖像的生成詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Django實(shí)現(xiàn)跨域的2種方法

    Django實(shí)現(xiàn)跨域的2種方法

    這篇文章主要介紹了Django實(shí)現(xiàn)跨域的2中方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python格式化字符串的案例方法

    Python格式化字符串的案例方法

    在編寫程序的過程中,經(jīng)常需要進(jìn)行格式化輸出,每次用每次查,干脆就在這里整理一下,下面這篇文章主要給大家介紹了關(guān)于python字符串格式化的相關(guān)資料,分別是%格式符和format方式,需要的朋友可以參考下
    2022-03-03
  • Python中DJANGO簡(jiǎn)單測(cè)試實(shí)例

    Python中DJANGO簡(jiǎn)單測(cè)試實(shí)例

    這篇文章主要介紹了Python中DJANGO簡(jiǎn)單測(cè)試,實(shí)例分析了DJANGO的用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-05-05

最新評(píng)論

汉沽区| 兴义市| 凯里市| 广元市| 西吉县| 靖安县| 东安县| 曲周县| 将乐县| 双峰县| 美姑县| 康平县| 海门市| 鄂托克前旗| 宣威市| 曲阜市| 黔江区| 景德镇市| 陆川县| 阳朔县| 樟树市| 盐津县| 大安市| 错那县| 莱阳市| 石城县| 二连浩特市| 十堰市| 额敏县| 孟村| 吕梁市| 青川县| 常熟市| 崇阳县| 视频| 伊金霍洛旗| 台州市| 南阳市| 理塘县| 中方县| 南宫市|