深度学习算法--python实现用tensorflow构建的输入数据有多个来源的分类器+TensorBoard可视化图

假设来自于源A的数据通过占位符馈送,并且源B是数据生成器网络的输出。通过调用build_generator函数在generator域建立生成器网络,然后调用build_classifier在classifier域添加分类器:

import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

###########################
##   Helper functions    ##
###########################

def build_classifier(data, labels, n_classes=2):
    data_shape = data.get_shape().as_list()
    weights = tf.get_variable(name = 'weights',
                              shape=(data_shape[1],
                                     n_classes),
                              dtype=tf.float32)
    bias = tf.get_variable(name='bias',
                           initializer=tf.zeros(
                               shape=n_classes))
    print(weights)
    print(bias)
    logits = tf.add(tf.matmul(data, weights),
                    bias,
                    name='logits')
    print(logits)
    return logits, tf.nn.softmax(logits)


def build_generator(data, n_hidden):
    data_shape = data.get_shape().as_list()
    w1 = tf.Variable(
        tf.random_normal(shape=(data_shape[1],
                                n_hidden)),
        name='w1')
    b1 = tf.Variable(tf.zeros(shape=n_hidden),
                     name='b1')
    hidden = tf.add(tf.matmul(data, w1), b1,
                    name='hidden_pre-activation')
    hidden = tf.nn.relu(hidden, 'hidden_activation')

    w2 = tf.Variable(
        tf.random_normal(shape=(n_hidden,
                                data_shape[1])),
        name='w2')
    b2 = tf.Variable(tf.zeros(shape=data_shape[1]),
                     name='b2')
    output = tf.add(tf.matmul(hidden, w2), b2,
                    name = 'output')
    return output, tf.nn.sigmoid(output)


###########################
##  Building the graph   ##
###########################

batch_size =64
g = tf.Graph()

with g.as_default():
    tf_X = tf.placeholder(shape=(batch_size, 100),
                          dtype=tf.float32,
                          name='tf_X')

    ## build the generator
    with tf.variable_scope('generator'):
        gen_out1 = build_generator(data=tf_X,
                                   n_hidden=50)

    ## build the classifier
    with tf.variable_scope('classifier') as scope:
        ## classifier for the original data:
        cls_out1 = build_classifier(data=tf_X,
                                    labels=tf.ones(
                                        shape=batch_size))

        ## reuse the classifier for generated data
        scope.reuse_variables()
        cls_out2 = build_classifier(data=gen_out1[1],
                                    labels=tf.zeros(
                                        shape=batch_size))

# 可视化并导出图
with tf.Session(graph = g) as sess:
    sess.run(tf.global_variables_initializer())

    file_writer = tf.summary.FileWriter(logdir='logs/', graph=g)

运行结果:
<tf.Variable ‘classifier/weights:0’ shape=(100, 2) dtype=float32_ref>
<tf.Variable ‘classifier/bias:0’ shape=(2,) dtype=float32_ref>
Tensor(“classifier/logits:0”, shape=(64, 2), dtype=float32)
<tf.Variable ‘classifier/weights:0’ shape=(100, 2) dtype=float32_ref>
<tf.Variable ‘classifier/bias:0’ shape=(2,) dtype=float32_ref>
Tensor(“classifier/logits_1:0”, shape=(64, 2), dtype=float32)

<tf.Variable ‘classifier/weights:0’ shape=(100, 2) dtype=float32_ref>
<tf.Variable ‘classifier/bias:0’ shape=(2,) dtype=float32_ref>
Tensor(“classifier/logits:0”, shape=(64, 2), dtype=float32)
<tf.Variable ‘classifier/weights:0’ shape=(100, 2) dtype=float32_ref>
<tf.Variable ‘classifier/bias:0’ shape=(2,) dtype=float32_ref>
Tensor(“classifier/logits_1:0”, shape=(64, 2), dtype=float32)

另外,TensorBoard可视化图
TensorBoard是TensorFlow一个非常好的功能,它负责可视化和模型学习。可视化允许我们看到节点之间的连接,探索它们之间的依赖关系,并在需要时进行模型调试。

前面代码最后的:

file_writer = tf.summary.FileWriter(logdir='logs/', graph=g)

将会创建新目录:logs/,想要可视化只需要在cmd上面当前代码路径下运行下述命令:

tensorboard --logdir logs/

该命令将显示一个URL地址。把地址拷贝到浏览器的地址栏来启动TensorBoard。将会看到与模型相对应的图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值