假设来自于源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。将会看到与模型相对应的图