def _build(self, incoming, *args, **kwargs): """ Args: incoming : A `Tensor`. The incoming tensor. """ inference = incoming def apply_dropout(): if isinstance(self.keep_prob, float): _keep_prob = tf.get_variable( name='keep_prob', shape=[], initializer=tf.constant_initializer(self.keep_prob), trainable=False) tf.add_to_collection(tf.GraphKeys.DROPOUTS, _keep_prob) if type(inference) in [list, np.array]: for x in inference: tf.nn.dropout(x=x, keep_prob=_keep_prob, noise_shape=self.noise_shape, seed=self.seed) return inference else: return tf.nn.dropout(x=inference, keep_prob=_keep_prob, noise_shape=self.noise_shape, seed=self.seed) if Modes.is_train(self.mode): inference = apply_dropout() track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def get_summary(stype, name, value=None, collect=True, **kwargs): """Creates or retrieves a summary. It keep tracks of all graph summaries through SUMMARIES_BY_NAMES collection. If a summary tags already exists, it will return that summary tensor. Args: stype: `str`. Summary type: 'histogram', 'scalar' or 'image'. name: `str`. The summary tag (name). value: `Tensor`. The summary initialization value. Default: None. collect: `boolean`. Adds the summary to the summaries collection. Returns: The summary `Tensor`. """ summary = _summary_for_name(name) if summary is None: summary = SummaryTypes.summarize(stype, name, value, **kwargs) track({name: summary}, collection=tf.GraphKeys.SUMMARIES_BY_NAMES) if collect: track(summary, collection=tf.GraphKeys.TRAIN_SUMMARIES) return summary
def _build(self, features, labels=None, params=None, config=None): # Pre-process features and labels features, labels = self._preprocess(features, labels) results = self._call_graph_fn(inputs=features) if not isinstance(results, BridgeSpec): raise ValueError('`bridge_fn` should return a BridgeSpec.') loss = None train_op = None eval_metrics = None if Modes.is_infer(self.mode): predictions = self._build_predictions( results=results.results, features=features, labels=labels) else: losses, loss = self._build_loss(results, features, features) eval_metrics = self._build_eval_metrics(results.results, features, features) if Modes.is_train(self.mode): train_op = self._build_train_op(loss) self._build_summary_op(results=results.results, features=features, labels=labels) predictions = self._build_predictions( results=results.results, features=features, labels=labels) # We add 'useful' tensors to the graph collection so that we # can easly find them in our hooks/monitors. track(predictions, tf.GraphKeys.PREDICTIONS) return EstimatorSpec(mode=self.mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=eval_metrics)
def _build(self, incoming, *args, **kwargs): """ Args: 2-D Tensor [samples, ids]. Returns: 3-D Tensor [samples, embedded_ids, features]. """ input_shape = get_shape(incoming) assert len(input_shape) == 2, 'Incoming Tensor shape must be 2-D' weights_init = getters.get_initializer(self.weights_init) self._w = variable('w', shape=[self.input_dim, self.output_dim], initializer=weights_init, trainable=self.trainable, restore=self.restore) track(self._w, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = tf.cast(x=incoming, dtype=tf.int32) inference = tf.nn.embedding_lookup( params=self._w, ids=inference, validate_indices=self.validate_indices) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def decay(exploration_rate=0.15, decay_type='polynomial_decay', start_decay_at=0, stop_decay_at=1e9, decay_rate=0., staircase=False, decay_steps=100000, min_exploration_rate=0): """Builds a decaying exploration. Decay epsilon based on number of states and the decay_type. Args: exploration_rate: `float` or `list` of `float`. The initial value of the exploration rate. decay_type: A decay function name defined in `exploration_decay` possible Values: exponential_decay, inverse_time_decay, natural_exp_decay, piecewise_constant, polynomial_decay. start_decay_at: `int`. When to start the decay. stop_decay_at: `int`. When to stop the decay. decay_rate: A Python number. The decay rate. staircase: Whether to apply decay in a discrete staircase, as opposed to continuous, fashion. decay_steps: How often to apply decay. min_exploration_rate: `float`. Don't decay below this number. Returns: `function` the exploration logic operation. """ exploration_rate = _decay_fn(timestep=get_global_timestep(), exploration_rate=exploration_rate, decay_type=decay_type, start_decay_at=start_decay_at, stop_decay_at=stop_decay_at, decay_rate=decay_rate, staircase=staircase, decay_steps=decay_steps, min_exploration_rate=min_exploration_rate) track(exploration_rate, tf.GraphKeys.EXPLORATION_RATE) return exploration_rate
def _build(self, features, labels, params=None, config=None): """Build the different operation of the model.""" # Pre-process features and labels features, labels = self._preprocess(features, labels) results = self._call_graph_fn(features=features, labels=labels) loss = None train_op = None eval_metrics = None if Modes.is_infer(self.mode): predictions = self._build_predictions(results=results, features=features, labels=labels) extra_ops = self._build_extra_ops(results=results, features=features, labels=labels) else: losses, loss = self._build_loss(results, features, labels) eval_metrics = self._build_eval_metrics(results, features, labels) if Modes.is_train(self.mode): train_op = self._build_train_op(loss) self._build_summary_op(results=results, features=features, labels=labels) predictions = self._build_predictions(results=results, features=features, labels=labels) extra_ops = self._build_extra_ops(results=results, features=features, labels=labels) track(predictions, tf.GraphKeys.PREDICTIONS) return EstimatorSpec(mode=self.mode, predictions=predictions, loss=loss, extra_ops=extra_ops, train_op=train_op, eval_metric_ops=eval_metrics)
def _build(self, incoming, *args, **kwargs): """ Args: 2-D Tensor [samples, ids]. Returns: 3-D Tensor [samples, embedded_ids, features]. """ input_shape = get_shape(incoming) assert len(input_shape) == 2, 'Incoming Tensor shape must be 2-D' weights_init = getters.get_initializer(self.weights_init) self._w = variable('w', shape=[self.input_dim, self.output_dim], initializer=weights_init, trainable=self.trainable, restore=self.restore) track(self._w, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = tf.cast(x=incoming, dtype=tf.int32) inference = tf.nn.embedding_lookup( params=self._w, ids=inference, validate_indices=self.validate_indices) # Embedding doesn't support masking, so we save sequence length prior to the lookup. # Expand dim to 3d. shape = [-1] + inference.get_shape().as_list()[1:3] + [1] inference.seq_length = retrieve_seq_length_op( tf.reshape(incoming, shape)) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def _build(self, features, labels=None, params=None, config=None): # Pre-process features and labels features, labels = self._preprocess(features, labels) results = self._call_graph_fn(features=features, labels=labels) if not isinstance(results, BridgeSpec): raise ValueError('`bridge_fn` should return a BridgeSpec.') loss = None train_op = None eval_metrics = None if Modes.is_infer(self.mode): predictions = self._build_predictions( results=results.results, features=features, labels=labels) else: losses, loss = self._build_loss(results, features, features) eval_metrics = self._build_eval_metrics(results.results, features, features) if Modes.is_train(self.mode): train_op = self._build_train_op(loss) self._build_summary_op(results=results.results, features=features, labels=labels) predictions = self._build_predictions( results=results.results, features=features, labels=labels) track(predictions, tf.GraphKeys.PREDICTIONS) return EstimatorSpec(mode=self.mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=eval_metrics)
def _build(self, # pylint: disable=arguments-differ features, labels, params=None, config=None): """Build the different operation of the model.""" # Pre-process features and labels features, labels = self._preprocess(features, labels) results = self._call_graph_fn(features=features, labels=labels) loss = None train_op = None eval_metrics = None if Modes.is_infer(self.mode): predictions = self._build_predictions(results=results, features=features, labels=labels) extra_ops = self._build_extra_ops(results=results, features=features, labels=labels) else: _, loss = self._build_loss(results, features, labels) eval_metrics = self._build_eval_metrics(results, features, labels) if Modes.is_train(self.mode): train_op = self._build_train_op(loss) self._build_summary_op(results=results, features=features, labels=labels) predictions = self._build_predictions(results=results, features=features, labels=labels) extra_ops = self._build_extra_ops(results=results, features=features, labels=labels) track(predictions, tf.GraphKeys.PREDICTIONS) return EstimatorSpec(mode=self.mode, predictions=predictions, loss=loss, extra_ops=extra_ops, train_op=train_op, eval_metric_ops=eval_metrics)
def _build(self, dependencies, *args, **kwargs): """ Args: dependencies: List of Tensors [_shape_]. Returns: Concatenated Tensors [nb_tensors, _shape_]. """ x = tf.concat(axis=1, values=dependencies) track(x, tf.GraphKeys.LAYER_TENSOR, self.module_name) return x
def _build(self, incoming, *args, **kwargs): """ Args: incoming: List of Tensors [_shape_]. Returns: Concatenated Tensors [nb_tensors, _shape_]. """ x = tf.slice(incoming, begin=self.being, size=self.size) track(x, tf.GraphKeys.LAYER_TENSOR, self.module_name) return x
def built_activation(x, collect): """Builds the metric function. Args: x: activated tensor. collect: whether to collect this metric under the metric collection. """ if collect: track(x, tf.GraphKeys.ACTIVATIONS) return x
def _build(self, incoming, *args, **kwargs): """ Args: 4-D Tensor Layer. Returns: 4-D Tensor Layer. (Same dimension as input). """ incoming = tf.nn.lrn( input=incoming, depth_radius=self.depth_radius, bias=self.bias, alpha=self.alpha, beta=self.beta, name=self.name) track(incoming, tf.GraphKeys.LAYER_TENSOR, self.module_name) return incoming
def _build(self, incoming, *args, **kwargs): """ Args: incoming: List of Tensors. Returns: Merged Tensors. """ self._built_modules = [ module(incoming, *args, **kwargs) for module in self._modules ] if self.merge_mode == self.MergeMode.CONCAT: x = tf.concat(axis=self.axis, values=self._built_modules) elif self.merge_mode == self.MergeMode.ELEMENTWISE_SUM: x = self._built_modules[0] for i in xrange(1, len(self._built_modules)): x = tf.add(x, self._built_modules[i]) elif self.merge_mode == self.MergeMode.ELEMENTWISE_MUL: x = self._built_modules[0] for i in xrange(1, len(self._built_modules)): x = tf.multiply(x, self._built_modules[i]) elif self.merge_mode == self.MergeMode.SUM: x = tf.reduce_sum(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.MEAN: x = tf.reduce_mean(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.PROD: x = tf.reduce_prod(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.MAX: x = tf.reduce_max(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.MIN: x = tf.reduce_min(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.AND: x = tf.reduce_all(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.OR: x = tf.reduce_any(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) else: raise Exception('Unknown merge mode', str(self.merge_mode)) track(x, tf.GraphKeys.LAYER_TENSOR, self.module_name) return x
def _build(self, incoming, *args, **kwargs): """ Args: incoming: A `Tensor`. The incoming tensor. """ inference = incoming if isinstance(inference, list): inference = tf.concat(axis=inference, values=0) inference = tf.cast(x=inference, dtype=tf.float32) inference = tf.reshape(tensor=inference, shape=self.new_shape) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def _build(self, incoming, *args, **kwargs): """ Args: incoming: 1-D tensor Returns: A `Tensor` with the same shape as `x`. """ incoming = tf.convert_to_tensor(value=incoming, name='x') square_sum = tf.reduce_sum(input_tensor=tf.square(x=incoming), axis=[self.dim], keep_dims=True) x_inv_norm = tf.rsqrt(x=tf.maximum(x=square_sum, y=self.epsilon)) incoming = tf.multiply(x=incoming, y=x_inv_norm, name=self.module_name) track(incoming, tf.GraphKeys.LAYER_TENSOR, self.module_name) return incoming
def _build(self, incoming, *args, **kwargs): """ Args: incoming: (2+)-D `Tensor`. Returns: 2-D `Tensor` [batch, flatten_dims]. """ input_shape = get_shape(incoming) assert len(input_shape) > 1, 'Incoming Tensor shape must be at least 2-D' dims = total_tensor_depth(tensor_shape=input_shape) x = tf.reshape(tensor=incoming, shape=[-1, dims]) track(x, tf.GraphKeys.LAYER_TENSOR, self.name) return x
def _build(self, incoming, *args, **kwargs): """ Args: incoming: The Labels Placeholder. Returns: 2-D Tensor, The encoded labels. """ if incoming.dtype != dtypes.int64: incoming = standard_ops.to_int64(incoming) incoming = standard_ops.one_hot(indices=incoming, depth=self.n_classes, on_value=self.on_value, off_value=self.off_value) track(incoming, tf.GraphKeys.LAYER_TENSOR, self.module_name) return incoming
def _build(self, incoming, *args, **kwargs): """ Args: incoming: `Tensor`. 3-D Tensor [samples, timesteps, input dim]. """ self._declare_dependencies() sequence_length = None if self.dynamic: sequence_length = retrieve_seq_length_op(incoming if isinstance( incoming, tf.Tensor) else tf.stack(incoming)) input_shape = get_shape(incoming) inference = incoming # If a tensor given, convert it to a per timestep list if type(inference) not in [list, np.array]: ndim = len(input_shape) assert ndim >= 3, 'Input dim should be at least 3.' axes = [1, 0] + list(range(2, ndim)) inference = tf.transpose(inference, (axes)) inference = tf.unstack(value=inference) if self.dynamic: outputs, state = tf.nn.dynamic_rnn( cell=self._cell, inputs=inference, dtype=tf.float32, initial_state=self.initial_state, sequence_length=sequence_length, scope=self.module_name) else: outputs, state = rnn.static_rnn(cell=self._cell, inputs=inference, dtype=tf.float32, initial_state=self.initial_state, sequence_length=sequence_length, scope=self.module_name) for v in [self._cell.w, self._cell.b]: if hasattr(v, '__len__'): for var in v: track(var, tf.GraphKeys.LAYER_VARIABLES, self.module_name) else: track(v, tf.GraphKeys.LAYER_VARIABLES, self.module_name) track(outputs[-1], tf.GraphKeys.ACTIVATIONS, self.module_name) if self.dynamic: if self.return_seq: o = outputs else: outputs = tf.transpose(tf.stack(outputs), [1, 0, 2]) o = advanced_indexing_op(outputs, sequence_length) else: o = outputs if self.return_seq else outputs[-1] track(o, tf.GraphKeys.LAYER_TENSOR, self.module_name) return (o, state) if self.return_state else o
def _build(self, incoming, *args, **kwargs): """ Args: 4-D Tensor Layer. Returns: 4-D Tensor Layer. (Same dimension as input). """ incoming = tf.nn.lrn(input=incoming, depth_radius=self.depth_radius, bias=self.bias, alpha=self.alpha, beta=self.beta, name=self.name) track(incoming, tf.GraphKeys.LAYER_TENSOR, self.module_name) return incoming
def metric(y_pred, y_true): """ Args: y_pred: `Tensor` y_true: `Tensor` Returns: `Float`. The calculated metric. """ check_metric_data(y_pred, y_true) with get_name_scope(name, scope): x = fct(y_pred, y_true) if collect: track(x, tf.GraphKeys.METRICS) return x
def _build(self, incoming, *args, **kwargs): input_shape = get_shape(incoming) input_ndim = len(input_shape) gamma_init = tf.random_normal_initializer(mean=self.gamma, stddev=self.stddev) self._beta = variable(name='beta', shape=[input_shape[-1]], initializer=tf.constant_initializer(self.beta), trainable=self.trainable, restore=self.restore) self._gamma = variable(name='gamma', shape=[input_shape[-1]], initializer=gamma_init, trainable=self.trainable, restore=self.restore) track(self._beta, tf.GraphKeys.LAYER_VARIABLES, self.module_name) track(self._gamma, tf.GraphKeys.LAYER_VARIABLES, self.module_name) if not self.restore: track(tf.GraphKeys.EXCL_RESTORE_VARIABLES, self._beta) track(tf.GraphKeys.EXCL_RESTORE_VARIABLES, self._gamma) axis = list(xrange(input_ndim - 1)) moving_mean = variable(name='moving_mean', shape=input_shape[-1:], initializer=tf.zeros_initializer(), trainable=False, restore=self.restore) moving_variance = variable(name='moving_variance', shape=input_shape[-1:], initializer=tf.constant_initializer(1.), trainable=False, restore=self.restore) def update_mean_var(): mean, variance = tf.nn.moments(x=incoming, axes=axis) update_moving_mean = moving_averages.assign_moving_average( variable=moving_mean, value=mean, decay=self.decay, zero_debias=False) update_moving_variance = moving_averages.assign_moving_average( variable=moving_variance, value=variance, decay=self.decay, zero_debias=False) with tf.control_dependencies([update_moving_mean, update_moving_variance]): return tf.identity(mean), tf.identity(variance) # Retrieve variable managing training mode if Modes.is_train(self.mode): mean, var = update_mean_var() else: mean, var = moving_mean, moving_variance incoming = tf.nn.batch_normalization(x=incoming, mean=mean, variance=var, offset=self._beta, scale=self._gamma, variance_epsilon=self.epsilon) incoming.set_shape(input_shape) track(incoming, tf.GraphKeys.LAYER_TENSOR, self.module_name) return incoming
def _build(self, incoming, *args, **kwargs): """ Args: incoming: `Tensor`. 3-D Tensor [samples, timesteps, input dim]. """ self._declare_dependencies() sequence_length = kwargs.get('sequence_length') if self.dynamic and sequence_length is None: sequence_length = retrieve_seq_length_op( incoming if isinstance(incoming, tf.Tensor) else tf.stack(incoming)) input_shape = get_shape(incoming) inference = incoming # If a static rnn and tensor given, convert it to a per timestep list if type(inference) not in [list, np.array] and not self.dynamic: ndim = len(input_shape) assert ndim >= 3, 'Input dim should be at least 3.' axes = [1, 0] + list(xrange(2, ndim)) inference = tf.transpose(inference, axes) inference = tf.unstack(value=inference) if self.dynamic: outputs, state = tf.nn.dynamic_rnn( cell=self._cell, inputs=inference, dtype=tf.float32, initial_state=self.initial_state, sequence_length=sequence_length, scope=self.module_name) else: outputs, state = rnn.static_rnn( cell=self._cell, inputs=inference, dtype=tf.float32, initial_state=self.initial_state, sequence_length=sequence_length, scope=self.module_name) for v in [self._cell.w, self._cell.b]: if hasattr(v, '__len__'): for var in v: track(var, tf.GraphKeys.LAYER_VARIABLES, self.module_name) else: track(v, tf.GraphKeys.LAYER_VARIABLES, self.module_name) track(outputs[-1], tf.GraphKeys.ACTIVATIONS, self.module_name) if self.dynamic: if self.return_seq: o = outputs else: o = get_sequence_relevant_output(outputs, sequence_length) else: o = outputs if self.return_seq else outputs[-1] track(o, tf.GraphKeys.LAYER_TENSOR, self.module_name) return (o, state) if self.return_state else o
def _build(self, features, labels, params=None, config=None): """Build the different operation of the model.""" # Pre-process features and labels features, labels = self._preprocess(features, labels) results = self._call_graph_fn(inputs=features) loss = None train_op = None eval_metrics = None if Modes.is_infer(self.mode): predictions = self._build_predictions(results=results, features=features, labels=labels) extra_ops = self._build_extra_ops(results=results, features=features, labels=labels) else: losses, loss = self._build_loss(results, features, labels) eval_metrics = self._build_eval_metrics(results, features, labels) if Modes.is_train(self.mode): train_op = self._build_train_op(loss) self._build_summary_op(results=results, features=features, labels=labels) predictions = self._build_predictions(results=results, features=features, labels=labels) extra_ops = self._build_extra_ops(results=results, features=features, labels=labels) # We add 'useful' tensors to the graph collection so that we # can easily find them in our hooks/monitors. track(predictions, tf.GraphKeys.PREDICTIONS) return EstimatorSpec(mode=self.mode, predictions=predictions, loss=loss, extra_ops=extra_ops, train_op=train_op, eval_metric_ops=eval_metrics)
def random_decay(num_actions=None, decay_type='polynomial_decay', start_decay_at=0, stop_decay_at=1e9, decay_rate=0., staircase=False, decay_steps=10000, min_exploration_rate=0): """Builds a random decaying exploration. Decay a random value based on number of states and the decay_type. Args: num_actions: `int` or None. If discrete num_action must be None. decay_type: A decay function name defined in `exploration_decay` possible Values: exponential_decay, inverse_time_decay, natural_exp_decay, piecewise_constant, polynomial_decay. start_decay_at: `int`. When to start the decay. stop_decay_at: `int`. When to stop the decay. decay_rate: A Python number. The decay rate. staircase: Whether to apply decay in a discrete staircase, as opposed to continuous, fashion. decay_steps: How often to apply decay. min_exploration_rate: `float`. Don't decay below this number. Returns: `function` the exploration logic operation. """ if num_actions is None: exploration_rate = partial(np.random.randn, 1) else: exploration_rate = partial(np.random.randn, num_actions) exploration_rate = _decay_fn(timestep=get_global_timestep(), exploration_rate=exploration_rate, decay_type=decay_type, start_decay_at=start_decay_at, stop_decay_at=stop_decay_at, decay_rate=decay_rate, staircase=staircase, decay_steps=decay_steps, min_exploration_rate=min_exploration_rate) track(exploration_rate, tf.GraphKeys.EXPLORATION_RATE) return exploration_rate
def _build(self, incoming, *args, **kwargs): """ Args: incoming: `Tensor`. 3-D Tensor Layer [samples, timesteps, input dim]. """ assert (self.rnncell_fw.output_size == self.rnncell_bw.output_size), "RNN Cells number of units must match!" input_shape = get_shape(incoming) # TODO: DropoutWrapper inference = incoming # If a tensor given, convert it to a per timestep list if type(inference) not in [list, np.array]: ndim = len(input_shape) assert ndim >= 3, 'Input dim should be at least 3.' axes = [1, 0] + list(xrange(2, ndim)) inference = tf.transpose(inference, (axes,)) inference = tf.unstack(inference) sequence_length = None if self.dynamic: sequence_length = retrieve_seq_length_op( incoming if isinstance(incoming, tf.Tensor) else tf.stack(incoming)) outputs, states_fw, states_bw = tf.nn.bidirectional_dynamic_rnn( cell_fw=self.rnncell_fw, cell_bw=self.rnncell_bw, inputs=inference, initial_state_fw=self.initial_state_fw, initial_state_bw=self.initial_state_bw, sequence_length=sequence_length, dtype=tf.float32) else: outputs, states_fw, states_bw = rnn.static_bidirectional_rnn( cell_fw=self.rnncell_fw, cell_bw=self.rnncell_bw, inputs=inference, initial_state_fw=self.initial_state_fw, initial_state_bw=self.initial_state_bw, dtype=tf.float32) for v in [self.rnncell_fw.w, self.rnncell_fw.b, self.rnncell_bw.w, self.rnncell_bw.b]: if hasattr(v, '__len__'): for var in v: track(var, tf.GraphKeys.LAYER_VARIABLES, self.module_name) else: track(v, tf.GraphKeys.LAYER_VARIABLES, self.module_name) tf.add_to_collection(tf.GraphKeys.ACTIVATIONS, outputs[-1]) if self.dynamic: if self.return_seq: o = outputs else: outputs = tf.transpose(tf.stack(outputs), [1, 0, 2]) o = advanced_indexing_op(outputs, sequence_length) else: o = outputs if self.return_seq else outputs[-1] track(o, tf.GraphKeys.LAYER_TENSOR, self.module_name) return (o, states_fw, states_bw) if self.return_states else o
def _build(self, incoming, *args, **kwargs): """ Args: 2-D Tensor [samples, ids]. Returns: 3-D Tensor [samples, embedded_ids, features]. """ input_shape = get_shape(incoming) assert len(input_shape) == 2, 'Incoming Tensor shape must be 2-D' weights_init = getters.get_initializer(self.weights_init) self._w = variable('w', shape=[self.input_dim, self.output_dim], initializer=weights_init, trainable=self.trainable, restore=self.restore) track(self._w, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = tf.cast(x=incoming, dtype=tf.int32) inference = tf.nn.embedding_lookup(params=self._w, ids=inference, validate_indices=self.validate_indices) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def _build(self, features, labels, params, config): """Subclasses should implement this method. See the `model_fn` documentation in tf.contrib.learn.Estimator class for a more detailed explanation. """ # Pre-process features and labels features, labels = self._preprocess(features, labels) results = self._graph_fn(mode=self.mode, inputs=features['source_ids']) loss = None train_op = None eval_metrics = None if self.mode == ModeKeys.PREDICT: predictions = self._build_predictions(results=results, features=features, labels=labels) else: losses, loss = self._build_loss(results, features, labels) eval_metrics = self._build_eval_metrics(results, features, labels) if self.mode == ModeKeys.TRAIN: train_op = self._build_train_op(loss) self._build_summary_op() predictions = self._build_predictions(results=results, features=features, labels=labels, losses=losses) # We add 'useful' tensors to the graph collection so that we # can easly find them in our hooks/monitors. track(predictions, tf.GraphKeys.PREDICTIONS) return EstimatorSpec(mode=self.mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=eval_metrics)
def _build(self, incoming, *args, **kwargs): """ Args: incoming: List of Tensors. Returns: Merged Tensors. """ self._built_modules = [module(incoming, *args, **kwargs) for module in self._modules] if self.merge_mode == self.MergeMode.CONCAT: x = tf.concat(axis=self.axis, values=self._built_modules) elif self.merge_mode == self.MergeMode.ELEMENTWISE_SUM: x = self._built_modules[0] for i in xrange(1, len(self._built_modules)): x = tf.add(x, self._built_modules[i]) elif self.merge_mode == self.MergeMode.ELEMENTWISE_MUL: x = self._built_modules[0] for i in xrange(1, len(self._built_modules)): x = tf.multiply(x, self._built_modules[i]) elif self.merge_mode == self.MergeMode.SUM: x = tf.reduce_sum(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.MEAN: x = tf.reduce_mean(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.PROD: x = tf.reduce_prod(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.MAX: x = tf.reduce_max(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.MIN: x = tf.reduce_min(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.AND: x = tf.reduce_all(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) elif self.merge_mode == self.MergeMode.OR: x = tf.reduce_any(tf.concat(axis=self.axis, values=self._built_modules), axis=self.axis) else: raise Exception('Unknown merge mode', str(self.merge_mode)) track(x, tf.GraphKeys.LAYER_TENSOR, self.module_name) return x
def _build(self, features, labels=None, params=None, config=None): # Pre-process features and labels features, labels = self._preprocess(features, labels) results = self._call_graph_fn(features=features, labels=labels) if not isinstance(results, BridgeSpec): raise ValueError('`bridge_fn` should return a BridgeSpec.') loss = None train_op = None eval_metrics = None if Modes.is_infer(self.mode): predictions = self._build_predictions(results=results.results, features=features, labels=labels) else: _, loss = self._build_loss(results, features, features) eval_metrics = self._build_eval_metrics(results.results, features, features) if Modes.is_train(self.mode): train_op = self._build_train_op(loss) self._build_summary_op(results=results.results, features=features, labels=labels) predictions = self._build_predictions(results=results.results, features=features, labels=labels) track(predictions, tf.GraphKeys.PREDICTIONS) return EstimatorSpec(mode=self.mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=eval_metrics)
def _build(self, incoming, *args, **kwargs): """ Args: incoming: (2+)-D Tensor [samples, input dim]. If not 2D, input will be flatten. Returns: 2D Tensor [samples, num_units]. """ self._declare_dependencies() input_shape = get_shape(incoming) incoming = validate_dtype(incoming) assert len( input_shape) > 1, 'Incoming Tensor shape must be at least 2-D' n_inputs = total_tensor_depth(tensor_shape=input_shape) regularizer = getters.get_regularizer(self.regularizer, scale=self.scale, collect=True) self._w = variable(name='w', shape=[n_inputs, self.num_units], dtype=incoming.dtype, regularizer=regularizer, initializer=getters.get_initializer( self.weights_init), trainable=self.trainable, restore=self.restore) track(self._w, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = incoming # If input is not 2d, flatten it. if len(input_shape) > 2: inference = tf.reshape(tensor=inference, shape=[-1, n_inputs]) inference = tf.matmul(a=inference, b=self._w) self._b = None if self.bias: self._b = variable(name='b', shape=[self.num_units], dtype=incoming.dtype, initializer=getters.get_initializer( self.bias_init), trainable=self.trainable, restore=self.restore) track(self._b, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = tf.nn.bias_add(value=inference, bias=self._b) if self.activation: inference = getters.get_activation(self.activation, collect=True)(inference) if self._dropout: inference = self._dropout(inference) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def _build(self, incoming, *args, **kwargs): """ Args: incoming: (2+)-D Tensor [samples, input dim]. If not 2D, input will be flatten. Returns: 2D Tensor [samples, num_units]. """ self._declare_dependencies() input_shape = get_shape(incoming) incoming = validate_dtype(incoming) assert len(input_shape) > 1, 'Incoming Tensor shape must be at least 2-D' n_inputs = total_tensor_depth(tensor_shape=input_shape) regularizer = getters.get_regularizer(self.regularizer, scale=self.scale, collect=True) self._w = variable( name='w', shape=[n_inputs, self.num_units], dtype=incoming.dtype, regularizer=regularizer, initializer=getters.get_initializer(self.weights_init), trainable=self.trainable, restore=self.restore) track(self._w, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = incoming # If input is not 2d, flatten it. if len(input_shape) > 2: inference = tf.reshape(tensor=inference, shape=[-1, n_inputs]) inference = tf.matmul(a=inference, b=self._w) self._b = None if self.bias: self._b = variable(name='b', shape=[self.num_units], dtype=incoming.dtype, initializer=getters.get_initializer(self.bias_init), trainable=self.trainable, restore=self.restore) track(self._b, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = tf.nn.bias_add(value=inference, bias=self._b) if self.activation: inference = getters.get_activation(self.activation, collect=True)(inference) if self._dropout: inference = self._dropout(inference) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def _build(self, incoming, *args, **kwargs): """ Args: incoming: 1-D Tensor [samples]. If not 2D, input will be flatten. Returns: 1-D Tensor [samples]. """ input_shape = get_shape(incoming) n_inputs = int(np.prod(a=input_shape[1:])) initializer = tf.constant_initializer(value=np.random.randn()) self._w = variable(name='w', shape=[n_inputs], dtype=incoming.dtype, initializer=initializer, trainable=self.trainable, restore=self.restore) track(self._w, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = incoming # If input is not 2d, flatten it. if len(input_shape) > 1: inference = tf.reshape(tensor=inference, shape=[-1]) inference = tf.multiply(x=inference, y=self._w) self._b = None if self.bias: self._b = variable(name='b', shape=[n_inputs], dtype=incoming.dtype, initializer=initializer, trainable=self.trainable, restore=self.restore) inference = tf.add(inference, self._b) track(self._b, tf.GraphKeys.LAYER_VARIABLES, self.module_name) if self.activation: inference = getters.get_activation(self.activation, collect=True)(inference) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def _build(self, incoming, *args, **kwargs): """ Args: incoming: 1-D Tensor [samples]. If not 2D, input will be flatten. Returns: 1-D Tensor [samples]. """ input_shape = get_shape(incoming) n_inputs = total_tensor_depth(tensor_shape=input_shape) initializer = tf.constant_initializer(value=np.random.randn()) self._w = variable(name='w', shape=[n_inputs], dtype=incoming.dtype, initializer=initializer, trainable=self.trainable, restore=self.restore) track(self._w, tf.GraphKeys.LAYER_VARIABLES, self.module_name) inference = incoming # If input is not 2d, flatten it. if len(input_shape) > 1: inference = tf.reshape(tensor=inference, shape=[-1]) inference = tf.multiply(x=inference, y=self._w) self._b = None if self.bias: self._b = variable(name='b', shape=[n_inputs], dtype=incoming.dtype, initializer=initializer, trainable=self.trainable, restore=self.restore) inference = tf.add(inference, self._b) track(self._b, tf.GraphKeys.LAYER_VARIABLES, self.module_name) if self.activation: inference = getters.get_activation(self.activation, collect=True)(inference) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def create_learning_rate_decay_fn(learning_rate, decay_type, decay_steps, decay_rate, start_decay_at=0, stop_decay_at=1e9, min_learning_rate=None, staircase=False, global_step=None): """Creates a function that decays the learning rate. Args: learning_rate: A Tensor or a floating point value. The learning rate to use. decay_steps: How often to apply decay. decay_rate: A Python number. The decay rate. start_decay_at: Don't decay before this step stop_decay_at: Don't decay after this step min_learning_rate: Don't decay below this number decay_type: A decay function name defined in `tf.train` possible Values: exponential_decay, inverse_time_decay, natural_exp_decay, piecewise_constant, polynomial_decay. staircase: Whether to apply decay in a discrete staircase, as opposed to continuous, fashion. global_step: Scalar int `Tensor`, step counter for each update. If not supplied, it will be fetched from the default graph (see `tf.contrib.framework.get_global_step` for details). If it's not been created, no step will be incremented with each weight update. `learning_rate_decay_fn` requires `global_step`. Returns: A function that takes (learning_rate, global_step) as inputs and returns the learning rate for the given step. Returns `None` if decay_type is empty or None. """ if decay_type is None or decay_type == "": return learning_rate start_decay_at = tf.to_int32(start_decay_at) stop_decay_at = tf.to_int32(stop_decay_at) def decay_fn(learning_rate, global_step): """The computed learning rate decay function.""" global_step = tf.to_int32(global_step) decay_type_fn = getattr(tf.train, decay_type) kwargs = dict( learning_rate=learning_rate, global_step=tf.minimum(global_step, stop_decay_at) - start_decay_at, decay_steps=decay_steps, staircase=staircase, name="decayed_learning_rate" ) decay_fn_args = get_arguments(decay_type_fn) if 'decay_rate' in decay_fn_args: kwargs['decay_rate'] = decay_rate if 'staircase' in decay_fn_args: kwargs['staircase'] = staircase decayed_learning_rate = decay_type_fn(**kwargs) final_lr = tf.train.piecewise_constant( x=global_step, boundaries=[start_decay_at], values=[learning_rate, decayed_learning_rate]) if min_learning_rate: final_lr = tf.maximum(final_lr, min_learning_rate) return final_lr learning_rate = decay_fn(learning_rate, global_step or get_global_step()) track(learning_rate, tf.GraphKeys.LEARNING_RATE) return learning_rate
def create_learning_rate_decay_fn(learning_rate, decay_type, decay_steps, decay_rate, start_decay_at=0, stop_decay_at=1e9, min_learning_rate=None, staircase=False, global_step=None): """Creates a function that decays the learning rate. Args: learning_rate: A Tensor or a floating point value. The learning rate to use. decay_steps: How often to apply decay. decay_rate: A Python number. The decay rate. start_decay_at: Don't decay before this step stop_decay_at: Don't decay after this step min_learning_rate: Don't decay below this number decay_type: A decay function name defined in `tf.train` possible Values: exponential_decay, inverse_time_decay, natural_exp_decay, piecewise_constant, polynomial_decay. staircase: Whether to apply decay in a discrete staircase, as opposed to continuous, fashion. global_step: Scalar int `Tensor`, step counter for each update. If not supplied, it will be fetched from the default graph (see `tf.contrib.framework.get_global_step` for details). If it's not been created, no step will be incremented with each weight update. `learning_rate_decay_fn` requires `global_step`. Returns: A function that takes (learning_rate, global_step) as inputs and returns the learning rate for the given step. Returns `None` if decay_type is empty or None. """ if decay_type is None or decay_type == "": return learning_rate start_decay_at = tf.to_int32(start_decay_at) stop_decay_at = tf.to_int32(stop_decay_at) def decay_fn(learning_rate, global_step): """The computed learning rate decay function.""" global_step = tf.to_int32(global_step) decay_type_fn = getattr(tf.train, decay_type) decayed_learning_rate = decay_type_fn( learning_rate=learning_rate, global_step=tf.minimum(global_step, stop_decay_at) - start_decay_at, decay_steps=decay_steps, decay_rate=decay_rate, staircase=staircase, name="decayed_learning_rate") final_lr = tf.train.piecewise_constant( x=global_step, boundaries=[start_decay_at], values=[learning_rate, decayed_learning_rate]) if min_learning_rate: final_lr = tf.maximum(final_lr, min_learning_rate) return final_lr learning_rate = decay_fn(learning_rate, global_step or get_global_step()) track(learning_rate, tf.GraphKeys.LEARNING_RATE) return learning_rate
def _build(self, incoming, *args, **kwargs): """ Args: incoming: (2+)-D Tensor [samples, input dim]. If not 2D, input will be flatten. Returns: 2D Tensor [samples, num_units]. """ self._declare_dependencies() input_shape = get_shape(incoming) assert len(input_shape) > 1, 'Incoming Tensor shape must be at least 2-D' n_inputs = total_tensor_depth(tensor_shape=input_shape) regularizer = getters.get_regularizer(self.regularizer, scale=self.scale, collect=True) initializer = getters.get_initializer(self.weights_init) self._w = variable(name='w', shape=[n_inputs, self.num_units], regularizer=regularizer, initializer=initializer, trainable=self.trainable, restore=self.restore) track(self._w, tf.GraphKeys.LAYER_VARIABLES, self.module_name) self._b = variable(name='b', shape=[self.num_units], initializer=getters.get_initializer(self.bias_init), trainable=self.trainable, restore=self.restore) track(self._b, tf.GraphKeys.LAYER_VARIABLES, self.module_name) # Weight and bias for the transform gate self._w_t = variable(name='w_t', shape=[n_inputs, self.num_units], regularizer=None, initializer=initializer, trainable=self.trainable, restore=self.restore) track(self._w_t, tf.GraphKeys.LAYER_VARIABLES, self.module_name) self._b_t = variable(name='b_t', shape=[self.num_units], initializer=tf.constant_initializer(-1), trainable=self.trainable, restore=self.restore) track(self._b_t, tf.GraphKeys.LAYER_VARIABLES, self.module_name) # If input is not 2d, flatten it. if len(input_shape) > 2: incoming = tf.reshape(tensor=incoming, shape=[-1, n_inputs]) H = getters.get_activation(self.activation)(tf.matmul(a=incoming, b=self._w) + self._b) T = tf.sigmoid(tf.matmul(a=incoming, b=self._w_t) + self._b_t) if self._transform_dropout: T = self._transform_dropout(T) C = tf.subtract(x=1.0, y=T) inference = tf.add(x=tf.multiply(x=H, y=T), y=tf.multiply(x=incoming, y=C)) track(inference, tf.GraphKeys.ACTIVATIONS) track(inference, tf.GraphKeys.LAYER_TENSOR, self.module_name) return inference
def _build(self, incoming, *args, **kwargs): """ Args: incoming: `Tensor`. 3-D Tensor Layer [samples, timesteps, input dim]. """ assert (self.rnncell_fw.output_size == self.rnncell_bw.output_size), "RNN Cells number of units must match!" sequence_length = kwargs.get('sequence_length') if self.dynamic and sequence_length is None: sequence_length = retrieve_seq_length_op( incoming if isinstance(incoming, tf.Tensor) else tf.stack(incoming)) input_shape = get_shape(incoming) # TODO: DropoutWrapper inference = incoming # If a static rnn and tensor given, convert it to a per timestep list if type(inference) not in [list, np.array] and not self.dynamic: ndim = len(input_shape) assert ndim >= 3, 'Input dim should be at least 3.' axes = [1, 0] + list(xrange(2, ndim)) inference = tf.transpose(inference, axes) inference = tf.unstack(value=inference) if self.dynamic: # outputs are a tuple of (fw, bw) outputs outputs, (states_fw, states_bw) = tf.nn.bidirectional_dynamic_rnn( cell_fw=self.rnncell_fw, cell_bw=self.rnncell_bw, inputs=inference, initial_state_fw=self.initial_state_fw, initial_state_bw=self.initial_state_bw, sequence_length=sequence_length, dtype=tf.float32) else: # outputs are a concatenation of both bw and fw outputs outputs, states_fw, states_bw = rnn.static_bidirectional_rnn( cell_fw=self.rnncell_fw, cell_bw=self.rnncell_bw, inputs=inference, initial_state_fw=self.initial_state_fw, initial_state_bw=self.initial_state_bw, sequence_length=sequence_length, dtype=tf.float32) for v in [self.rnncell_fw.w, self.rnncell_fw.b, self.rnncell_bw.w, self.rnncell_bw.b]: if hasattr(v, '__len__'): for var in v: track(var, tf.GraphKeys.LAYER_VARIABLES, self.module_name) else: track(v, tf.GraphKeys.LAYER_VARIABLES, self.module_name) if self.dynamic: tf.add_to_collection(tf.GraphKeys.ACTIVATIONS, outputs[0][-1]) else: tf.add_to_collection(tf.GraphKeys.ACTIVATIONS, outputs[-1]) if self.dynamic: if self.return_seq: o = outputs else: # we are only interested in the fw pass here o = get_sequence_relevant_output(outputs[0], sequence_length) else: o = outputs if self.return_seq else outputs[-1] track(o, tf.GraphKeys.LAYER_TENSOR, self.module_name) return (o, states_fw, states_bw) if self.return_states else o
def decay(exploration_rate=0.15, decay_type='polynomial_decay', start_decay_at=0, stop_decay_at=1e9, decay_rate=0., staircase=False, decay_steps=100000, min_exploration_rate=0): """Builds a decaying exploration. Decay epsilon based on number of states and the decay_type. Args: exploration_rate: `float` or `list` of `float`. The initial value of the exploration rate. decay_type: A decay function name defined in `exploration_decay` possible Values: exponential_decay, inverse_time_decay, natural_exp_decay, piecewise_constant, polynomial_decay. start_decay_at: `int`. When to start the decay. stop_decay_at: `int`. When to stop the decay. decay_rate: A Python number. The decay rate. staircase: Whether to apply decay in a discrete staircase, as opposed to continuous, fashion. decay_steps: How often to apply decay. min_exploration_rate: `float`. Don't decay below this number. Returns: `function` the exploration function logic. """ def decay_fn(timestep): """The computed decayed exploration rate. Args: timestep: the current timestep. """ timestep = tf.to_int32(timestep) decay_type_fn = getattr(exploration_decay, decay_type) kwargs = dict( exploration_rate=exploration_rate, timestep=tf.minimum(timestep, tf.to_int32(stop_decay_at)) - tf.to_int32(start_decay_at), decay_steps=decay_steps, name="decayed_exploration_rate") decay_fn_args = get_arguments(decay_type_fn) if 'decay_rate' in decay_fn_args: kwargs['decay_rate'] = decay_rate if 'staircase' in decay_fn_args: kwargs['staircase'] = staircase decayed_exploration_rate = decay_type_fn(**kwargs) final_exploration_rate = tf.train.piecewise_constant( x=timestep, boundaries=[start_decay_at], values=[exploration_rate, decayed_exploration_rate]) if min_exploration_rate: final_exploration_rate = tf.maximum(final_exploration_rate, min_exploration_rate) return final_exploration_rate exploration_rate = decay_fn(get_global_timestep()) track(exploration_rate, tf.GraphKeys.EXPLORATION_RATE) return exploration_rate
def regularizer(x): x = fct(x) if collect: track(x, tf.GraphKeys.REGULARIZATION_LOSSES) return x
def _build(self, incoming, *args, **kwargs): input_shape = get_shape(incoming) input_ndim = len(input_shape) gamma_init = tf.random_normal_initializer(mean=self.gamma, stddev=self.stddev) self._beta = variable(name='beta', shape=[input_shape[-1]], initializer=tf.constant_initializer(self.beta), trainable=self.trainable, restore=self.restore) self._gamma = variable(name='gamma', shape=[input_shape[-1]], initializer=gamma_init, trainable=self.trainable, restore=self.restore) track(self._beta, tf.GraphKeys.LAYER_VARIABLES, self.module_name) track(self._gamma, tf.GraphKeys.LAYER_VARIABLES, self.module_name) if not self.restore: track(tf.GraphKeys.EXCL_RESTORE_VARIABLES, self._beta) track(tf.GraphKeys.EXCL_RESTORE_VARIABLES, self._gamma) axis = list(xrange(input_ndim - 1)) moving_mean = variable(name='moving_mean', shape=input_shape[-1:], initializer=tf.zeros_initializer(), trainable=False, restore=self.restore) moving_variance = variable(name='moving_variance', shape=input_shape[-1:], initializer=tf.constant_initializer(1.), trainable=False, restore=self.restore) def update_mean_var(): mean, variance = tf.nn.moments(x=incoming, axes=axis) update_moving_mean = moving_averages.assign_moving_average( variable=moving_mean, value=mean, decay=self.decay, zero_debias=False) update_moving_variance = moving_averages.assign_moving_average( variable=moving_variance, value=variance, decay=self.decay, zero_debias=False) with tf.control_dependencies( [update_moving_mean, update_moving_variance]): return tf.identity(mean), tf.identity(variance) # Retrieve variable managing training mode if Modes.is_train(self.mode): mean, var = update_mean_var() else: mean, var = moving_mean, moving_variance incoming = tf.nn.batch_normalization(x=incoming, mean=mean, variance=var, offset=self._beta, scale=self._gamma, variance_epsilon=self.epsilon) incoming.set_shape(input_shape) track(incoming, tf.GraphKeys.LAYER_TENSOR, self.module_name) return incoming
def activation(x): x = fct(x, name=name) if collect: track(x, tf.GraphKeys.ACTIVATIONS) return x