Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Tensorflow be used to instantiate an estimator using Python?
TensorFlow Estimators provide a high-level API for building machine learning models. The DNNClassifier is a pre-made estimator that creates deep neural networks for classification tasks.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
An Estimator is TensorFlow's high-level representation of a complete model, designed for easy scaling and asynchronous training. We'll demonstrate using the classic Iris dataset for multi-class classification.
Setting Up Feature Columns
First, we need to define feature columns that describe how the model should use input data ?
import tensorflow as tf
# Define feature columns for Iris dataset
my_feature_columns = []
feature_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
for key in feature_names:
my_feature_columns.append(tf.feature_column.numeric_column(key=key))
print("Feature columns created:", len(my_feature_columns))
Feature columns created: 4
Creating the DNNClassifier
Now we can instantiate the DNNClassifier estimator with our feature columns ?
print("Build a DNN that has 2 hidden layers with 30 and 10 hidden nodes each")
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns,
hidden_units=[30, 10],
n_classes=3)
print("Classifier created successfully")
Build a DNN that has 2 hidden layers with 30 and 10 hidden nodes each
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpdh8866zb
INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmpdh8866zb', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
rewrite_options {
meta_optimizer_iterations: ONE
}
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_checkpoint_save_graph_def': True, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
Classifier created successfully
Parameters
The DNNClassifier accepts several key parameters ?
| Parameter | Description | Example |
|---|---|---|
feature_columns |
List defining input features | my_feature_columns |
hidden_units |
List of hidden layer sizes | [30, 10] |
n_classes |
Number of output classes |
3 (for Iris) |
model_dir |
Directory to save model | '/tmp/model' |
Available Pre-made Estimators
TensorFlow provides several pre-made estimators for different use cases ?
-
tf.estimator.DNNClassifier− Deep neural networks for multi-class classification -
tf.estimator.LinearClassifier− Linear models for classification -
tf.estimator.DNNLinearCombinedClassifier− Wide & deep models combining both approaches -
tf.estimator.DNNRegressor− Deep neural networks for regression tasks
Conclusion
TensorFlow's DNNClassifier provides an easy way to create deep neural network classifiers. Simply define your feature columns, specify the network architecture with hidden_units, and set the number of classes for your classification problem.
