Skip to content

Commit 7a34628

Browse files
committed
Fixed tensorflow#4083 and two points of optimizations
1. Solved by adding cast with tf.cast(predictions, tf.float64) at the point of problem case. 2. Completed make_dataset() of automobile_data.py for the transforming of DataFrame to Dataset, the standard one we know of official site. 3. Added from_dataset() in automobile_data.py for re-use by others sites in cookbook/regression.
1 parent 9f7a5fa commit 7a34628

File tree

5 files changed

+35
-72
lines changed

5 files changed

+35
-72
lines changed

samples/cookbook/regression/automobile_data.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,18 @@ def load_data(y_name="price", train_fraction=0.7, seed=None):
109109

110110
return (x_train, y_train), (x_test, y_test)
111111

112-
def make_dataset(x, y=None):
113-
"""Create a slice Dataset from a pandas DataFrame and labels"""
114-
# TODO(markdaooust): simplify this after the 1.4 cut.
115-
# Convert the DataFrame to a dict
116-
x = dict(x)
117112

118-
# Convert the pd.Series to np.arrays
119-
for key in x:
120-
x[key] = np.array(x[key])
113+
def from_dataset(dataset): return lambda: dataset.make_one_shot_iterator().get_next()
121114

122-
items = [x]
123-
if y is not None:
124-
items.append(np.array(y, dtype=np.float32))
125115

126-
# Create a Dataset of slices
127-
return tf.data.Dataset.from_tensor_slices(tuple(items))
116+
def make_dataset(batch_sz, x, y=None, shuffle=False, shuffle_buffer_size=1000):
117+
"""Create a slice Dataset from a pandas DataFrame and labels"""
118+
if y is not None:
119+
dataset = tf.data.Dataset.from_tensor_slices((dict(x), y))
120+
else:
121+
dataset = tf.data.Dataset.from_tensor_slices(dict(x))
122+
if shuffle:
123+
dataset = dataset.shuffle(shuffle_buffer_size).batch(batch_sz).repeat()
124+
else:
125+
dataset = dataset.batch(batch_sz)
126+
return dataset

samples/cookbook/regression/custom_regression.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
parser.add_argument('--price_norm_factor', default=1000., type=float,
3232
help='price normalization factor')
3333

34-
35-
def from_dataset(ds):
36-
return lambda: ds.make_one_shot_iterator().get_next()
37-
38-
3934
def my_dnn_regression_fn(features, labels, mode, params):
4035
"""A model function implementing DNN regression for a custom Estimator."""
4136

@@ -81,6 +76,10 @@ def my_dnn_regression_fn(features, labels, mode, params):
8176
# Calculate root mean squared error
8277
print(labels)
8378
print(predictions)
79+
80+
# Fixed for #4083
81+
predictions = tf.cast(predictions, tf.float64)
82+
8483
rmse = tf.metrics.root_mean_squared_error(labels, predictions)
8584

8685
# Add the rmse to the collection of evaluation metrics.
@@ -103,16 +102,10 @@ def main(argv):
103102
test_y /= args.price_norm_factor
104103

105104
# Build the training dataset.
106-
train = (
107-
automobile_data.make_dataset(train_x, train_y)
108-
# Shuffling with a buffer larger than the data set ensures
109-
# that the examples are well mixed.
110-
.shuffle(1000).batch(args.batch_size)
111-
# Repeat forever
112-
.repeat())
105+
train = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
113106

114107
# Build the validation dataset.
115-
test = automobile_data.make_dataset(test_x, test_y).batch(args.batch_size)
108+
test = automobile_data.make_dataset(args.batch_size, test_x, test_y)
116109

117110
# The first way assigns a unique weight to each category. To do this you must
118111
# specify the category's vocabulary (values outside this specification will
@@ -151,10 +144,10 @@ def main(argv):
151144
})
152145

153146
# Train the model.
154-
model.train(input_fn=from_dataset(train), steps=args.train_steps)
147+
model.train(input_fn=automobile_data.from_dataset(train), steps=args.train_steps)
155148

156149
# Evaluate how the model performs on data it has not yet seen.
157-
eval_result = model.evaluate(input_fn=from_dataset(test))
150+
eval_result = model.evaluate(input_fn=automobile_data.from_dataset(test))
158151

159152
# Print the Root Mean Square Error (RMSE).
160153
print("\n" + 80 * "*")

samples/cookbook/regression/dnn_regression.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
help='price normalization factor')
3333

3434

35-
def from_dataset(ds):
36-
return lambda: ds.make_one_shot_iterator().get_next()
37-
38-
3935
def main(argv):
4036
"""Builds, trains, and evaluates the model."""
4137
args = parser.parse_args(argv[1:])
@@ -46,16 +42,10 @@ def main(argv):
4642
test_y /= args.price_norm_factor
4743

4844
# Build the training dataset.
49-
train = (
50-
automobile_data.make_dataset(train_x, train_y)
51-
# Shuffling with a buffer larger than the data set ensures
52-
# that the examples are well mixed.
53-
.shuffle(1000).batch(args.batch_size)
54-
# Repeat forever
55-
.repeat())
45+
train = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
5646

5747
# Build the validation dataset.
58-
test = automobile_data.make_dataset(test_x, test_y).batch(args.batch_size)
48+
test = automobile_data.make_dataset(args.batch_size, test_x, test_y)
5949

6050
# Use the same categorical columns as in `linear_regression_categorical`
6151
body_style_vocab = ["hardtop", "wagon", "sedan", "hatchback", "convertible"]
@@ -84,10 +74,10 @@ def main(argv):
8474

8575
# Train the model.
8676
# By default, the Estimators log output every 100 steps.
87-
model.train(input_fn=from_dataset(train), steps=args.train_steps)
77+
model.train(input_fn=automobile_data.from_dataset(train), steps=args.train_steps)
8878

8979
# Evaluate how the model performs on data it has not yet seen.
90-
eval_result = model.evaluate(input_fn=from_dataset(test))
80+
eval_result = model.evaluate(input_fn=automobile_data.from_dataset(test))
9181

9282
# The evaluation returns a Python dictionary. The "average_loss" key holds the
9383
# Mean Squared Error (MSE).

samples/cookbook/regression/linear_regression.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
help='price normalization factor')
3434

3535

36-
def from_dataset(ds):
37-
return lambda: ds.make_one_shot_iterator().get_next()
38-
39-
4036
def main(argv):
4137
"""Builds, trains, and evaluates the model."""
4238
args = parser.parse_args(argv[1:])
@@ -47,16 +43,10 @@ def main(argv):
4743
test_y /= args.price_norm_factor
4844

4945
# Build the training dataset.
50-
train = (
51-
automobile_data.make_dataset(train_x, train_y)
52-
# Shuffling with a buffer larger than the data set ensures
53-
# that the examples are well mixed.
54-
.shuffle(1000).batch(args.batch_size)
55-
# Repeat forever
56-
.repeat())
46+
train = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
5747

5848
# Build the validation dataset.
59-
test = automobile_data.make_dataset(test_x, test_y).batch(args.batch_size)
49+
test = automobile_data.make_dataset(args.batch_size, test_x, test_y)
6050

6151
feature_columns = [
6252
# "curb-weight" and "highway-mpg" are numeric columns.
@@ -69,10 +59,10 @@ def main(argv):
6959

7060
# Train the model.
7161
# By default, the Estimators log output every 100 steps.
72-
model.train(input_fn=from_dataset(train), steps=args.train_steps)
62+
model.train(input_fn=automobile_data.from_dataset(train), steps=args.train_steps)
7363

7464
# Evaluate how the model performs on data it has not yet seen.
75-
eval_result = model.evaluate(input_fn=from_dataset(test))
65+
eval_result = model.evaluate(input_fn=automobile_data.from_dataset(test))
7666

7767
# The evaluation returns a Python dictionary. The "average_loss" key holds the
7868
# Mean Squared Error (MSE).
@@ -88,8 +78,9 @@ def main(argv):
8878
"curb-weight": np.array([2000, 3000]),
8979
"highway-mpg": np.array([30, 40])
9080
}
91-
predict = automobile_data.make_dataset(input_dict).batch(1)
92-
predict_results = model.predict(input_fn=from_dataset(predict))
81+
82+
predict = automobile_data.make_dataset(1, input_dict)
83+
predict_results = model.predict(input_fn=automobile_data.from_dataset(predict))
9384

9485
# Print the prediction results.
9586
print("\nPrediction results:")

samples/cookbook/regression/linear_regression_categorical.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
help='price normalization factor')
3333

3434

35-
def from_dataset(ds):
36-
return lambda: ds.make_one_shot_iterator().get_next()
37-
38-
3935
def main(argv):
4036
"""Builds, trains, and evaluates the model."""
4137
args = parser.parse_args(argv[1:])
@@ -46,16 +42,10 @@ def main(argv):
4642
test_y /= args.price_norm_factor
4743

4844
# Build the training dataset.
49-
train = (
50-
automobile_data.make_dataset(train_x, train_y)
51-
# Shuffling with a buffer larger than the data set ensures
52-
# that the examples are well mixed.
53-
.shuffle(1000).batch(args.batch_size)
54-
# Repeat forever
55-
.repeat())
45+
train = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
5646

5747
# Build the validation dataset.
58-
test = automobile_data.make_dataset(test_x, test_y).batch(args.batch_size)
48+
test = automobile_data.make_dataset(args.batch_size, test_x, test_y)
5949

6050
# The following code demonstrates two of the ways that `feature_columns` can
6151
# be used to build a model with categorical inputs.
@@ -93,10 +83,10 @@ def main(argv):
9383

9484
# Train the model.
9585
# By default, the Estimators log output every 100 steps.
96-
model.train(input_fn=from_dataset(train), steps=args.train_steps)
86+
model.train(input_fn=automobile_data.from_dataset(train), steps=args.train_steps)
9787

9888
# Evaluate how the model performs on data it has not yet seen.
99-
eval_result = model.evaluate(input_fn=from_dataset(test))
89+
eval_result = model.evaluate(input_fn=automobile_data.from_dataset(test))
10090

10191
# The evaluation returns a Python dictionary. The "average_loss" key holds the
10292
# Mean Squared Error (MSE).

0 commit comments

Comments
 (0)