在程序中实现网络可视化,只需要在加载完网络上之后,加上:

summary_writer=tf.summary.FileWriter("./log/",sess.graph)

        上边的sess.graph就是定义的网络结构,使用summary.FileWriter方法保存到本地:

summary_writer=tf.summary.FileWriter("./log/",tf.get_default_graph())

eg:

# -*- coding: utf-8 -*-
import tensorflow as tf
 
 
# 图像大小
IMAGE_HEIGHT = 256
IMAGE_WIDTH = 256
MAX_CAPTCHA = 4
CHAR_SET_LEN = 10
 
input = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT , IMAGE_WIDTH, 1])
 
# 定义CNN
def crack_captcha_cnn(x=input, w_alpha=0.01, b_alpha=0.1):
    # conv layer
    w_c1 = tf.Variable(w_alpha * tf.random_normal([3, 3, 1, 32]))
    b_c1 = tf.Variable(b_alpha * tf.random_normal([32]))
    conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w_c1, strides=[1, 1, 1, 1], padding='SAME'), b_c1))
    conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
 
    # Fully connected layer
    w_d = tf.Variable(w_alpha * tf.random_normal([8 * 20 * 64, 1024]))
    b_d = tf.Variable(b_alpha * tf.random_normal([1024]))
    dense = tf.reshape(conv1, [-1, w_d.get_shape().as_list()[0]])
    dense = tf.nn.relu(tf.add(tf.matmul(dense, w_d), b_d))
 
    w_out = tf.Variable(w_alpha * tf.random_normal([1024, MAX_CAPTCHA * CHAR_SET_LEN]))
    b_out = tf.Variable(b_alpha * tf.random_normal([MAX_CAPTCHA * CHAR_SET_LEN]))
    out = tf.add(tf.matmul(dense, w_out), b_out)
    return out
 
# 加载网络
evaluate_net = crack_captcha_cnn()
 
with tf.Session() as sess:
    # 网络结构写入
    summary_writer = tf.summary.FileWriter('./log/', sess.graph)
    # summary_writer = tf.summary.FileWriter('./log/', tf.get_default_graph())
 
print('OK')

        执行完成之后在程序目录下生成log文件夹,保存网络信息,使用tensorboard执行:

tensorboard --logdir=log

        在浏览器输入返回的网址,网络结构如下:

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐