def _test_complete_flow(self, train_input_fn, eval_input_fn, predict_input_fn, input_dimension, label_dimension, batch_size): feature_columns = [feature_column_v2.numeric_column('x', shape=(input_dimension,))] est = dnn.DNNRegressor( hidden_units=(2, 2), feature_columns=feature_columns, label_dimension=label_dimension, model_dir=self._model_dir) # TRAIN num_steps = 10 est.train(train_input_fn, steps=num_steps) # EVALUATE scores = est.evaluate(eval_input_fn) self.assertEqual(num_steps, scores[ops.GraphKeys.GLOBAL_STEP]) self.assertIn('loss', six.iterkeys(scores)) # PREDICT predictions = np.array([ x[prediction_keys.PredictionKeys.PREDICTIONS] for x in est.predict(predict_input_fn) ]) self.assertAllEqual((batch_size, label_dimension), predictions.shape) # EXPORT feature_spec = feature_column_v2.make_parse_example_spec_v2(feature_columns) serving_input_receiver_fn = export.build_parsing_serving_input_receiver_fn( feature_spec) export_dir = est.export_savedmodel(tempfile.mkdtemp(), serving_input_receiver_fn) self.assertTrue(gfile.Exists(export_dir))
def DNNRegressorWithLayerAnnotations( # pylint: disable=invalid-name hidden_units, feature_columns, model_dir=None, label_dimension=1, weight_column=None, optimizer='Adagrad', activation_fn=nn.relu, dropout=None, input_layer_partitioner=None, config=None, warm_start_from=None, loss_reduction=losses.Reduction.SUM_OVER_BATCH_SIZE, ): """A regressor for TensorFlow DNN models with layer annotations. This regressor is fuctionally identical to estimator.DNNRegressor as far as training and evaluating models is concerned. The key difference is that this classifier adds additional layer annotations, which can be used for computing Integrated Gradients. Integrated Gradients is a method for attributing a classifier's predictions to its input features (https://arxiv.org/pdf/1703.01365.pdf). Given an input instance, the method assigns attribution scores to individual features in proportion to the feature's importance to the classifier's prediction. See estimator.DNNRegressor for example code for training and evaluating models using this regressor. This regressor is checkpoint-compatible with estimator.DNNRegressor and therefore the following should work seamlessly: # Instantiate ordinary estimator as usual. estimator = tf.estimator.DNNRegressor( config, feature_columns, hidden_units, ...) # Train estimator, export checkpoint. tf.estimator.train_and_evaluate(estimator, ...) # Instantiate estimator with annotations with the same configuration as the # ordinary estimator. estimator_with_annotations = ( tf.contrib.estimator.DNNRegressorWithLayerAnnotations( config, feature_columns, hidden_units, ...)) # Call export_savedmodel with the same arguments as the ordinary estimator, # using the checkpoint produced for the ordinary estimator. estimator_with_annotations.export_saved_model( export_dir_base, serving_input_receiver, ... checkpoint_path='/path/to/ordinary/estimator/checkpoint/model.ckpt-1234') Args: hidden_units: Iterable of number hidden units per layer. All layers are fully connected. Ex. `[64, 32]` means first layer has 64 nodes and second one has 32. feature_columns: An iterable containing all the feature columns used by the model. All items in the set should be instances of classes derived from `_FeatureColumn`. model_dir: Directory to save model parameters, graph and etc. This can also be used to load checkpoints from the directory into a estimator to continue training a previously saved model. label_dimension: Number of regression targets per example. This is the size of the last dimension of the labels and logits `Tensor` objects (typically, these have shape `[batch_size, label_dimension]`). weight_column: A string or a `_NumericColumn` created by `tf.feature_column.numeric_column` defining feature column representing weights. It is used to down weight or boost examples during training. It will be multiplied by the loss of the example. If it is a string, it is used as a key to fetch weight tensor from the `features`. If it is a `_NumericColumn`, raw tensor is fetched by key `weight_column.key`, then weight_column.normalizer_fn is applied on it to get weight tensor. optimizer: An instance of `tf.Optimizer` used to train the model. Defaults to Adagrad optimizer. activation_fn: Activation function applied to each layer. If `None`, will use `tf.nn.relu`. dropout: When not `None`, the probability we will drop out a given coordinate. input_layer_partitioner: Optional. Partitioner for input layer. Defaults to `min_max_variable_partitioner` with `min_slice_size` 64 << 20. config: `RunConfig` object to configure the runtime settings. warm_start_from: A string filepath to a checkpoint to warm-start from, or a `WarmStartSettings` object to fully configure warm-starting. If the string filepath is provided instead of a `WarmStartSettings`, then all weights are warm-started, and it is assumed that vocabularies and Tensor names are unchanged. loss_reduction: One of `tf.losses.Reduction` except `NONE`. Describes how to reduce training loss over batch. Defaults to `SUM`. Returns: DNNRegressor with layer annotations. """ original = dnn.DNNRegressor( hidden_units=hidden_units, feature_columns=feature_columns, model_dir=model_dir, label_dimension=label_dimension, weight_column=weight_column, optimizer=optimizer, activation_fn=activation_fn, dropout=dropout, input_layer_partitioner=input_layer_partitioner, config=config, warm_start_from=warm_start_from, loss_reduction=loss_reduction, ) def _model_fn(features, labels, mode, config): with _monkey_patch( feature_column_lib, '_internal_input_layer', make_input_layer_with_layer_annotations( feature_column_lib._internal_input_layer)): # pylint: disable=protected-access return original.model_fn(features, labels, mode, config) return estimator.Estimator(model_fn=_model_fn, model_dir=model_dir, config=config, warm_start_from=warm_start_from)
def _dnn_regressor_fn(*args, **kwargs): return dnn.DNNRegressor(*args, **kwargs)