pdfcoffee
Chapter 3• crossed_column: When we want to use two columns combined as onefeature, for example, in the case of geolocation-based data it makes sense tocombine longitude and latitude values as one feature.• numeric_column: Used when the feature is a numeric, it can be a singlevalue or even a matrix.• indicator_column: We do not use this directly. Instead, it is used with thecategorical column, but only when the number of categories is limited andcan be represented as one-hot encoded.• embedding_column: We do not use this directly. Instead, it is used with thecategorical column, but only when the number of categories is very large andcannot be represented as one-hot encoded.• bucketized_column: This is used when, instead of a specific numeric value,we split the data into different categories depending upon its value.The first six functions inherit from the Categorical Column class, the next threeinherit from the Dense Column class, and the last one inherits from both classes. Inthe following example we will use numeric_column and categorical_column_with_vocabulary_list functions.Input functionsThe data for training, evaluation, as well as prediction, needs to be made availablethrough an input function. The input function returns a tf.data.Dataset object;the object returns a tuple containing features and labels.MNIST using TensorFlow Estimator APILet us build a simple TensorFlow estimator with a simple dataset for a multipleregression problem. We continue with the home price prediction, but now havetwo features, that is, we are considering two independent variables: the area of thehouse and its type (bungalow or apartment) on which we presume our price shoulddepend:1. We import the necessary modules. We will need TensorFlow and itsfeature_column module. Since our dataset contains both numeric andcategorical data, we need the functions to process both types of data:import tensorflow as tffrom tensorflow import feature_column as fcnumeric_column = fc.numeric_columncategorical_column_with_vocabulary_list = fc.categorical_column_with_vocabulary_list[ 95 ]
Regression2. Now, we define the feature columns we will be using to train the regressor.Our dataset, as we mentioned, consists of two features "area" a numericvalue signifying the area of the house and "type" telling if it is a "bungalow"or "apartment":featcols = [tf.feature_column.numeric_column("area"),tf.feature_column.categorical_column_with_vocabulary_list("type",["bungalow","apartment"])]3. In the next step, we define an input function to provide input for training.The function returns a tuple containing features and labels:def train_input_fn():features = {"area":[1000,2000,4000,1000,2000,4000],"type":["bungalow","bungalow","house","apartment","apartment","apartment"]}labels = [ 500 , 1000 , 1500 , 700 , 1300 , 1900 ]return features, labels4. Next, we use the premade LinearRegressor estimator and fit it on thetraining dataset:model = tf.estimator.LinearRegressor(featcols)model.train(train_input_fn, steps=200)5. Now that the estimator is trained, let us see the result of the prediction:def predict_input_fn():features = {"area":[1500,1800],"type":["house","apt"]}return featurespredictions = model.predict(predict_input_fn)print(next(predictions))print(next(predictions))--------------------------------------------------6. The result:{'predictions': array([692.7829], dtype=float32)}{'predictions': array([830.9035], dtype=float32)}[ 96 ]
- Page 79 and 80: Neural Network Foundations with Ten
- Page 81 and 82: Neural Network Foundations with Ten
- Page 83 and 84: Neural Network Foundations with Ten
- Page 86 and 87: TensorFlow 1.x and 2.xThe intent of
- Page 88 and 89: An example to start withWe'll consi
- Page 90 and 91: Chapter 23. Placeholders: Placehold
- Page 92 and 93: • To create random values from a
- Page 94 and 95: To know the value, we need to creat
- Page 96 and 97: Chapter 2Both PyTorch and TensorFlo
- Page 98 and 99: Chapter 2state = [tf.zeros([100, 10
- Page 100 and 101: Chapter 2For now, there's no need t
- Page 102 and 103: Chapter 2Let's see an example of a
- Page 104 and 105: Chapter 2If you want to save a mode
- Page 106 and 107: Chapter 2supervised=True)train_data
- Page 108 and 109: Chapter 2There, tf.feature_column.n
- Page 110 and 111: Chapter 2print (dz_dx)print (dy_dx)
- Page 112 and 113: Chapter 2In our toy example we use
- Page 114 and 115: Chapter 2For multi-machine training
- Page 116 and 117: Chapter 25. Use tf.layers modules t
- Page 118 and 119: Chapter 2Keras or tf.keras?Another
- Page 120: • tf.data can be used to load mod
- Page 123 and 124: RegressionLet us imagine a simpler
- Page 125 and 126: RegressionTake a look at the last t
- Page 127 and 128: Regression3. Now, we calculate the
- Page 129: RegressionIn the next section we wi
- Page 133 and 134: Regression2. Download the dataset:(
- Page 135 and 136: RegressionThe following is the Tens
- Page 137 and 138: RegressionIn regression the aim is
- Page 139 and 140: RegressionThe Estimator outputs the
- Page 141 and 142: RegressionThe following is the grap
- Page 143 and 144: RegressionReferencesHere are some g
- Page 145 and 146: Convolutional Neural NetworksIn thi
- Page 147 and 148: Convolutional Neural NetworksIn thi
- Page 149 and 150: Convolutional Neural NetworksIn oth
- Page 151 and 152: Convolutional Neural NetworksThen w
- Page 153 and 154: Convolutional Neural NetworksHoweve
- Page 155 and 156: Convolutional Neural NetworksPlotti
- Page 157 and 158: Convolutional Neural NetworksIn gen
- Page 159 and 160: Convolutional Neural NetworksOur ne
- Page 161 and 162: Convolutional Neural NetworksThese
- Page 163 and 164: Convolutional Neural NetworksSo, we
- Page 165 and 166: Convolutional Neural NetworksEach i
- Page 167 and 168: Convolutional Neural NetworksVery d
- Page 169 and 170: Convolutional Neural NetworksRecogn
- Page 171 and 172: Convolutional Neural NetworksIf we
- Page 173 and 174: Convolutional Neural NetworksRefere
- Page 175 and 176: Advanced Convolutional Neural Netwo
- Page 177 and 178: Advanced Convolutional Neural Netwo
- Page 179 and 180: Advanced Convolutional Neural Netwo
Chapter 3
• crossed_column: When we want to use two columns combined as one
feature, for example, in the case of geolocation-based data it makes sense to
combine longitude and latitude values as one feature.
• numeric_column: Used when the feature is a numeric, it can be a single
value or even a matrix.
• indicator_column: We do not use this directly. Instead, it is used with the
categorical column, but only when the number of categories is limited and
can be represented as one-hot encoded.
• embedding_column: We do not use this directly. Instead, it is used with the
categorical column, but only when the number of categories is very large and
cannot be represented as one-hot encoded.
• bucketized_column: This is used when, instead of a specific numeric value,
we split the data into different categories depending upon its value.
The first six functions inherit from the Categorical Column class, the next three
inherit from the Dense Column class, and the last one inherits from both classes. In
the following example we will use numeric_column and categorical_column_
with_vocabulary_list functions.
Input functions
The data for training, evaluation, as well as prediction, needs to be made available
through an input function. The input function returns a tf.data.Dataset object;
the object returns a tuple containing features and labels.
MNIST using TensorFlow Estimator API
Let us build a simple TensorFlow estimator with a simple dataset for a multiple
regression problem. We continue with the home price prediction, but now have
two features, that is, we are considering two independent variables: the area of the
house and its type (bungalow or apartment) on which we presume our price should
depend:
1. We import the necessary modules. We will need TensorFlow and its
feature_column module. Since our dataset contains both numeric and
categorical data, we need the functions to process both types of data:
import tensorflow as tf
from tensorflow import feature_column as fc
numeric_column = fc.numeric_column
categorical_column_with_vocabulary_list = fc.categorical_column_
with_vocabulary_list
[ 95 ]