MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / Model

Class Model

examples/basics/mnist-visualizations.py:66–114  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64
65
66class Model(ModelDesc):
67 def inputs(self):
68 return [tf.TensorSpec((None, IMAGE_SIZE, IMAGE_SIZE), tf.float32, 'input'),
69 tf.TensorSpec((None,), tf.int32, 'label')]
70
71 def build_graph(self, image, label):
72 image = tf.expand_dims(image * 2 - 1, 3)
73
74 with argscope(Conv2D, kernel_shape=3, nl=tf.nn.relu, out_channel=32):
75 c0 = Conv2D('conv0', image)
76 p0 = MaxPooling('pool0', c0, 2)
77 c1 = Conv2D('conv1', p0)
78 c2 = Conv2D('conv2', c1)
79 p1 = MaxPooling('pool1', c2, 2)
80 c3 = Conv2D('conv3', p1)
81 fc1 = FullyConnected('fc0', c3, 512, nl=tf.nn.relu)
82 fc1 = Dropout('dropout', fc1, 0.5)
83 logits = FullyConnected('fc1', fc1, out_dim=10, nl=tf.identity)
84
85 with tf.name_scope('visualizations'):
86 visualize_conv_weights(c0.variables.W, 'conv0')
87 visualize_conv_activations(c0, 'conv0')
88 visualize_conv_weights(c1.variables.W, 'conv1')
89 visualize_conv_activations(c1, 'conv1')
90 visualize_conv_weights(c2.variables.W, 'conv2')
91 visualize_conv_activations(c2, 'conv2')
92 visualize_conv_weights(c3.variables.W, 'conv3')
93 visualize_conv_activations(c3, 'conv3')
94
95 tf.summary.image('input', (image + 1.0) * 128., 3)
96
97 cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label)
98 cost = tf.reduce_mean(cost, name='cross_entropy_loss')
99
100 tf.reduce_mean(tf.cast(tf.nn.in_top_k(logits, label, 1), tf.float32), name='accuracy')
101
102 wd_cost = tf.multiply(1e-5,
103 regularize_cost('fc.*/W', tf.nn.l2_loss),
104 name='regularize_loss')
105 return tf.add_n([wd_cost, cost], name='total_cost')
106
107 def optimizer(self):
108 lr = tf.train.exponential_decay(
109 learning_rate=1e-3,
110 global_step=get_global_step_var(),
111 decay_steps=468 * 10,
112 decay_rate=0.3, staircase=True, name='learning_rate')
113 tf.summary.scalar('lr', lr)
114 return tf.train.AdamOptimizer(lr)
115
116
117def get_data():

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…