Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
4cfb259f
Commit
4cfb259f
authored
Apr 25, 2018
by
XinyueZ
Browse files
Removed from_dataset(), let make_dataset() return input_fn as provider of dataset
parent
7a34628e
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
37 deletions
+39
-37
samples/cookbook/regression/automobile_data.py
samples/cookbook/regression/automobile_data.py
+13
-12
samples/cookbook/regression/custom_regression.py
samples/cookbook/regression/custom_regression.py
+5
-5
samples/cookbook/regression/dnn_regression.py
samples/cookbook/regression/dnn_regression.py
+6
-6
samples/cookbook/regression/linear_regression.py
samples/cookbook/regression/linear_regression.py
+9
-8
samples/cookbook/regression/linear_regression_categorical.py
samples/cookbook/regression/linear_regression_categorical.py
+6
-6
No files found.
samples/cookbook/regression/automobile_data.py
View file @
4cfb259f
...
@@ -110,11 +110,10 @@ def load_data(y_name="price", train_fraction=0.7, seed=None):
...
@@ -110,11 +110,10 @@ def load_data(y_name="price", train_fraction=0.7, seed=None):
return
(
x_train
,
y_train
),
(
x_test
,
y_test
)
return
(
x_train
,
y_train
),
(
x_test
,
y_test
)
def
from_dataset
(
dataset
):
return
lambda
:
dataset
.
make_one_shot_iterator
().
get_next
()
def
make_dataset
(
batch_sz
,
x
,
y
=
None
,
shuffle
=
False
,
shuffle_buffer_size
=
1000
):
def
make_dataset
(
batch_sz
,
x
,
y
=
None
,
shuffle
=
False
,
shuffle_buffer_size
=
1000
):
"""Create a slice Dataset from a pandas DataFrame and labels"""
"""Create a slice Dataset from a pandas DataFrame and labels"""
def
input_fn
():
if
y
is
not
None
:
if
y
is
not
None
:
dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
dict
(
x
),
y
))
dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
dict
(
x
),
y
))
else
:
else
:
...
@@ -123,4 +122,6 @@ def make_dataset(batch_sz, x, y=None, shuffle=False, shuffle_buffer_size=1000):
...
@@ -123,4 +122,6 @@ def make_dataset(batch_sz, x, y=None, shuffle=False, shuffle_buffer_size=1000):
dataset
=
dataset
.
shuffle
(
shuffle_buffer_size
).
batch
(
batch_sz
).
repeat
()
dataset
=
dataset
.
shuffle
(
shuffle_buffer_size
).
batch
(
batch_sz
).
repeat
()
else
:
else
:
dataset
=
dataset
.
batch
(
batch_sz
)
dataset
=
dataset
.
batch
(
batch_sz
)
return
dataset
return
dataset
.
make_one_shot_iterator
().
get_next
()
return
input_fn
samples/cookbook/regression/custom_regression.py
View file @
4cfb259f
...
@@ -101,11 +101,11 @@ def main(argv):
...
@@ -101,11 +101,11 @@ def main(argv):
train_y
/=
args
.
price_norm_factor
train_y
/=
args
.
price_norm_factor
test_y
/=
args
.
price_norm_factor
test_y
/=
args
.
price_norm_factor
#
Build
the training dataset.
#
Provide
the training
input
dataset.
train
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
train_x
,
train_y
,
True
,
1000
)
train
_input_fn
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
train_x
,
train_y
,
True
,
1000
)
# Build the validation dataset.
# Build the validation dataset.
test
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
test_x
,
test_y
)
test
_input_fn
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
test_x
,
test_y
)
# The first way assigns a unique weight to each category. To do this you must
# The first way assigns a unique weight to each category. To do this you must
# specify the category's vocabulary (values outside this specification will
# specify the category's vocabulary (values outside this specification will
...
@@ -144,10 +144,10 @@ def main(argv):
...
@@ -144,10 +144,10 @@ def main(argv):
})
})
# Train the model.
# Train the model.
model
.
train
(
input_fn
=
automobile_data
.
from_dataset
(
train
)
,
steps
=
args
.
train_steps
)
model
.
train
(
input_fn
=
train_input_fn
,
steps
=
args
.
train_steps
)
# Evaluate how the model performs on data it has not yet seen.
# Evaluate how the model performs on data it has not yet seen.
eval_result
=
model
.
evaluate
(
input_fn
=
automobile_data
.
from_dataset
(
test
)
)
eval_result
=
model
.
evaluate
(
input_fn
=
test_input_fn
)
# Print the Root Mean Square Error (RMSE).
# Print the Root Mean Square Error (RMSE).
print
(
"
\n
"
+
80
*
"*"
)
print
(
"
\n
"
+
80
*
"*"
)
...
...
samples/cookbook/regression/dnn_regression.py
View file @
4cfb259f
...
@@ -41,11 +41,11 @@ def main(argv):
...
@@ -41,11 +41,11 @@ def main(argv):
train_y
/=
args
.
price_norm_factor
train_y
/=
args
.
price_norm_factor
test_y
/=
args
.
price_norm_factor
test_y
/=
args
.
price_norm_factor
#
Build
the training dataset.
#
Provide
the training
input
dataset.
train
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
train_x
,
train_y
,
True
,
1000
)
train
_input_fn
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
train_x
,
train_y
,
True
,
1000
)
#
Build
the validation dataset.
#
Provide
the validation
input
dataset.
test
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
test_x
,
test_y
)
test
_input_fn
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
test_x
,
test_y
)
# Use the same categorical columns as in `linear_regression_categorical`
# Use the same categorical columns as in `linear_regression_categorical`
body_style_vocab
=
[
"hardtop"
,
"wagon"
,
"sedan"
,
"hatchback"
,
"convertible"
]
body_style_vocab
=
[
"hardtop"
,
"wagon"
,
"sedan"
,
"hatchback"
,
"convertible"
]
...
@@ -74,10 +74,10 @@ def main(argv):
...
@@ -74,10 +74,10 @@ def main(argv):
# Train the model.
# Train the model.
# By default, the Estimators log output every 100 steps.
# By default, the Estimators log output every 100 steps.
model
.
train
(
input_fn
=
automobile_data
.
from_dataset
(
train
)
,
steps
=
args
.
train_steps
)
model
.
train
(
input_fn
=
train_input_fn
,
steps
=
args
.
train_steps
)
# Evaluate how the model performs on data it has not yet seen.
# Evaluate how the model performs on data it has not yet seen.
eval_result
=
model
.
evaluate
(
input_fn
=
automobile_data
.
from_dataset
(
test
)
)
eval_result
=
model
.
evaluate
(
input_fn
=
test_input_fn
)
# The evaluation returns a Python dictionary. The "average_loss" key holds the
# The evaluation returns a Python dictionary. The "average_loss" key holds the
# Mean Squared Error (MSE).
# Mean Squared Error (MSE).
...
...
samples/cookbook/regression/linear_regression.py
View file @
4cfb259f
...
@@ -42,11 +42,11 @@ def main(argv):
...
@@ -42,11 +42,11 @@ def main(argv):
train_y
/=
args
.
price_norm_factor
train_y
/=
args
.
price_norm_factor
test_y
/=
args
.
price_norm_factor
test_y
/=
args
.
price_norm_factor
#
Build
the training dataset.
#
Provide
the training
input
dataset.
train
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
train_x
,
train_y
,
True
,
1000
)
train
_input_fn
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
train_x
,
train_y
,
True
,
1000
)
#
Build
the validation dataset.
#
Provide
the validation
input
dataset.
test
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
test_x
,
test_y
)
test
_input_fn
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
test_x
,
test_y
)
feature_columns
=
[
feature_columns
=
[
# "curb-weight" and "highway-mpg" are numeric columns.
# "curb-weight" and "highway-mpg" are numeric columns.
...
@@ -59,10 +59,10 @@ def main(argv):
...
@@ -59,10 +59,10 @@ def main(argv):
# Train the model.
# Train the model.
# By default, the Estimators log output every 100 steps.
# By default, the Estimators log output every 100 steps.
model
.
train
(
input_fn
=
automobile_data
.
from_dataset
(
train
)
,
steps
=
args
.
train_steps
)
model
.
train
(
input_fn
=
train_input_fn
,
steps
=
args
.
train_steps
)
# Evaluate how the model performs on data it has not yet seen.
# Evaluate how the model performs on data it has not yet seen.
eval_result
=
model
.
evaluate
(
input_fn
=
automobile_data
.
from_dataset
(
test
)
)
eval_result
=
model
.
evaluate
(
input_fn
=
test_input_fn
)
# The evaluation returns a Python dictionary. The "average_loss" key holds the
# The evaluation returns a Python dictionary. The "average_loss" key holds the
# Mean Squared Error (MSE).
# Mean Squared Error (MSE).
...
@@ -79,8 +79,9 @@ def main(argv):
...
@@ -79,8 +79,9 @@ def main(argv):
"highway-mpg"
:
np
.
array
([
30
,
40
])
"highway-mpg"
:
np
.
array
([
30
,
40
])
}
}
predict
=
automobile_data
.
make_dataset
(
1
,
input_dict
)
# Provide the predict input dataset.
predict_results
=
model
.
predict
(
input_fn
=
automobile_data
.
from_dataset
(
predict
))
predict_input_fn
=
automobile_data
.
make_dataset
(
1
,
input_dict
)
predict_results
=
model
.
predict
(
input_fn
=
predict_input_fn
)
# Print the prediction results.
# Print the prediction results.
print
(
"
\n
Prediction results:"
)
print
(
"
\n
Prediction results:"
)
...
...
samples/cookbook/regression/linear_regression_categorical.py
View file @
4cfb259f
...
@@ -41,11 +41,11 @@ def main(argv):
...
@@ -41,11 +41,11 @@ def main(argv):
train_y
/=
args
.
price_norm_factor
train_y
/=
args
.
price_norm_factor
test_y
/=
args
.
price_norm_factor
test_y
/=
args
.
price_norm_factor
#
Build
the training dataset.
#
Provide
the training
input
dataset.
train
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
train_x
,
train_y
,
True
,
1000
)
train
_input_fn
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
train_x
,
train_y
,
True
,
1000
)
#
Build
the validation dataset.
#
Provide
the validation
input
dataset.
test
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
test_x
,
test_y
)
test
_input_fn
=
automobile_data
.
make_dataset
(
args
.
batch_size
,
test_x
,
test_y
)
# The following code demonstrates two of the ways that `feature_columns` can
# The following code demonstrates two of the ways that `feature_columns` can
# be used to build a model with categorical inputs.
# be used to build a model with categorical inputs.
...
@@ -83,10 +83,10 @@ def main(argv):
...
@@ -83,10 +83,10 @@ def main(argv):
# Train the model.
# Train the model.
# By default, the Estimators log output every 100 steps.
# By default, the Estimators log output every 100 steps.
model
.
train
(
input_fn
=
automobile_data
.
from_dataset
(
train
)
,
steps
=
args
.
train_steps
)
model
.
train
(
input_fn
=
train_input_fn
,
steps
=
args
.
train_steps
)
# Evaluate how the model performs on data it has not yet seen.
# Evaluate how the model performs on data it has not yet seen.
eval_result
=
model
.
evaluate
(
input_fn
=
automobile_data
.
from_dataset
(
test
)
)
eval_result
=
model
.
evaluate
(
input_fn
=
test_input_fn
)
# The evaluation returns a Python dictionary. The "average_loss" key holds the
# The evaluation returns a Python dictionary. The "average_loss" key holds the
# Mean Squared Error (MSE).
# Mean Squared Error (MSE).
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment