python學習筆記之tensorboard

2021-08-18 20:07:12 字數 2333 閱讀 2813

# coding: utf-8

# in[2]:

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

# in[3]:

# 載入資料集

mnist = input_data.read_data_sets("mnist_data"

, one_hot=true)

# 每個批次的大小

batch_size = 100

# 計算一共有多少個批次

n_batch = mnist.train.num_examples // batch_size

#定義乙個命名空間

with tf.name_scope('input'):

x = tf.placeholder(tf.float32, [none,

784],

name='x_input')

y = tf.placeholder(tf.float32, [none,

10],

name='y_input')

with tf.name_scope("layer"):

with tf.name_scope('weights'):

w = tf.variable(tf.zeros([784

, 10]))

with tf.name_scope('biases'):

b = tf.variable(tf.zeros([10]))

with tf.name_scope('wx_plus_b'):

wx_plus_b=tf.matmul(x, w) + b

# 建立乙個簡單的神經網路

with tf.name_scope('prediction'):

prediction = tf.nn.softmax(wx_plus_b)

with tf.name_scope('loss'):

# 二次代價函式

loss = tf.reduce_mean(tf.square(y - prediction))

with tf.name_scope("train"):

# 使用梯度下降法

train_step = tf.train.gradientdescentoptimizer(0.2).minimize(loss)

# 初始化變數

init = tf.global_variables_initializer()

with tf.name_scope('accuracy'):

with tf.name_scope("correct_prediction"):

# 結果存放在乙個布林型列表中

correct_prediction = tf.equal(tf.argmax(y,

1), tf.argmax(prediction,

1)) # argmax返回一維張量中最大的值所在的位置

# 求準確率

with tf.name_scope("accuracy"):

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.session() as sess:

sess.run(init)

writer=tf.summary.filewriter('logs/'

,sess.graph)

for epoch in

range(1):

for batch in

range(n_batch):

batch_xs, batch_ys = mnist.train.next_batch(batch_size)

sess.run(train_step,

feed_dict=)

acc = sess.run(accuracy,

feed_dict=)

print("iter " + str(epoch) + ",testing accuracy " + str(acc))

# in[ ]:

我的tensorboard生成資料夾為:e:\py3.6_proje\tensorflow\logs

開啟cmd  

1:輸入 e:    按enter

2:輸入tensorboard --logdir=e:\py3.6_proje\tensorflow\logs

3:將cmd生成的本地連線開啟,複製到google瀏覽器中,即可開啟tensorboard模型

Python學習筆記之IF

關係表示式 邏輯表示式 and,or,not 單分支 num 0 if num 1 print num 大於 1 else print num 不大於1 雙分支 num 10 if num 1 print num 大於 10 elif num 10 print num 等於 10 elif num ...

Python學習筆記 Python之函式

1.函式引數函式定義的時候自己定義的引數,稱為形參 函式呼叫時候,其引數為實參,即實際要傳遞的引數 舉例 def pname username username 形參 print username pname python 傳遞了乙個實參 args是接受所有未命名的引數 關鍵字 是乙個元組型別 ag...

Python學習筆記 Python之函式

1.函式引數函式定義時,自己定義的引數,稱為形參 函式呼叫時,其引數為實參,即實際要傳遞的引數 舉例 def pname username username 形參 print username pname python 傳遞了乙個實參 args是接受所有未命名的引數 關鍵字 是乙個元組型別 agrs...