通過python的matplotlib包將Tensorflow數(shù)據(jù)進(jìn)行可視化的方法
使用matplotlib中的一些函數(shù)將tensorflow中的數(shù)據(jù)可視化,更加便于分析
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def add_layer(inputs, in_size, out_size, activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
# Make up some real data
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# important step
#initialize_all_variables已被棄用,使用tf.global_variables_initializer代替。
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# plot the real data
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion() #使plt不會(huì)在show之后停止而是繼續(xù)運(yùn)行
plt.show()
for i in range(1000):
# training
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
# to visualize the result and improvement
try:
ax.lines.remove(lines[0]) #在每一次繪圖之前先講上一次繪圖刪除,使得畫面更加清晰
except Exception:
pass
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
# plot the prediction
lines = ax.plot(x_data, prediction_value, 'r-', lw=5) #'r-'指繪制一個(gè)紅色的線
plt.pause(1) #指等待一秒鐘
運(yùn)行結(jié)果如下:(實(shí)際效果應(yīng)該是動(dòng)態(tài)的,應(yīng)當(dāng)使用ipython運(yùn)行,使用jupyter運(yùn)行則圖片不是動(dòng)態(tài)的)

注意:initialize_all_variables已被棄用,使用tf.global_variables_initializer代替。
以上這篇通過python的matplotlib包將Tensorflow數(shù)據(jù)進(jìn)行可視化的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情
- Python數(shù)據(jù)可視化之使用matplotlib繪制簡單圖表
- Python利用matplotlib模塊數(shù)據(jù)可視化繪制3D圖
- Python 數(shù)據(jù)可視化之Matplotlib詳解
- python數(shù)據(jù)可視化之matplotlib.pyplot基礎(chǔ)以及折線圖
- Python數(shù)據(jù)可視化教程之Matplotlib實(shí)現(xiàn)各種圖表實(shí)例
- Python?matplotlib數(shù)據(jù)可視化圖繪制
相關(guān)文章
python判斷一個(gè)變量是否已經(jīng)設(shè)置的方法
這篇文章主要介紹了python判斷一個(gè)變量是否已經(jīng)設(shè)置的方法,有需要的朋友們可以跟著學(xué)習(xí)參考下。2020-08-08
PyTorch中的C++擴(kuò)展實(shí)現(xiàn)
這篇文章主要介紹了PyTorch中的C++擴(kuò)展實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
使用python爬取微博數(shù)據(jù)打造一顆“心”
這篇文章主要介紹了使用python基于微博數(shù)據(jù)打造一顆“心”,作為程序員,我準(zhǔn)備了一份特別的禮物,用以往發(fā)的微博數(shù)據(jù)打造一顆“愛心”,我想她一定會(huì)感動(dòng)得哭了吧,需要的朋友可以參考下2019-06-06
使用python 的matplotlib 畫軌道實(shí)例
今天小編就為大家分享一篇使用python 的matplotlib 畫軌道實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
詳談Python3 操作系統(tǒng)與路徑 模塊(os / os.path / pathlib)
下面小編就為大家分享一篇詳談Python3 操作系統(tǒng)與路徑 模塊(os / os.path / pathlib),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
基于Django的樂觀鎖與悲觀鎖解決訂單并發(fā)問題詳解
這篇文章主要介紹了基于Django的樂觀鎖與悲觀鎖解決訂單并發(fā)問題詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07

