Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cfb259

Browse files
committed
Removed from_dataset(), let make_dataset() return input_fn as provider of dataset
1 parent 7a34628 commit 4cfb259

File tree

5 files changed

+39
-37
lines changed

5 files changed

+39
-37
lines changed

samples/cookbook/regression/automobile_data.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,18 @@ def load_data(y_name="price", train_fraction=0.7, seed=None):
110110
return (x_train, y_train), (x_test, y_test)
111111

112112

113-
def from_dataset(dataset): return lambda: dataset.make_one_shot_iterator().get_next()
114-
115-
116113
def make_dataset(batch_sz, x, y=None, shuffle=False, shuffle_buffer_size=1000):
117114
"""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
115+
116+
def input_fn():
117+
if y is not None:
118+
dataset = tf.data.Dataset.from_tensor_slices((dict(x), y))
119+
else:
120+
dataset = tf.data.Dataset.from_tensor_slices(dict(x))
121+
if shuffle:
122+
dataset = dataset.shuffle(shuffle_buffer_size).batch(batch_sz).repeat()
123+
else:
124+
dataset = dataset.batch(batch_sz)
125+
return dataset.make_one_shot_iterator().get_next()
126+
127+
return input_fn

samples/cookbook/regression/custom_regression.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ def main(argv):
101101
train_y /= args.price_norm_factor
102102
test_y /= args.price_norm_factor
103103

104-
# Build the training dataset.
105-
train = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
104+
# Provide the training input dataset.
105+
train_input_fn = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
106106

107107
# Build the validation dataset.
108-
test = automobile_data.make_dataset(args.batch_size, test_x, test_y)
108+
test_input_fn = automobile_data.make_dataset(args.batch_size, test_x, test_y)
109109

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

146146
# Train the model.
147-
model.train(input_fn=automobile_data.from_dataset(train), steps=args.train_steps)
147+
model.train(input_fn=train_input_fn, steps=args.train_steps)
148148

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

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

samples/cookbook/regression/dnn_regression.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def main(argv):
4141
train_y /= args.price_norm_factor
4242
test_y /= args.price_norm_factor
4343

44-
# Build the training dataset.
45-
train = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
44+
# Provide the training input dataset.
45+
train_input_fn = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
4646

47-
# Build the validation dataset.
48-
test = automobile_data.make_dataset(args.batch_size, test_x, test_y)
47+
# Provide the validation input dataset.
48+
test_input_fn = automobile_data.make_dataset(args.batch_size, test_x, test_y)
4949

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

7575
# Train the model.
7676
# By default, the Estimators log output every 100 steps.
77-
model.train(input_fn=automobile_data.from_dataset(train), steps=args.train_steps)
77+
model.train(input_fn=train_input_fn, steps=args.train_steps)
7878

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

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

samples/cookbook/regression/linear_regression.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def main(argv):
4242
train_y /= args.price_norm_factor
4343
test_y /= args.price_norm_factor
4444

45-
# Build the training dataset.
46-
train = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
45+
# Provide the training input dataset.
46+
train_input_fn = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
4747

48-
# Build the validation dataset.
49-
test = automobile_data.make_dataset(args.batch_size, test_x, test_y)
48+
# Provide the validation input dataset.
49+
test_input_fn = automobile_data.make_dataset(args.batch_size, test_x, test_y)
5050

5151
feature_columns = [
5252
# "curb-weight" and "highway-mpg" are numeric columns.
@@ -59,10 +59,10 @@ def main(argv):
5959

6060
# Train the model.
6161
# By default, the Estimators log output every 100 steps.
62-
model.train(input_fn=automobile_data.from_dataset(train), steps=args.train_steps)
62+
model.train(input_fn=train_input_fn, steps=args.train_steps)
6363

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

6767
# The evaluation returns a Python dictionary. The "average_loss" key holds the
6868
# Mean Squared Error (MSE).
@@ -79,8 +79,9 @@ def main(argv):
7979
"highway-mpg": np.array([30, 40])
8080
}
8181

82-
predict = automobile_data.make_dataset(1, input_dict)
83-
predict_results = model.predict(input_fn=automobile_data.from_dataset(predict))
82+
# Provide the predict input dataset.
83+
predict_input_fn = automobile_data.make_dataset(1, input_dict)
84+
predict_results = model.predict(input_fn=predict_input_fn)
8485

8586
# Print the prediction results.
8687
print("\nPrediction results:")

samples/cookbook/regression/linear_regression_categorical.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def main(argv):
4141
train_y /= args.price_norm_factor
4242
test_y /= args.price_norm_factor
4343

44-
# Build the training dataset.
45-
train = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
44+
# Provide the training input dataset.
45+
train_input_fn = automobile_data.make_dataset(args.batch_size, train_x, train_y, True, 1000)
4646

47-
# Build the validation dataset.
48-
test = automobile_data.make_dataset(args.batch_size, test_x, test_y)
47+
# Provide the validation input dataset.
48+
test_input_fn = automobile_data.make_dataset(args.batch_size, test_x, test_y)
4949

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

8484
# Train the model.
8585
# By default, the Estimators log output every 100 steps.
86-
model.train(input_fn=automobile_data.from_dataset(train), steps=args.train_steps)
86+
model.train(input_fn=train_input_fn, steps=args.train_steps)
8787

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

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

0 commit comments

Comments
 (0)