parameters: (argv)
docstring: Builds, trains, and evaluates the model.
docstring_summary: Builds, trains, and evaluates the model.
docstring_tokens: Builds trains and evaluates the model .
function: def main(argv):
    """Builds, trains, and evaluates the model."""
    assert len(argv) == 1
    (train, test) = imports85.dataset()
  
    # Switch the labels to units of thousands for better convergence.
    def normalize_price(features, labels):
      return features, labels / PRICE_NORM_FACTOR
  
    train = train.map(normalize_price)
    test = test.map(normalize_price)
  
    # Build the training input_fn.
    def input_train():
      return (
          # Shuffling with a buffer larger than the data set ensures
          # that the examples are well mixed.
          train.shuffle(1000).batch(128)
          # Repeat forever
          .repeat())
  
    # Build the validation input_fn.
    def input_test():
      return test.shuffle(1000).batch(128)
  
    # The following code demonstrates two of the ways that `feature_columns` can
    # be used to build a model with categorical inputs.
  
    # The first way assigns a unique weight to each category. To do this, you must
    # specify the category's vocabulary (values outside this specification will
    # receive a weight of zero).
    # Alternatively, you can define the vocabulary in a file (by calling
    # `categorical_column_with_vocabulary_file`) or as a range of positive
    # integers (by calling `categorical_column_with_identity`)
    body_style_vocab = ["hardtop", "wagon", "sedan", "hatchback", "convertible"]
    body_style_column = tf.feature_column.categorical_column_with_vocabulary_list(
        key="body-style", vocabulary_list=body_style_vocab)
  
    # The second way, appropriate for an unspecified vocabulary, is to create a
    # hashed column. It will create a fixed length list of weights, and
    # automatically assign each input category to a weight. Due to the
    # pseudo-randomness of the process, some weights may be shared between
    # categories, while others will remain unused.
    make_column = tf.feature_column.categorical_column_with_hash_bucket(
        key="make", hash_bucket_size=50)
  
    feature_columns = [
        # This model uses the same two numeric features as `linear_regressor.py`
        tf.feature_column.numeric_column(key="curb-weight"),
        tf.feature_column.numeric_column(key="highway-mpg"),
        # This model adds two categorical colums that will adjust the price based
        # on "make" and "body-style".
        body_style_column,
        make_column,
    ]
  
    # Build the Estimator.
    model = tf.estimator.LinearRegressor(feature_columns=feature_columns)
  
    # Train the model.
    # By default, the Estimators log output every 100 steps.
    model.train(input_fn=input_train, steps=STEPS)
  
    # Evaluate how the model performs on data it has not yet seen.
    eval_result = model.evaluate(input_fn=input_test)
  
    # The evaluation returns a Python dictionary. The "average_loss" key holds the
    # Mean Squared Error (MSE).
    average_loss = eval_result["average_loss"]
  
    # Convert MSE to Root Mean Square Error (RMSE).
    print("\n" + 80 * "*")
    print("\nRMS error for the test set: ${:.0f}"
          .format(PRICE_NORM_FACTOR * average_loss**0.5))
  
    print()
function_tokens: def main ( argv ) : assert len ( argv ) == 1 ( train , test ) = imports85 . dataset ( ) # Switch the labels to units of thousands for better convergence. def normalize_price ( features , labels ) : return features , labels / PRICE_NORM_FACTOR train = train . map ( normalize_price ) test = test . map ( normalize_price ) # Build the training input_fn. def input_train ( ) : return ( # Shuffling with a buffer larger than the data set ensures # that the examples are well mixed. train . shuffle ( 1000 ) . batch ( 128 ) # Repeat forever . repeat ( ) ) # Build the validation input_fn. def input_test ( ) : return test . shuffle ( 1000 ) . batch ( 128 ) # The following code demonstrates two of the ways that `feature_columns` can # be used to build a model with categorical inputs. # The first way assigns a unique weight to each category. To do this, you must # specify the category's vocabulary (values outside this specification will # receive a weight of zero). # Alternatively, you can define the vocabulary in a file (by calling # `categorical_column_with_vocabulary_file`) or as a range of positive # integers (by calling `categorical_column_with_identity`) body_style_vocab = [ "hardtop" , "wagon" , "sedan" , "hatchback" , "convertible" ] body_style_column = tf . feature_column . categorical_column_with_vocabulary_list ( key = "body-style" , vocabulary_list = body_style_vocab ) # The second way, appropriate for an unspecified vocabulary, is to create a # hashed column. It will create a fixed length list of weights, and # automatically assign each input category to a weight. Due to the # pseudo-randomness of the process, some weights may be shared between # categories, while others will remain unused. make_column = tf . feature_column . categorical_column_with_hash_bucket ( key = "make" , hash_bucket_size = 50 ) feature_columns = [ # This model uses the same two numeric features as `linear_regressor.py` tf . feature_column . numeric_column ( key = "curb-weight" ) , tf . feature_column . numeric_column ( key = "highway-mpg" ) , # This model adds two categorical colums that will adjust the price based # on "make" and "body-style". body_style_column , make_column , ] # Build the Estimator. model = tf . estimator . LinearRegressor ( feature_columns = feature_columns ) # Train the model. # By default, the Estimators log output every 100 steps. model . train ( input_fn = input_train , steps = STEPS ) # Evaluate how the model performs on data it has not yet seen. eval_result = model . evaluate ( input_fn = input_test ) # The evaluation returns a Python dictionary. The "average_loss" key holds the # Mean Squared Error (MSE). average_loss = eval_result [ "average_loss" ] # Convert MSE to Root Mean Square Error (RMSE). print ( "\n" + 80 * "*" ) print ( "\nRMS error for the test set: ${:.0f}" . format ( PRICE_NORM_FACTOR * average_loss ** 0.5 ) ) print ( )