一种非常简单的分类器NN,具有一个2D输入节点和一个输出节点,可以对分别标记为0和1的两个点(1,0)和(1,1)进行分类。分类器模型的形式为w1 * x1 + w2 * x2 + b = 0 仅一步步训练网络,并且通过张量板工具绘制b参数的直方图。直方图看起来像是[1.97,2.16]范围内的均匀分布。为什么b的直方图具有这种形式?
import tensorflow as tf
import numpy as np
'''
generate synthesis dataset
'''
N = 2
x_np = np.array([[1,0],[1,1]]).astype('float32')
y_np = np.array([0,1]).astype('float32')
'''
generate tensorflow graph for the classifier model
'''
with tf.name_scope("placeholders"):
x = tf.placeholder(tf.float32, (N,2))
y = tf.placeholder(tf.float32, (N,))
with tf.name_scope("weights"):
W = tf.Variable([[3.],[5.]])
b = tf.Variable([2.])
with tf.name_scope("prediction"):
y_logit = tf.squeeze(tf.matmul(x,W)+b)
# define the loss function
with tf.name_scope("loss"):
#compute the cross-entropy term for each datapoint
entropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=y_logit, labels=y)
#sum all contributions
l = tf.reduce_sum(entropy)
# add training op
with tf.name_scope("optim"):
train_op = tf.train.GradientDescentOptimizer(.1).minimize(l)
'''
for debugging
'''
with tf.name_scope("summaries"):
tf.summary.histogram("b", b)
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter("/tmp/classifier-train", tf.get_default_graph())
'''
train the classifier model
'''
n_steps = 1
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
train_writer.add_graph(sess.graph)
#train the model
for i in range(n_steps):
feed_dict = {x:x_np, y:y_np}
_,summary,b_val = sess.run([train_op, merged, b], feed_dict=feed_dict)
train_writer.add_summary(summary,i)