TensorFlow给你的不仅仅是这些,有一个特别棒的工具TensorBoard能够可视化训练过程中的信息,能让人直观的感受,当然需要一些简单的配置: 写入Graph writer = tf.summary.FileWriter("/tmp/mnist_demo/1") writer.add_graph(sess.graph) 这里虽然能够可视化Graph,却感觉很杂乱,我们可以通过给一些node增加name,scope,让图变得更好看点: def conv_layer(input, channels_in, channels_out, name="conv"): with tf.name_scope(name): w = tf.Variable(tf.zeros([5, 5, channels_in, channels_out]), name="W") b = tf.Variable(tf.zeros([channels_out]), name="B") conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding="SAME") act = tf.nn.relu(conv + b) return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")def fc_layer(input, channels_in, channels_out, name="fc"): with tf.name_scope(name): w = tf.Variable(tf.zeros([channels_in, channels_out]), name="W") b = tf.Variable(tf.zeros([channels_out]), name="B") return tf.nn.relu(tf.matmul(input, w) + b)# Setup placeholders, and reshape the data x = tf.placeholder(tf.float32, shape=[None, 784], name="x") x_image = tf.reshape(x, [-1, 28, 28, 1]) y = tf.placeholder(tf.float32, shape=[None, 10], name="labels")conv1 = conv_layer(x_image, 1, 32, "conv1") conv2 = conv_layer(conv1, 32, 64, "conv2")flattened = tf.reshape(conv2, [-1, 7 * 7 * 64]) fc1 = fc_layer(flattened, 7 * 7 * 64, 1024, "fc1") logits = fc_layer(fc1, 1024, 10, "fc2")with tf.name_scope("xent"): xent = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))with tf.name_scope("train"): train_step = tf.train.AdamOptimizer(1e-4).minimize(xent)with tf.name_scope("accuracy"): correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))writer = tf.summary.FileWriter("/tmp/mnist_demo/2") writer.add_graph(sess.graph) 通过TensorFlow的api,收集更多的数据记录显示在TensorBoard中: tf.summary.scalar('cross_entropy', xent) tf.summary.scalar('accuracy', accuracy)tf.summary.image('input', x_image, 3) 修改Conv的代码,将Weight,bias,act加入到histogram中: def conv_layer(input, channels_in, channels_out, name="conv"): with tf.name_scope(name): w = tf.Variable(tf.zeros([5, 5, channels_in, channels_out]), name="W") b = tf.Variable(tf.zeros([channels_out]), name="B") conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding="SAME") act = tf.nn.relu(conv + b) tf.summary.histogram("weights", w) tf.summary.histogram("biases", b) tf.summary.histogram("activations", act) return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME") 配置将训练过程中的数据写入: (责任编辑:本港台直播) |