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

Method embed

examples/SimilarityLearning/mnist-embeddings.py:200–224  ·  view source on GitHub ↗

Embed all given tensors into an nfeatures-dim space.

(self, x, nfeatures=2)

Source from the content-addressed store, hash-verified

198
199class EmbeddingModel(ModelDesc):
200 def embed(self, x, nfeatures=2):
201 """Embed all given tensors into an nfeatures-dim space. """
202 list_split = 0
203 if isinstance(x, list):
204 list_split = len(x)
205 x = tf.concat(x, 0)
206
207 # pre-process MNIST dataflow data
208 x = tf.expand_dims(x, 3)
209 x = x * 2 - 1
210
211 # the embedding network
212 net = slim.layers.conv2d(x, 20, 5, scope='conv1')
213 net = slim.layers.max_pool2d(net, 2, scope='pool1')
214 net = slim.layers.conv2d(net, 50, 5, scope='conv2')
215 net = slim.layers.max_pool2d(net, 2, scope='pool2')
216 net = slim.layers.flatten(net, scope='flatten3')
217 net = slim.layers.fully_connected(net, 500, scope='fully_connected4')
218 embeddings = slim.layers.fully_connected(net, nfeatures, activation_fn=None, scope='fully_connected5')
219
220 # if "x" was a list of tensors, then split the embeddings
221 if list_split > 0:
222 embeddings = tf.split(embeddings, list_split, 0)
223
224 return embeddings
225
226 def optimizer(self):
227 lr = tf.get_variable('learning_rate', initializer=1e-4, trainable=False)

Callers 7

_injectMethod · 0.80
_batch_numpyMethod · 0.80
build_graphMethod · 0.80
build_graphMethod · 0.80
build_graphMethod · 0.80
build_graphMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected