<jupyter_start><jupyter_text>Working with other models from tf.contrib.learn<jupyter_code>import tensorflow as tf
import matplotlib.pyplot as plt
import os

import pandas as pd
from sklearn.model_selection import train_test_split
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1. Load and prepare the data<jupyter_code>current_dir = os.getcwd()
dataset_path = os.path.join(os.getcwd(), os.pardir, 'data', 'diamond_prices_original.csv')
diamonds = pd.read_csv(dataset_path)
TARGET = 'price'
## Separating into training and testing: the objects we got back are pandas DataFrames.
X_train, X_val, y_train, y_val = train_test_split(diamonds.drop(TARGET, axis=1),
                                                  diamonds[TARGET],
                                                  train_size=0.9,
                                                  random_state=60)
diamonds.head(10)<jupyter_output><empty_output><jupyter_text>2. Write one or more dataset importing functions<jupyter_code>BATCH_SIZE = 128
N_EPOCHS = 400
## Training input function
input_fn_train = tf.estimator.inputs.pandas_input_fn(
    x=X_train,
    y=y_train,
    batch_size=BATCH_SIZE,
    num_epochs=N_EPOCHS,
    shuffle = True,
    target_column=TARGET)

## Validation input function
input_fn_val = tf.estimator.inputs.pandas_input_fn(
    x=X_val,
    y=y_val,
    num_epochs=1,
    shuffle = False,
    target_column=TARGET)<jupyter_output><empty_output><jupyter_text>3. Define the features for the "deep" part of the model (DNN)Here we commonly use the numerical features or other dense columns in our data.<jupyter_code>numerical_cols = ['carat', 'depth', 'table', 'x', 'y', 'z']
categorical_cols = ['cut', 'color', 'clarity']

unique_values = {col: list(diamonds[col].unique()) for col in categorical_cols}

cut = tf.feature_column.categorical_column_with_vocabulary_list('cut', 
                                                                vocabulary_list=list(diamonds['cut'].unique()))


deep_columns = [tf.feature_column.numeric_column(col) for col in numerical_cols]
deep_columns = deep_columns + [tf.feature_column.indicator_column(cut)]<jupyter_output><empty_output><jupyter_text>4. Define the features for the "wide" part of the model (linear model)You can create interactions that result in sparse columns in this step.<jupyter_code>clarity = tf.feature_column.categorical_column_with_vocabulary_list('clarity', 
                                                                vocabulary_list=list(diamonds['clarity'].unique()))

color = tf.feature_column.categorical_column_with_vocabulary_list('color', 
                                                                vocabulary_list=list(diamonds['color'].unique()))

# Creating the 3 interaction features
# tf.feature_column.crossed_column: Returns a column for performing crosses of categorical features.
cut_x_color = tf.feature_column.crossed_column(['cut', 'color'], hash_bucket_size=5*7)
cut_x_clarity = tf.feature_column.crossed_column(['cut', 'clarity'], hash_bucket_size=5*8)
color_x_clarity = tf.feature_column.crossed_column(['color', 'clarity'], hash_bucket_size=7*8)

wide_columns = [color, clarity, cut_x_color, cut_x_clarity, color_x_clarity]<jupyter_output><empty_output><jupyter_text>5. Instantiate the DNNLinearCombinedRegressor Estimator<jupyter_code>combined_model = tf.estimator.DNNLinearCombinedRegressor(
    model_dir='./wide_and_deep_model',
    linear_feature_columns=wide_columns,
    dnn_feature_columns=deep_columns,
    dnn_hidden_units=[64, 32, 16, 8])<jupyter_output>INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': './wide_and_deep_model', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x0000023D9F48E400>, '_task_type': 'worker', '_task_id': 0, '_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}<jupyter_text>5. Train the model<jupyter_code>combined_model.train(input_fn=input_fn_train)<jupyter_output>INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into ./wide_and_deep_model\model.ckpt.
INFO:tensorflow:loss = 4.14569e+09, step = 1
INFO:tensorflow:global_step/sec: 190.763
INFO:tensorflow:loss = 3.48952e+09, step = 101 (0.527 sec)
INFO:tensorflow:global_step/sec: 314.002
INFO:tensorflow:loss = 3.84351e+09, step = 201 (0.319 sec)
INFO:tensorflow:global_step/sec: 287.781
INFO:tensorflow:loss = 4.21204e+09, step = 301 (0.348 sec)
INFO:tensorflow:global_step/sec: 331.264
INFO:tensorflow:loss = 3.55313e+09, step = 401 (0.301 sec)
INFO:tensorflow:global_step/sec: 260.996
INFO:tensorflow:loss = 3.73544e+09, step = 501 (0.384 sec)
INFO:tensorflow:global_step/sec: 281.039
INFO:tensorflow:loss = 2.89769e+09, step = 601 (0.356 sec)
INFO:tensorflow:global_step/sec: 239.653
INFO:tensorflow:loss = 4.20192e+09, step = 701 (0.418 sec)
INFO:tensorflow:global_step/sec: 286.951
INFO:tensorflow:loss = 4.17243e+09, step = 801 (0.353 sec)
INFO:tensorflow:global_step/sec[...]<jupyter_text>6. Visualize/analyze the results of the model<jupyter_code>combined_model.evaluate(input_fn=input_fn_val)
## This returns an iterator
predictions = combined_model.predict(input_fn=input_fn_val)
## Transform the values to a pandas series
predictions = pd.Series([x['predictions'][0] for x in predictions], index=y_val.index)
fig, ax = plt.subplots(figsize=(8,5))
ax.scatter(x=predictions, y=y_val, s=0.5)
ax.set_xlabel('Predicted prices')
ax.set_ylabel('Observed prices')
ax.set_title("Predictions vs. Observed Values in the validation set");
fig, ax = plt.subplots(figsize=(8,5))
ax.hist(abs(y_val-predictions), bins=100, edgecolor='black')
ax.set_xlim(0, 4e3);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11648204095334004, 'fraction_numerical': 0.0433031218529708, 'mean_word_length': 5.043610547667343, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '393005', 'license': 'MIT', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2000, 'n_words': 393, 'path': 'Section3/4_working_with_other_models_from_tfcontriblearn.ipynb', 'repo_name': 'PacktPublishing/TensorFlow-1.X-Recipes-for-Supervised-and-Unsupervised-Learning', 'size': '5958'}