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

Class Model

examples/basics/mnist-convnet.py:18–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16
17
18class Model(ModelDesc):
19 # See tutorial at https://tensorpack.readthedocs.io/tutorial/training-interface.html#with-modeldesc-and-trainconfig
20 def inputs(self):
21 """
22 Define all the inputs (with type, shape, name) that the graph will need.
23 """
24 return [tf.TensorSpec((None, IMAGE_SIZE, IMAGE_SIZE), tf.float32, 'input'),
25 tf.TensorSpec((None,), tf.int32, 'label')]
26
27 def build_graph(self, image, label):
28 """This function should build the model which takes the input variables (defined above)
29 and return cost at the end."""
30
31 # In tensorflow, inputs to convolution function are assumed to be
32 # NHWC. Add a single channel here.
33 image = tf.expand_dims(image, 3)
34
35 image = image * 2 - 1 # center the pixels values at zero
36 # The context manager `argscope` sets the default option for all the layers under
37 # this context. Here we use 32 channel convolution with shape 3x3
38 # See tutorial at https://tensorpack.readthedocs.io/tutorial/symbolic.html
39 with argscope(Conv2D, kernel_size=3, activation=tf.nn.relu, filters=32):
40 # LinearWrap is just a syntax sugar.
41 # See tutorial at https://tensorpack.readthedocs.io/tutorial/symbolic.html
42 logits = (LinearWrap(image)
43 .Conv2D('conv0')
44 .MaxPooling('pool0', 2)
45 .Conv2D('conv1')
46 .Conv2D('conv2')
47 .MaxPooling('pool1', 2)
48 .Conv2D('conv3')
49 .FullyConnected('fc0', 512, activation=tf.nn.relu)
50 .Dropout('dropout', rate=0.5)
51 .FullyConnected('fc1', 10, activation=tf.identity)())
52
53 # a vector of length B with loss of each sample
54 cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label)
55 cost = tf.reduce_mean(cost, name='cross_entropy_loss') # the average cross-entropy loss
56
57 correct = tf.cast(tf.nn.in_top_k(predictions=logits, targets=label, k=1), tf.float32, name='correct')
58 accuracy = tf.reduce_mean(correct, name='accuracy')
59
60 # This will monitor training error & accuracy (in a moving average fashion). The value will be automatically
61 # 1. written to tensosrboard
62 # 2. written to stat.json
63 # 3. printed after each epoch
64 # You can also just call `tf.summary.scalar`. But moving summary has some other benefits.
65 # See tutorial at https://tensorpack.readthedocs.io/tutorial/summary.html
66 train_error = tf.reduce_mean(1 - correct, name='train_error')
67 summary.add_moving_summary(train_error, accuracy)
68
69 # Use a regex to find parameters to apply weight decay.
70 # Here we apply a weight decay on all W (weight matrix) of all fc layers
71 # If you don't like regex, you can certainly define the cost in any other methods.
72 wd_cost = tf.multiply(1e-5,
73 regularize_cost('fc.*/W', tf.nn.l2_loss),
74 name='regularize_loss')
75 total_cost = tf.add_n([wd_cost, cost], name='total_cost')

Callers 1

mnist-convnet.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…