5 2Tensorboard網路結構

2021-09-26 03:49:11 字數 2105 閱讀 4687

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

# 載入資料集

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'):

#定義兩個placeholder

x=tf.placeholder(tf.float32,[none,784],name='x-input')   #我認為可以理解成給x起個別名

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]),name='w')

with tf.name_scope('biases'):

b=tf.variable(tf.zeros([10]),name='b')

with tf.name_scope('wx_plus_b'):

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

with tf.name_scope('softmax'):

prediction=tf.nn.softmax(wx_plus_b)

# #定義二次代價函式

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

with tf.name_scope('loss'):

loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=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_prediciton'):

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

correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #返回一維張量中最大的值所在的位置

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))

將tensorboard.exe所在路徑加入環境變數的path系統變數中,開啟命令列輸入cmd,輸入cd  生成檔案所在碟符,執行tensorboard --logdir=生成檔案所在碟符的完整路徑,得到乙個位址,在谷歌瀏覽器上輸入該位址。

TensorBoard網路執行

import tensorflow as tf from tensorflow.examples.tutorials.mnist import input data 載入資料集 mnist input data.read data sets mnist data one hot true 每個批次的...

tensorboard網路結構

import tensorflow as tf import numpy as np out tf.layers.conv2d x,filter nums,filter size,use bias false,kernel initializer tf.truncated normal initia...

52 網路交友

在網路社交的過程中,通過朋友,也能認識新的朋友。在某個朋友關係圖中,假定 a 和 b 是朋友,b 和 c 是朋友,那麼 a 和 c 也會成為朋友。即,我們規定朋友的朋友也是朋友。現在要求你每當有一對新的朋友認識的時候,你需要計算兩人的朋友圈合併以後的大小。第一行 乙個整數 n n 500 0 表示有...