MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / Model

Class Model

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

Source from the content-addressed store, hash-verified

21
22
23class Model(ModelDesc):
24 def inputs(self):
25 """
26 Define all the inputs (with type, shape, name) that the graph will need.
27 """
28 return [tf.TensorSpec((None, IMAGE_SIZE, IMAGE_SIZE), tf.float32, 'input'),
29 tf.TensorSpec((None,), tf.int32, 'label')]
30
31 def build_graph(self, image, label):
32 """This function should build the model which takes the input variables
33 and return cost at the end"""
34
35 # In tensorflow, inputs to convolution function are assumed to be
36 # NHWC. Add a single channel here.
37 image = tf.expand_dims(image, 3)
38
39 image = image * 2 - 1 # center the pixels values at zero
40
41 # The context manager `argscope` sets the default option for all the layers under
42 # this context. Here we use 32 channel convolution with shape 3x3
43 with argscope([tf.layers.conv2d], padding='same', activation=tf.nn.relu):
44 l = tf.layers.conv2d(image, 32, 3, name='conv0')
45 l = tf.layers.max_pooling2d(l, 2, 2, padding='valid')
46 l = tf.layers.conv2d(l, 32, 3, name='conv1')
47 l = tf.layers.conv2d(l, 32, 3, name='conv2')
48 l = tf.layers.max_pooling2d(l, 2, 2, padding='valid')
49 l = tf.layers.conv2d(l, 32, 3, name='conv3')
50 l = tf.layers.flatten(l)
51 l = tf.layers.dense(l, 512, activation=tf.nn.relu, name='fc0')
52 l = tf.layers.dropout(l, rate=0.5, training=self.training)
53 logits = tf.layers.dense(l, 10, activation=tf.identity, name='fc1')
54
55 # a vector of length B with loss of each sample
56 cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label)
57 cost = tf.reduce_mean(cost, name='cross_entropy_loss') # the average cross-entropy loss
58
59 correct = tf.cast(tf.nn.in_top_k(logits, label, 1), tf.float32, name='correct')
60 accuracy = tf.reduce_mean(correct, name='accuracy')
61
62 # This will monitor training error & accuracy (in a moving average fashion). The value will be automatically
63 # 1. written to tensosrboard
64 # 2. written to stat.json
65 # 3. printed after each epoch
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.*/kernel', tf.nn.l2_loss),
74 name='regularize_loss')
75 total_cost = tf.add_n([wd_cost, cost], name='total_cost')
76 summary.add_moving_summary(cost, wd_cost, total_cost)
77
78 # monitor histogram of all weight (of conv and fc layers) in tensorboard
79 summary.add_param_summary(('.*/kernel', ['histogram', 'rms']))
80 # the function should return the total cost to be optimized

Callers 1

mnist-tflayers.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…