MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / tpu_model

Function tpu_model

tensorflow/contrib/tpu/python/tpu/keras_support.py:2188–2259  ·  view source on GitHub ↗

Copy `model` along with weights to the TPU. Returns a TPU model. Usage: ``` a = Input(shape=(32,)) b = Dense(32)(a) model = Model(inputs=a, outputs=b) # If `num_cores_per_host` is greater than one, batch parallelism will be used # to run on multiple TPU cores. strategy = keras_s

(model, strategy=None)

Source from the content-addressed store, hash-verified

2186 'https://www.tensorflow.org/api_docs/python/tf/contrib/distribute/DistributionStrategy'
2187)
2188def tpu_model(model, strategy=None):
2189 """Copy `model` along with weights to the TPU.
2190
2191 Returns a TPU model.
2192
2193 Usage:
2194 ```
2195 a = Input(shape=(32,))
2196 b = Dense(32)(a)
2197 model = Model(inputs=a, outputs=b)
2198
2199 # If `num_cores_per_host` is greater than one, batch parallelism will be used
2200 # to run on multiple TPU cores.
2201 strategy = keras_support.TPUDistributionStrategy(tpu_cluster_resolver)
2202 model = keras_support.tpu_model(model, strategy)
2203 model.compile(
2204 optimizer=tf.compat.v1.train.GradientDescentOptimizer(learning_rate=1.0),
2205 ...)
2206 ```
2207
2208 Args:
2209 model: A `tf.keras.Model` instance.
2210 strategy: `TPUDistributionStrategy`. The strategy to use for replicating
2211 model across multiple TPU cores.
2212
2213 Returns:
2214 A new `KerasTPUModel` instance.
2215 """
2216 _validate_shapes(model)
2217 # TODO(xiejw): Validate TPU model. TPUModel only?
2218 # TODO(xiejw): Validate replicas. Full or 1. Shall we allow subset?
2219 # TODO(xiejw): Adds reduction option.
2220
2221 if strategy is None:
2222 strategy = TPUDistributionStrategy()
2223 else:
2224 if not isinstance(strategy, TPUDistributionStrategy):
2225 raise TypeError(
2226 '`strategy` must have type `tf.contrib.tpu.TPUDistributionStrategy`. '
2227 'Got: {}'.format(type(strategy)))
2228
2229 # If the model has already been initialized, grab the optimizer configuration
2230 # and model weights before entering the TPU session.
2231 if model.optimizer:
2232 if (isinstance(model.optimizer, keras_optimizers.Optimizer) and not
2233 isinstance(model.optimizer, keras_optimizers.TFOptimizer)):
2234 optimizer_config = model.optimizer.get_config()
2235 else:
2236 optimizer_config = None
2237 model_weights = model.get_weights()
2238 else:
2239 model_weights = None
2240
2241 setup_tpu_session(strategy._tpu_cluster_resolver)
2242
2243 # Force initialization of the CPU model in the TPU session.
2244 cpu_model = models.clone_model(model)
2245 if model.optimizer:

Callers

nothing calls this directly

Calls 12

_validate_shapesFunction · 0.85
typeFunction · 0.85
setup_tpu_sessionFunction · 0.85
_clone_optimizerFunction · 0.85
KerasTPUModelClass · 0.85
formatMethod · 0.45
get_configMethod · 0.45
get_weightsMethod · 0.45
compileMethod · 0.45
set_weightsMethod · 0.45
reset_statesMethod · 0.45

Tested by

no test coverage detected