欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TensorFlow實(shí)現(xiàn)模型評(píng)估

 更新時(shí)間:2018年09月07日 14:26:26   作者:lilongsy  
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)模型評(píng)估,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

我們需要評(píng)估模型預(yù)測(cè)值來(lái)評(píng)估訓(xùn)練的好壞。

模型評(píng)估是非常重要的,隨后的每個(gè)模型都有模型評(píng)估方式。使用TensorFlow時(shí),需要把模型評(píng)估加入到計(jì)算圖中,然后在模型訓(xùn)練完后調(diào)用模型評(píng)估。

在訓(xùn)練模型過(guò)程中,模型評(píng)估能洞察模型算法,給出提示信息來(lái)調(diào)試、提高或者改變整個(gè)模型。但是在模型訓(xùn)練中并不是總需要模型評(píng)估,我們將展示如何在回歸算法和分類(lèi)算法中使用它。

訓(xùn)練模型之后,需要定量評(píng)估模型的性能如何。在理想情況下,評(píng)估模型需要一個(gè)訓(xùn)練數(shù)據(jù)集和測(cè)試數(shù)據(jù)集,有時(shí)甚至需要一個(gè)驗(yàn)證數(shù)據(jù)集。

想評(píng)估一個(gè)模型時(shí)就得使用大批量數(shù)據(jù)點(diǎn)。如果完成批量訓(xùn)練,我們可以重用模型來(lái)預(yù)測(cè)批量數(shù)據(jù)點(diǎn)。但是如果要完成隨機(jī)訓(xùn)練,就不得不創(chuàng)建單獨(dú)的評(píng)估器來(lái)處理批量數(shù)據(jù)點(diǎn)。

分類(lèi)算法模型基于數(shù)值型輸入預(yù)測(cè)分類(lèi)值,實(shí)際目標(biāo)是1和0的序列。我們需要度量預(yù)測(cè)值與真實(shí)值之間的距離。分類(lèi)算法模型的損失函數(shù)一般不容易解釋模型好壞,所以通常情況是看下準(zhǔn)確預(yù)測(cè)分類(lèi)的結(jié)果的百分比。

不管算法模型預(yù)測(cè)的如何,我們都需要測(cè)試算法模型,這點(diǎn)相當(dāng)重要。在訓(xùn)練數(shù)據(jù)和測(cè)試數(shù)據(jù)上都進(jìn)行模型評(píng)估,以搞清楚模型是否過(guò)擬合。

# TensorFlowm模型評(píng)估
#
# This code will implement two models. The first
# is a simple regression model, we will show how to
# call the loss function, MSE during training, and
# output it after for test and training sets.
#
# The second model will be a simple classification
# model. We will also show how to print percent
# classified correctly during training and after
# for both the test and training sets.

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()

# 創(chuàng)建計(jì)算圖
sess = tf.Session()

# 回歸例子:
# We will create sample data as follows:
# x-data: 100 random samples from a normal ~ N(1, 0.1)
# target: 100 values of the value 10.
# We will fit the model:
# x-data * A = target
# 理論上, A = 10.

# 聲明批量大小
batch_size = 25

# 創(chuàng)建數(shù)據(jù)集
x_vals = np.random.normal(1, 0.1, 100)
y_vals = np.repeat(10., 100)
x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

# 八二分訓(xùn)練/測(cè)試數(shù)據(jù) train/test = 80%/20%
train_indices = np.random.choice(len(x_vals), round(len(x_vals)*0.8), replace=False)
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
x_vals_train = x_vals[train_indices]
x_vals_test = x_vals[test_indices]
y_vals_train = y_vals[train_indices]
y_vals_test = y_vals[test_indices]

# 創(chuàng)建變量 (one model parameter = A)
A = tf.Variable(tf.random_normal(shape=[1,1]))

# 增加操作到計(jì)算圖
my_output = tf.matmul(x_data, A)

# 增加L2損失函數(shù)到計(jì)算圖
loss = tf.reduce_mean(tf.square(my_output - y_target))

# 創(chuàng)建優(yōu)化器
my_opt = tf.train.GradientDescentOptimizer(0.02)
train_step = my_opt.minimize(loss)

# 初始化變量
init = tf.global_variables_initializer()
sess.run(init)

# 迭代運(yùn)行
# 如果在損失函數(shù)中使用的模型輸出結(jié)果經(jīng)過(guò)轉(zhuǎn)換操作,例如,sigmoid_cross_entropy_with_logits()函數(shù),
# 為了精確計(jì)算預(yù)測(cè)結(jié)果,別忘了在模型評(píng)估中也要進(jìn)行轉(zhuǎn)換操作。
for i in range(100):
  rand_index = np.random.choice(len(x_vals_train), size=batch_size)
  rand_x = np.transpose([x_vals_train[rand_index]])
  rand_y = np.transpose([y_vals_train[rand_index]])
  sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
  if (i+1)%25==0:
    print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
    print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))

# 評(píng)估準(zhǔn)確率(loss)
mse_test = sess.run(loss, feed_dict={x_data: np.transpose([x_vals_test]), y_target: np.transpose([y_vals_test])})
mse_train = sess.run(loss, feed_dict={x_data: np.transpose([x_vals_train]), y_target: np.transpose([y_vals_train])})
print('MSE on test:' + str(np.round(mse_test, 2)))
print('MSE on train:' + str(np.round(mse_train, 2)))

# 分類(lèi)算法案例
# We will create sample data as follows:
# x-data: sample 50 random values from a normal = N(-1, 1)
#     + sample 50 random values from a normal = N(1, 1)
# target: 50 values of 0 + 50 values of 1.
#     These are essentially 100 values of the corresponding output index
# We will fit the binary classification model:
# If sigmoid(x+A) < 0.5 -> 0 else 1
# Theoretically, A should be -(mean1 + mean2)/2

# 重置計(jì)算圖
ops.reset_default_graph()

# 加載計(jì)算圖
sess = tf.Session()

# 聲明批量大小
batch_size = 25

# 創(chuàng)建數(shù)據(jù)集
x_vals = np.concatenate((np.random.normal(-1, 1, 50), np.random.normal(2, 1, 50)))
y_vals = np.concatenate((np.repeat(0., 50), np.repeat(1., 50)))
x_data = tf.placeholder(shape=[1, None], dtype=tf.float32)
y_target = tf.placeholder(shape=[1, None], dtype=tf.float32)

# 分割數(shù)據(jù)集 train/test = 80%/20%
train_indices = np.random.choice(len(x_vals), round(len(x_vals)*0.8), replace=False)
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
x_vals_train = x_vals[train_indices]
x_vals_test = x_vals[test_indices]
y_vals_train = y_vals[train_indices]
y_vals_test = y_vals[test_indices]

# 創(chuàng)建變量 (one model parameter = A)
A = tf.Variable(tf.random_normal(mean=10, shape=[1]))

# Add operation to graph
# Want to create the operstion sigmoid(x + A)
# Note, the sigmoid() part is in the loss function
my_output = tf.add(x_data, A)

# 增加分類(lèi)損失函數(shù) (cross entropy)
xentropy = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=my_output, labels=y_target))

# Create Optimizer
my_opt = tf.train.GradientDescentOptimizer(0.05)
train_step = my_opt.minimize(xentropy)

# Initialize variables
init = tf.global_variables_initializer()
sess.run(init)

# 運(yùn)行迭代
for i in range(1800):
  rand_index = np.random.choice(len(x_vals_train), size=batch_size)
  rand_x = [x_vals_train[rand_index]]
  rand_y = [y_vals_train[rand_index]]
  sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
  if (i+1)%200==0:
    print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
    print('Loss = ' + str(sess.run(xentropy, feed_dict={x_data: rand_x, y_target: rand_y})))

# 評(píng)估預(yù)測(cè)
# 用squeeze()函數(shù)封裝預(yù)測(cè)操作,使得預(yù)測(cè)值和目標(biāo)值有相同的維度。
y_prediction = tf.squeeze(tf.round(tf.nn.sigmoid(tf.add(x_data, A))))
# 用equal()函數(shù)檢測(cè)是否相等,
# 把得到的true或false的boolean型張量轉(zhuǎn)化成float32型,
# 再對(duì)其取平均值,得到一個(gè)準(zhǔn)確度值。
correct_prediction = tf.equal(y_prediction, y_target)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
acc_value_test = sess.run(accuracy, feed_dict={x_data: [x_vals_test], y_target: [y_vals_test]})
acc_value_train = sess.run(accuracy, feed_dict={x_data: [x_vals_train], y_target: [y_vals_train]})
print('Accuracy on train set: ' + str(acc_value_train))
print('Accuracy on test set: ' + str(acc_value_test))

# 繪制分類(lèi)結(jié)果
A_result = -sess.run(A)
bins = np.linspace(-5, 5, 50)
plt.hist(x_vals[0:50], bins, alpha=0.5, label='N(-1,1)', color='white')
plt.hist(x_vals[50:100], bins[0:50], alpha=0.5, label='N(2,1)', color='red')
plt.plot((A_result, A_result), (0, 8), 'k--', linewidth=3, label='A = '+ str(np.round(A_result, 2)))
plt.legend(loc='upper right')
plt.title('Binary Classifier, Accuracy=' + str(np.round(acc_value_test, 2)))
plt.show()

輸出:

Step #25 A = [[ 5.79096079]]
Loss = 16.8725
Step #50 A = [[ 8.36085415]]
Loss = 3.60671
Step #75 A = [[ 9.26366138]]
Loss = 1.05438
Step #100 A = [[ 9.58914948]]
Loss = 1.39841
MSE on test:1.04
MSE on train:1.13
Step #200 A = [ 5.83126402]
Loss = 1.9799
Step #400 A = [ 1.64923656]
Loss = 0.678205
Step #600 A = [ 0.12520729]
Loss = 0.218827
Step #800 A = [-0.21780498]
Loss = 0.223919
Step #1000 A = [-0.31613481]
Loss = 0.234474
Step #1200 A = [-0.33259964]
Loss = 0.237227
Step #1400 A = [-0.28847221]
Loss = 0.345202
Step #1600 A = [-0.30949864]
Loss = 0.312794
Step #1800 A = [-0.33211425]
Loss = 0.277342
Accuracy on train set: 0.9625
Accuracy on test set: 1.0

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 打印中文字符的三種方法

    Python 打印中文字符的三種方法

    本文給大家分享三種方法實(shí)現(xiàn)python打印中文字符的方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-08-08
  • python編寫(xiě)樸素貝葉斯用于文本分類(lèi)

    python編寫(xiě)樸素貝葉斯用于文本分類(lèi)

    這篇文章主要為大家詳細(xì)介紹了python編寫(xiě)樸素貝葉斯用于文本分類(lèi),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • python super函數(shù)使用方法詳解

    python super函數(shù)使用方法詳解

    這篇文章主要介紹了python super函數(shù)使用方法詳解,需要的朋友可以參考下
    2020-02-02
  • python中的字典操作及字典函數(shù)

    python中的字典操作及字典函數(shù)

    本篇文章給大家介紹了python中的字典,包括字典的操作,字典函數(shù)實(shí)現(xiàn)代碼,需要的朋友參考下吧
    2018-01-01
  • Python實(shí)現(xiàn)多線程爬表情包詳解

    Python實(shí)現(xiàn)多線程爬表情包詳解

    這篇文章主要介紹了Python多線程爬表情包,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • python開(kāi)發(fā)前景如何

    python開(kāi)發(fā)前景如何

    在本篇文章中小編給大家整理了關(guān)于python開(kāi)發(fā)前景的知識(shí)點(diǎn)及相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2020-06-06
  • Python 自動(dòng)補(bǔ)全(vim)

    Python 自動(dòng)補(bǔ)全(vim)

    Python自動(dòng)補(bǔ)全有vim編輯下和python交互模式下,下面分別介紹如何在這2種情況下實(shí)現(xiàn)Tab鍵自動(dòng)補(bǔ)全
    2014-11-11
  • NumPy內(nèi)存布局的實(shí)現(xiàn)

    NumPy內(nèi)存布局的實(shí)現(xiàn)

    本文主要介紹了NumPy內(nèi)存布局的實(shí)現(xiàn),括連續(xù)內(nèi)存布局(C順序)和分散內(nèi)存布局(Fortran順序),并通過(guò)實(shí)例演示如何操作數(shù)組的內(nèi)存布局,感興趣的可以了解一下
    2024-01-01
  • python使用pandas讀寫(xiě)excel文件的方法實(shí)例

    python使用pandas讀寫(xiě)excel文件的方法實(shí)例

    pandas是一個(gè)十分強(qiáng)大的數(shù)據(jù)處理工具,最近需要處理數(shù)據(jù)并輸入到excel,簡(jiǎn)單列舉它的用法,這篇文章主要給大家介紹了關(guān)于python使用pandas讀寫(xiě)excel文件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • python壓縮和解壓縮模塊之zlib的用法

    python壓縮和解壓縮模塊之zlib的用法

    這篇文章主要介紹了python壓縮和解壓縮模塊之zlib的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評(píng)論