Delete out-of-date samples.
PiperOrigin-RevId: 219193262
This commit is contained in:
parent
98fb3b9243
commit
1b114bf111
@ -1,25 +0,0 @@
|
||||
# Description:
|
||||
# Examples of adding a benchmark to TensorFlow.
|
||||
|
||||
load(
|
||||
"//tensorflow/tools/test:performance.bzl",
|
||||
"tf_py_logged_benchmark",
|
||||
)
|
||||
|
||||
licenses(["notice"]) # Apache 2.0
|
||||
|
||||
exports_files(["LICENSE"])
|
||||
|
||||
py_test(
|
||||
name = "sample_benchmark",
|
||||
srcs = ["sample_benchmark.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
],
|
||||
)
|
||||
|
||||
tf_py_logged_benchmark(
|
||||
name = "sample_logged_benchmark",
|
||||
target = "//tensorflow/examples/benchmark:sample_benchmark",
|
||||
)
|
@ -1,50 +0,0 @@
|
||||
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""Sample TensorFlow benchmark."""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import time
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
# Define a class that extends from tf.test.Benchmark.
|
||||
class SampleBenchmark(tf.test.Benchmark):
|
||||
|
||||
# Note: benchmark method name must start with `benchmark`.
|
||||
def benchmarkSum(self):
|
||||
with tf.Session() as sess:
|
||||
x = tf.constant(10)
|
||||
y = tf.constant(5)
|
||||
result = tf.add(x, y)
|
||||
|
||||
iters = 100
|
||||
start_time = time.time()
|
||||
for _ in range(iters):
|
||||
sess.run(result)
|
||||
total_wall_time = time.time() - start_time
|
||||
|
||||
# Call report_benchmark to report a metric value.
|
||||
self.report_benchmark(
|
||||
name="sum_wall_time",
|
||||
# This value should always be per iteration.
|
||||
wall_time=total_wall_time/iters,
|
||||
iters=iters)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
tf.test.main()
|
@ -1,106 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""Linear regression using the LinearRegressor Estimator."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
import imports85 # pylint: disable=g-bad-import-order
|
||||
|
||||
STEPS = 1000
|
||||
PRICE_NORM_FACTOR = 1000
|
||||
|
||||
|
||||
def main(argv):
|
||||
"""Builds, trains, and evaluates the model."""
|
||||
assert len(argv) == 1
|
||||
(train, test) = imports85.dataset()
|
||||
|
||||
# Switch the labels to units of thousands for better convergence.
|
||||
def to_thousands(features, labels):
|
||||
return features, labels / PRICE_NORM_FACTOR
|
||||
|
||||
train = train.map(to_thousands)
|
||||
test = test.map(to_thousands)
|
||||
|
||||
# Build the training input_fn.
|
||||
def input_train():
|
||||
return (
|
||||
# Shuffling with a buffer larger than the data set ensures
|
||||
# that the examples are well mixed.
|
||||
train.shuffle(1000).batch(128)
|
||||
# Repeat forever
|
||||
.repeat().make_one_shot_iterator().get_next())
|
||||
|
||||
# Build the validation input_fn.
|
||||
def input_test():
|
||||
return (test.shuffle(1000).batch(128)
|
||||
.make_one_shot_iterator().get_next())
|
||||
|
||||
feature_columns = [
|
||||
# "curb-weight" and "highway-mpg" are numeric columns.
|
||||
tf.feature_column.numeric_column(key="curb-weight"),
|
||||
tf.feature_column.numeric_column(key="highway-mpg"),
|
||||
]
|
||||
|
||||
# Build the Estimator.
|
||||
model = tf.estimator.LinearRegressor(feature_columns=feature_columns)
|
||||
|
||||
# Train the model.
|
||||
# By default, the Estimators log output every 100 steps.
|
||||
model.train(input_fn=input_train, steps=STEPS)
|
||||
|
||||
# Evaluate how the model performs on data it has not yet seen.
|
||||
eval_result = model.evaluate(input_fn=input_test)
|
||||
|
||||
# The evaluation returns a Python dictionary. The "average_loss" key holds the
|
||||
# Mean Squared Error (MSE).
|
||||
average_loss = eval_result["average_loss"]
|
||||
|
||||
# Convert MSE to Root Mean Square Error (RMSE).
|
||||
print("\n" + 80 * "*")
|
||||
print("\nRMS error for the test set: ${:.0f}"
|
||||
.format(PRICE_NORM_FACTOR * average_loss**0.5))
|
||||
|
||||
# Run the model in prediction mode.
|
||||
input_dict = {
|
||||
"curb-weight": np.array([2000, 3000]),
|
||||
"highway-mpg": np.array([30, 40])
|
||||
}
|
||||
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
input_dict, shuffle=False)
|
||||
predict_results = model.predict(input_fn=predict_input_fn)
|
||||
|
||||
# Print the prediction results.
|
||||
print("\nPrediction results:")
|
||||
for i, prediction in enumerate(predict_results):
|
||||
msg = ("Curb weight: {: 4d}lbs, "
|
||||
"Highway: {: 0d}mpg, "
|
||||
"Prediction: ${: 9.2f}")
|
||||
msg = msg.format(input_dict["curb-weight"][i], input_dict["highway-mpg"][i],
|
||||
PRICE_NORM_FACTOR * prediction["predictions"][0])
|
||||
|
||||
print(" " + msg)
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# The Estimator periodically generates "INFO" logs; make these logs visible.
|
||||
tf.logging.set_verbosity(tf.logging.INFO)
|
||||
tf.app.run(main=main)
|
@ -28,29 +28,3 @@ py_binary(
|
||||
"//tensorflow/examples/tutorials/mnist",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "fully_connected_preloaded",
|
||||
srcs = [
|
||||
"fully_connected_preloaded.py",
|
||||
],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//tensorflow/examples/tutorials/mnist",
|
||||
"//tensorflow/examples/tutorials/mnist:input_data",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "fully_connected_preloaded_var",
|
||||
srcs = [
|
||||
"fully_connected_preloaded_var.py",
|
||||
],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//tensorflow/examples/tutorials/mnist",
|
||||
"//tensorflow/examples/tutorials/mnist:input_data",
|
||||
],
|
||||
)
|
||||
|
@ -1,189 +0,0 @@
|
||||
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
|
||||
"""Trains the MNIST network using preloaded data in a constant.
|
||||
|
||||
Run using bazel:
|
||||
|
||||
bazel run --config opt \
|
||||
<...>/tensorflow/examples/how_tos/reading_data:fully_connected_preloaded
|
||||
|
||||
or, if installed via pip:
|
||||
|
||||
cd tensorflow/examples/how_tos/reading_data
|
||||
python fully_connected_preloaded.py
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import time
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
from tensorflow.examples.tutorials.mnist import mnist
|
||||
|
||||
# Basic model parameters as external flags.
|
||||
FLAGS = None
|
||||
|
||||
|
||||
def run_training():
|
||||
"""Train MNIST for a number of epochs."""
|
||||
# Get the sets of images and labels for training, validation, and
|
||||
# test on MNIST.
|
||||
data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)
|
||||
|
||||
# Tell TensorFlow that the model will be built into the default Graph.
|
||||
with tf.Graph().as_default():
|
||||
with tf.name_scope('input'):
|
||||
# Input data, pin to CPU because rest of pipeline is CPU-only
|
||||
with tf.device('/cpu:0'):
|
||||
input_images = tf.constant(data_sets.train.images)
|
||||
input_labels = tf.constant(data_sets.train.labels)
|
||||
|
||||
image, label = tf.train.slice_input_producer(
|
||||
[input_images, input_labels], num_epochs=FLAGS.num_epochs)
|
||||
label = tf.cast(label, tf.int32)
|
||||
images, labels = tf.train.batch(
|
||||
[image, label], batch_size=FLAGS.batch_size)
|
||||
|
||||
# Build a Graph that computes predictions from the inference model.
|
||||
logits = mnist.inference(images, FLAGS.hidden1, FLAGS.hidden2)
|
||||
|
||||
# Add to the Graph the Ops for loss calculation.
|
||||
loss = mnist.loss(logits, labels)
|
||||
|
||||
# Add to the Graph the Ops that calculate and apply gradients.
|
||||
train_op = mnist.training(loss, FLAGS.learning_rate)
|
||||
|
||||
# Add the Op to compare the logits to the labels during evaluation.
|
||||
eval_correct = mnist.evaluation(logits, labels)
|
||||
|
||||
# Build the summary operation based on the TF collection of Summaries.
|
||||
summary_op = tf.summary.merge_all()
|
||||
|
||||
# Create a saver for writing training checkpoints.
|
||||
saver = tf.train.Saver()
|
||||
|
||||
# Create the op for initializing variables.
|
||||
init_op = tf.group(tf.global_variables_initializer(),
|
||||
tf.local_variables_initializer())
|
||||
# Create a session for running Ops on the Graph.
|
||||
sess = tf.Session()
|
||||
|
||||
# Run the Op to initialize the variables.
|
||||
sess.run(init_op)
|
||||
|
||||
# Instantiate a SummaryWriter to output summaries and the Graph.
|
||||
summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph)
|
||||
|
||||
# Start input enqueue threads.
|
||||
coord = tf.train.Coordinator()
|
||||
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
|
||||
|
||||
# And then after everything is built, start the training loop.
|
||||
try:
|
||||
step = 0
|
||||
while not coord.should_stop():
|
||||
start_time = time.time()
|
||||
|
||||
# Run one step of the model.
|
||||
_, loss_value = sess.run([train_op, loss])
|
||||
|
||||
duration = time.time() - start_time
|
||||
|
||||
# Write the summaries and print an overview fairly often.
|
||||
if step % 100 == 0:
|
||||
# Print status to stdout.
|
||||
print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value,
|
||||
duration))
|
||||
# Update the events file.
|
||||
summary_str = sess.run(summary_op)
|
||||
summary_writer.add_summary(summary_str, step)
|
||||
step += 1
|
||||
|
||||
# Save a checkpoint periodically.
|
||||
if (step + 1) % 1000 == 0:
|
||||
print('Saving')
|
||||
saver.save(sess, FLAGS.train_dir, global_step=step)
|
||||
|
||||
step += 1
|
||||
except tf.errors.OutOfRangeError:
|
||||
print('Saving')
|
||||
saver.save(sess, FLAGS.train_dir, global_step=step)
|
||||
print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))
|
||||
finally:
|
||||
# When done, ask the threads to stop.
|
||||
coord.request_stop()
|
||||
|
||||
# Wait for threads to finish.
|
||||
coord.join(threads)
|
||||
sess.close()
|
||||
|
||||
|
||||
def main(_):
|
||||
run_training()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--learning_rate',
|
||||
type=float,
|
||||
default=0.01,
|
||||
help='Initial learning rate.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--num_epochs',
|
||||
type=int,
|
||||
default=2,
|
||||
help='Number of epochs to run trainer.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hidden1',
|
||||
type=int,
|
||||
default=128,
|
||||
help='Number of units in hidden layer 1.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hidden2',
|
||||
type=int,
|
||||
default=32,
|
||||
help='Number of units in hidden layer 2.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--batch_size',
|
||||
type=int,
|
||||
default=100,
|
||||
help='Batch size. Must divide evenly into the dataset sizes.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--train_dir',
|
||||
type=str,
|
||||
default='/tmp/data',
|
||||
help='Directory to put the training data.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--fake_data',
|
||||
default=False,
|
||||
help='If true, uses fake data for unit testing.',
|
||||
action='store_true'
|
||||
)
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,200 +0,0 @@
|
||||
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
|
||||
"""Trains the MNIST network using preloaded data stored in a variable.
|
||||
|
||||
Run using bazel:
|
||||
|
||||
bazel run --config opt \
|
||||
<...>/tensorflow/examples/how_tos/reading_data:fully_connected_preloaded_var
|
||||
|
||||
or, if installed via pip:
|
||||
|
||||
cd tensorflow/examples/how_tos/reading_data
|
||||
python fully_connected_preloaded_var.py
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import time
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
from tensorflow.examples.tutorials.mnist import mnist
|
||||
|
||||
# Basic model parameters as external flags.
|
||||
FLAGS = None
|
||||
|
||||
|
||||
def run_training():
|
||||
"""Train MNIST for a number of epochs."""
|
||||
# Get the sets of images and labels for training, validation, and
|
||||
# test on MNIST.
|
||||
data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data)
|
||||
|
||||
# Tell TensorFlow that the model will be built into the default Graph.
|
||||
with tf.Graph().as_default():
|
||||
with tf.name_scope('input'):
|
||||
# Input data
|
||||
images_initializer = tf.placeholder(
|
||||
dtype=data_sets.train.images.dtype,
|
||||
shape=data_sets.train.images.shape)
|
||||
labels_initializer = tf.placeholder(
|
||||
dtype=data_sets.train.labels.dtype,
|
||||
shape=data_sets.train.labels.shape)
|
||||
input_images = tf.Variable(
|
||||
images_initializer, trainable=False, collections=[])
|
||||
input_labels = tf.Variable(
|
||||
labels_initializer, trainable=False, collections=[])
|
||||
|
||||
image, label = tf.train.slice_input_producer(
|
||||
[input_images, input_labels], num_epochs=FLAGS.num_epochs)
|
||||
label = tf.cast(label, tf.int32)
|
||||
images, labels = tf.train.batch(
|
||||
[image, label], batch_size=FLAGS.batch_size)
|
||||
|
||||
# Build a Graph that computes predictions from the inference model.
|
||||
logits = mnist.inference(images, FLAGS.hidden1, FLAGS.hidden2)
|
||||
|
||||
# Add to the Graph the Ops for loss calculation.
|
||||
loss = mnist.loss(logits, labels)
|
||||
|
||||
# Add to the Graph the Ops that calculate and apply gradients.
|
||||
train_op = mnist.training(loss, FLAGS.learning_rate)
|
||||
|
||||
# Add the Op to compare the logits to the labels during evaluation.
|
||||
eval_correct = mnist.evaluation(logits, labels)
|
||||
|
||||
# Build the summary operation based on the TF collection of Summaries.
|
||||
summary_op = tf.summary.merge_all()
|
||||
|
||||
# Create a saver for writing training checkpoints.
|
||||
saver = tf.train.Saver()
|
||||
|
||||
# Create the op for initializing variables.
|
||||
init_op = tf.group(tf.global_variables_initializer(),
|
||||
tf.local_variables_initializer())
|
||||
|
||||
# Create a session for running Ops on the Graph.
|
||||
sess = tf.Session()
|
||||
|
||||
# Run the Op to initialize the variables.
|
||||
sess.run(init_op)
|
||||
sess.run(input_images.initializer,
|
||||
feed_dict={images_initializer: data_sets.train.images})
|
||||
sess.run(input_labels.initializer,
|
||||
feed_dict={labels_initializer: data_sets.train.labels})
|
||||
|
||||
# Instantiate a SummaryWriter to output summaries and the Graph.
|
||||
summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph)
|
||||
|
||||
# Start input enqueue threads.
|
||||
coord = tf.train.Coordinator()
|
||||
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
|
||||
|
||||
# And then after everything is built, start the training loop.
|
||||
try:
|
||||
step = 0
|
||||
while not coord.should_stop():
|
||||
start_time = time.time()
|
||||
|
||||
# Run one step of the model.
|
||||
_, loss_value = sess.run([train_op, loss])
|
||||
|
||||
duration = time.time() - start_time
|
||||
|
||||
# Write the summaries and print an overview fairly often.
|
||||
if step % 100 == 0:
|
||||
# Print status to stdout.
|
||||
print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value,
|
||||
duration))
|
||||
# Update the events file.
|
||||
summary_str = sess.run(summary_op)
|
||||
summary_writer.add_summary(summary_str, step)
|
||||
step += 1
|
||||
|
||||
# Save a checkpoint periodically.
|
||||
if (step + 1) % 1000 == 0:
|
||||
print('Saving')
|
||||
saver.save(sess, FLAGS.train_dir, global_step=step)
|
||||
|
||||
step += 1
|
||||
except tf.errors.OutOfRangeError:
|
||||
print('Saving')
|
||||
saver.save(sess, FLAGS.train_dir, global_step=step)
|
||||
print('Done training for %d epochs, %d steps.' % (FLAGS.num_epochs, step))
|
||||
finally:
|
||||
# When done, ask the threads to stop.
|
||||
coord.request_stop()
|
||||
|
||||
# Wait for threads to finish.
|
||||
coord.join(threads)
|
||||
sess.close()
|
||||
|
||||
|
||||
def main(_):
|
||||
run_training()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--learning_rate',
|
||||
type=float,
|
||||
default=0.01,
|
||||
help='Initial learning rate.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--num_epochs',
|
||||
type=int,
|
||||
default=2,
|
||||
help='Number of epochs to run trainer.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hidden1',
|
||||
type=int,
|
||||
default=128,
|
||||
help='Number of units in hidden layer 1.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hidden2',
|
||||
type=int,
|
||||
default=32,
|
||||
help='Number of units in hidden layer 2.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--batch_size',
|
||||
type=int,
|
||||
default=100,
|
||||
help='Batch size. Must divide evenly into the dataset sizes.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--train_dir',
|
||||
type=str,
|
||||
default='/tmp/data',
|
||||
help='Directory to put the training data.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--fake_data',
|
||||
default=False,
|
||||
help='If true, uses fake data for unit testing.',
|
||||
action='store_true'
|
||||
)
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -9,30 +9,6 @@ licenses(["notice"]) # Apache 2.0
|
||||
|
||||
exports_files(["LICENSE"])
|
||||
|
||||
py_binary(
|
||||
name = "boston",
|
||||
srcs = ["boston.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = ["//tensorflow:tensorflow_py"],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "hdf5_classification",
|
||||
srcs = ["hdf5_classification.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//third_party/py/numpy",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "iris",
|
||||
srcs = ["iris.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = ["//tensorflow:tensorflow_py"],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "iris_custom_decay_dnn",
|
||||
srcs = ["iris_custom_decay_dnn.py"],
|
||||
@ -47,89 +23,6 @@ py_binary(
|
||||
deps = ["//tensorflow:tensorflow_py"],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "iris_run_config",
|
||||
srcs = ["iris_run_config.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = ["//tensorflow:tensorflow_py"],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "random_forest_mnist",
|
||||
srcs = ["random_forest_mnist.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//tensorflow/examples/tutorials/mnist:input_data",
|
||||
"//tensorflow/python:platform",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "resnet",
|
||||
srcs = ["resnet.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = ["//tensorflow:tensorflow_py"],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "text_classification",
|
||||
srcs = ["text_classification.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//tensorflow/contrib/layers:layers_py",
|
||||
"//third_party/py/numpy",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "text_classification_character_cnn",
|
||||
srcs = ["text_classification_character_cnn.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//third_party/py/numpy",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "text_classification_character_rnn",
|
||||
srcs = ["text_classification_character_rnn.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//third_party/py/numpy",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "text_classification_cnn",
|
||||
srcs = ["text_classification_cnn.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//third_party/py/numpy",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "mnist",
|
||||
srcs = ["mnist.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//third_party/py/numpy",
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "multiple_gpu",
|
||||
srcs = ["multiple_gpu.py"],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = ["//tensorflow:tensorflow_py"],
|
||||
)
|
||||
|
||||
sh_test(
|
||||
name = "examples_test",
|
||||
size = "large",
|
||||
|
@ -6,29 +6,11 @@ create, train, and use deep learning models easily.
|
||||
See the [Quickstart tutorial](https://www.tensorflow.org/get_started/estimator)
|
||||
for an introduction to the API.
|
||||
|
||||
To run most of these examples, you need to install the `scikit learn` library
|
||||
(`pip install -U scikit-learn`). Some examples use the `pandas` library for data
|
||||
processing (`pip install -U pandas`).
|
||||
|
||||
## Basics
|
||||
|
||||
* [Deep Neural Network Regression with Boston Data](https://www.tensorflow.org/code/tensorflow/examples/learn/boston.py)
|
||||
* [Deep Neural Network Classification with Iris Data](https://www.tensorflow.org/code/tensorflow/examples/learn/iris.py)
|
||||
* [Building a Custom Model](https://www.tensorflow.org/code/tensorflow/examples/learn/iris_custom_model.py)
|
||||
* [Building a Model Using Different GPU Configurations](https://www.tensorflow.org/code/tensorflow/examples/learn/iris_run_config.py)
|
||||
|
||||
## Techniques
|
||||
|
||||
* [Deep Neural Network with Customized Decay Function](https://www.tensorflow.org/code/tensorflow/examples/learn/iris_custom_decay_dnn.py)
|
||||
|
||||
## Specialized Models
|
||||
* [Building a Random Forest Model](https://www.tensorflow.org/code/tensorflow/examples/learn/random_forest_mnist.py)
|
||||
* [Building a Wide & Deep Model](https://github.com/tensorflow/models/tree/master/official/wide_deep/wide_deep.py)
|
||||
* [Building a Residual Network Model](https://www.tensorflow.org/code/tensorflow/examples/learn/resnet.py)
|
||||
|
||||
## Text classification
|
||||
|
||||
* [Text Classification Using Recurrent Neural Networks on Words](https://www.tensorflow.org/code/tensorflow/examples/learn/text_classification.py)
|
||||
* [Text Classification Using Convolutional Neural Networks on Words](https://www.tensorflow.org/code/tensorflow/examples/learn/text_classification_cnn.py)
|
||||
* [Text Classification Using Recurrent Neural Networks on Characters](https://www.tensorflow.org/code/tensorflow/examples/learn/text_classification_character_rnn.py)
|
||||
* [Text Classification Using Convolutional Neural Networks on Characters](https://www.tensorflow.org/code/tensorflow/examples/learn/text_classification_character_cnn.py)
|
||||
|
@ -1,71 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Example of DNNRegressor for Housing dataset."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
from sklearn import datasets
|
||||
from sklearn import metrics
|
||||
from sklearn import model_selection
|
||||
from sklearn import preprocessing
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
# Load dataset
|
||||
boston = datasets.load_boston()
|
||||
x, y = boston.data, boston.target
|
||||
|
||||
# Split dataset into train / test
|
||||
x_train, x_test, y_train, y_test = model_selection.train_test_split(
|
||||
x, y, test_size=0.2, random_state=42)
|
||||
|
||||
# Scale data (training set) to 0 mean and unit standard deviation.
|
||||
scaler = preprocessing.StandardScaler()
|
||||
x_train = scaler.fit_transform(x_train)
|
||||
|
||||
# Build 2 layer fully connected DNN with 10, 10 units respectively.
|
||||
feature_columns = [
|
||||
tf.feature_column.numeric_column('x', shape=np.array(x_train).shape[1:])]
|
||||
regressor = tf.estimator.DNNRegressor(
|
||||
feature_columns=feature_columns, hidden_units=[10, 10])
|
||||
|
||||
# Train.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={'x': x_train}, y=y_train, batch_size=1, num_epochs=None, shuffle=True)
|
||||
regressor.train(input_fn=train_input_fn, steps=2000)
|
||||
|
||||
# Predict.
|
||||
x_transformed = scaler.transform(x_test)
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={'x': x_transformed}, y=y_test, num_epochs=1, shuffle=False)
|
||||
predictions = regressor.predict(input_fn=test_input_fn)
|
||||
y_predicted = np.array(list(p['predictions'] for p in predictions))
|
||||
y_predicted = y_predicted.reshape(np.array(y_test).shape)
|
||||
|
||||
# Score with sklearn.
|
||||
score_sklearn = metrics.mean_squared_error(y_predicted, y_test)
|
||||
print('MSE (sklearn): {0:f}'.format(score_sklearn))
|
||||
|
||||
# Score with tensorflow.
|
||||
scores = regressor.evaluate(input_fn=test_input_fn)
|
||||
print('MSE (tensorflow): {0:f}'.format(scores['average_loss']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tf.app.run()
|
@ -44,15 +44,5 @@ function test() {
|
||||
fi
|
||||
}
|
||||
|
||||
test boston
|
||||
test iris
|
||||
test iris_custom_decay_dnn
|
||||
test iris_custom_model
|
||||
test iris_run_config
|
||||
test random_forest_mnist
|
||||
test resnet
|
||||
test text_classification --test_with_fake_data
|
||||
test text_classification_builtin_rnn_model --test_with_fake_data
|
||||
test text_classification_character_cnn --test_with_fake_data
|
||||
test text_classification_character_rnn --test_with_fake_data
|
||||
test text_classification_cnn --test_with_fake_data
|
||||
|
@ -1,81 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Example of DNNClassifier for Iris plant dataset, hdf5 format."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
from sklearn import datasets
|
||||
from sklearn import metrics
|
||||
from sklearn import model_selection
|
||||
import tensorflow as tf
|
||||
import h5py # pylint: disable=g-bad-import-order
|
||||
|
||||
|
||||
X_FEATURE = 'x' # Name of the input feature.
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
# Load dataset.
|
||||
iris = datasets.load_iris()
|
||||
x_train, x_test, y_train, y_test = model_selection.train_test_split(
|
||||
iris.data, iris.target, test_size=0.2, random_state=42)
|
||||
|
||||
# Note that we are saving and load iris data as h5 format as a simple
|
||||
# demonstration here.
|
||||
h5f = h5py.File('/tmp/test_hdf5.h5', 'w')
|
||||
h5f.create_dataset('X_train', data=x_train)
|
||||
h5f.create_dataset('X_test', data=x_test)
|
||||
h5f.create_dataset('y_train', data=y_train)
|
||||
h5f.create_dataset('y_test', data=y_test)
|
||||
h5f.close()
|
||||
|
||||
h5f = h5py.File('/tmp/test_hdf5.h5', 'r')
|
||||
x_train = np.array(h5f['X_train'])
|
||||
x_test = np.array(h5f['X_test'])
|
||||
y_train = np.array(h5f['y_train'])
|
||||
y_test = np.array(h5f['y_test'])
|
||||
|
||||
# Build 3 layer DNN with 10, 20, 10 units respectively.
|
||||
feature_columns = [
|
||||
tf.feature_column.numeric_column(
|
||||
X_FEATURE, shape=np.array(x_train).shape[1:])]
|
||||
classifier = tf.estimator.DNNClassifier(
|
||||
feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3)
|
||||
|
||||
# Train.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: x_train}, y=y_train, num_epochs=None, shuffle=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=200)
|
||||
|
||||
# Predict.
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: x_test}, y=y_test, num_epochs=1, shuffle=False)
|
||||
predictions = classifier.predict(input_fn=test_input_fn)
|
||||
y_predicted = np.array(list(p['class_ids'] for p in predictions))
|
||||
y_predicted = y_predicted.reshape(np.array(y_test).shape)
|
||||
|
||||
# Score with sklearn.
|
||||
score = metrics.accuracy_score(y_test, y_predicted)
|
||||
print('Accuracy (sklearn): {0:f}'.format(score))
|
||||
|
||||
# Score with tensorflow.
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tf.app.run()
|
@ -1,115 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Example of DNNClassifier for Iris plant dataset.
|
||||
|
||||
This example uses APIs in Tensorflow 1.4 or above.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
|
||||
from six.moves.urllib.request import urlretrieve
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
# Data sets
|
||||
IRIS_TRAINING = 'iris_training.csv'
|
||||
IRIS_TRAINING_URL = 'http://download.tensorflow.org/data/iris_training.csv'
|
||||
|
||||
IRIS_TEST = 'iris_test.csv'
|
||||
IRIS_TEST_URL = 'http://download.tensorflow.org/data/iris_test.csv'
|
||||
|
||||
FEATURE_KEYS = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
|
||||
|
||||
|
||||
def maybe_download_iris_data(file_name, download_url):
|
||||
"""Downloads the file and returns the number of data."""
|
||||
if not os.path.exists(file_name):
|
||||
urlretrieve(download_url, file_name)
|
||||
|
||||
# The first line is a comma-separated string. The first one is the number of
|
||||
# total data in the file.
|
||||
with open(file_name, 'r') as f:
|
||||
first_line = f.readline()
|
||||
num_elements = first_line.split(',')[0]
|
||||
return int(num_elements)
|
||||
|
||||
|
||||
def input_fn(file_name, num_data, batch_size, is_training):
|
||||
"""Creates an input_fn required by Estimator train/evaluate."""
|
||||
# If the data sets aren't stored locally, download them.
|
||||
|
||||
def _parse_csv(rows_string_tensor):
|
||||
"""Takes the string input tensor and returns tuple of (features, labels)."""
|
||||
# Last dim is the label.
|
||||
num_features = len(FEATURE_KEYS)
|
||||
num_columns = num_features + 1
|
||||
columns = tf.decode_csv(rows_string_tensor,
|
||||
record_defaults=[[]] * num_columns)
|
||||
features = dict(zip(FEATURE_KEYS, columns[:num_features]))
|
||||
labels = tf.cast(columns[num_features], tf.int32)
|
||||
return features, labels
|
||||
|
||||
def _input_fn():
|
||||
"""The input_fn."""
|
||||
dataset = tf.data.TextLineDataset([file_name])
|
||||
# Skip the first line (which does not have data).
|
||||
dataset = dataset.skip(1)
|
||||
dataset = dataset.map(_parse_csv)
|
||||
|
||||
if is_training:
|
||||
# For this small dataset, which can fit into memory, to achieve true
|
||||
# randomness, the shuffle buffer size is set as the total number of
|
||||
# elements in the dataset.
|
||||
dataset = dataset.shuffle(num_data)
|
||||
dataset = dataset.repeat()
|
||||
|
||||
dataset = dataset.batch(batch_size)
|
||||
iterator = dataset.make_one_shot_iterator()
|
||||
features, labels = iterator.get_next()
|
||||
return features, labels
|
||||
|
||||
return _input_fn
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
tf.logging.set_verbosity(tf.logging.INFO)
|
||||
|
||||
num_training_data = maybe_download_iris_data(
|
||||
IRIS_TRAINING, IRIS_TRAINING_URL)
|
||||
num_test_data = maybe_download_iris_data(IRIS_TEST, IRIS_TEST_URL)
|
||||
|
||||
# Build 3 layer DNN with 10, 20, 10 units respectively.
|
||||
feature_columns = [
|
||||
tf.feature_column.numeric_column(key, shape=1) for key in FEATURE_KEYS]
|
||||
classifier = tf.estimator.DNNClassifier(
|
||||
feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3)
|
||||
|
||||
# Train.
|
||||
train_input_fn = input_fn(IRIS_TRAINING, num_training_data, batch_size=32,
|
||||
is_training=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=400)
|
||||
|
||||
# Eval.
|
||||
test_input_fn = input_fn(IRIS_TEST, num_test_data, batch_size=32,
|
||||
is_training=False)
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tf.app.run()
|
@ -1,71 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Example of DNNClassifier for Iris plant dataset, with run config."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
from sklearn import datasets
|
||||
from sklearn import metrics
|
||||
from sklearn import model_selection
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
X_FEATURE = 'x' # Name of the input feature.
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
# Load dataset.
|
||||
iris = datasets.load_iris()
|
||||
x_train, x_test, y_train, y_test = model_selection.train_test_split(
|
||||
iris.data, iris.target, test_size=0.2, random_state=42)
|
||||
|
||||
# You can define you configurations by providing a RunConfig object to
|
||||
# estimator to control session configurations, e.g. tf_random_seed.
|
||||
run_config = tf.estimator.RunConfig().replace(tf_random_seed=1)
|
||||
|
||||
# Build 3 layer DNN with 10, 20, 10 units respectively.
|
||||
feature_columns = [
|
||||
tf.feature_column.numeric_column(
|
||||
X_FEATURE, shape=np.array(x_train).shape[1:])]
|
||||
classifier = tf.estimator.DNNClassifier(
|
||||
feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3,
|
||||
config=run_config)
|
||||
|
||||
# Train.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: x_train}, y=y_train, num_epochs=None, shuffle=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=200)
|
||||
|
||||
# Predict.
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: x_test}, y=y_test, num_epochs=1, shuffle=False)
|
||||
predictions = classifier.predict(input_fn=test_input_fn)
|
||||
y_predicted = np.array(list(p['class_ids'] for p in predictions))
|
||||
y_predicted = y_predicted.reshape(np.array(y_test).shape)
|
||||
|
||||
# Score with sklearn.
|
||||
score = metrics.accuracy_score(y_test, y_predicted)
|
||||
print('Accuracy (sklearn): {0:f}'.format(score))
|
||||
|
||||
# Score with tensorflow.
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tf.app.run()
|
@ -1,135 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""This showcases how simple it is to build image classification networks.
|
||||
|
||||
It follows description from this TensorFlow tutorial:
|
||||
https://www.tensorflow.org/versions/master/tutorials/mnist/pros/index.html#deep-mnist-for-experts
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
N_DIGITS = 10 # Number of digits.
|
||||
X_FEATURE = 'x' # Name of the input feature.
|
||||
|
||||
|
||||
def conv_model(features, labels, mode):
|
||||
"""2-layer convolution model."""
|
||||
# Reshape feature to 4d tensor with 2nd and 3rd dimensions being
|
||||
# image width and height final dimension being the number of color channels.
|
||||
feature = tf.reshape(features[X_FEATURE], [-1, 28, 28, 1])
|
||||
|
||||
# First conv layer will compute 32 features for each 5x5 patch
|
||||
with tf.variable_scope('conv_layer1'):
|
||||
h_conv1 = tf.layers.conv2d(
|
||||
feature,
|
||||
filters=32,
|
||||
kernel_size=[5, 5],
|
||||
padding='same',
|
||||
activation=tf.nn.relu)
|
||||
h_pool1 = tf.layers.max_pooling2d(
|
||||
h_conv1, pool_size=2, strides=2, padding='same')
|
||||
|
||||
# Second conv layer will compute 64 features for each 5x5 patch.
|
||||
with tf.variable_scope('conv_layer2'):
|
||||
h_conv2 = tf.layers.conv2d(
|
||||
h_pool1,
|
||||
filters=64,
|
||||
kernel_size=[5, 5],
|
||||
padding='same',
|
||||
activation=tf.nn.relu)
|
||||
h_pool2 = tf.layers.max_pooling2d(
|
||||
h_conv2, pool_size=2, strides=2, padding='same')
|
||||
# reshape tensor into a batch of vectors
|
||||
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
|
||||
|
||||
# Densely connected layer with 1024 neurons.
|
||||
h_fc1 = tf.layers.dense(h_pool2_flat, 1024, activation=tf.nn.relu)
|
||||
h_fc1 = tf.layers.dropout(
|
||||
h_fc1,
|
||||
rate=0.5,
|
||||
training=(mode == tf.estimator.ModeKeys.TRAIN))
|
||||
|
||||
# Compute logits (1 per class) and compute loss.
|
||||
logits = tf.layers.dense(h_fc1, N_DIGITS, activation=None)
|
||||
|
||||
# Compute predictions.
|
||||
predicted_classes = tf.argmax(logits, 1)
|
||||
if mode == tf.estimator.ModeKeys.PREDICT:
|
||||
predictions = {
|
||||
'class': predicted_classes,
|
||||
'prob': tf.nn.softmax(logits)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
|
||||
|
||||
# Compute loss.
|
||||
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
|
||||
|
||||
# Create training op.
|
||||
if mode == tf.estimator.ModeKeys.TRAIN:
|
||||
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
|
||||
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
|
||||
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
|
||||
|
||||
# Compute evaluation metrics.
|
||||
eval_metric_ops = {
|
||||
'accuracy': tf.metrics.accuracy(
|
||||
labels=labels, predictions=predicted_classes)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode, loss=loss, eval_metric_ops=eval_metric_ops)
|
||||
|
||||
|
||||
def main(unused_args):
|
||||
tf.logging.set_verbosity(tf.logging.INFO)
|
||||
|
||||
### Download and load MNIST dataset.
|
||||
mnist = tf.contrib.learn.datasets.DATASETS['mnist']('/tmp/mnist')
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: mnist.train.images},
|
||||
y=mnist.train.labels.astype(np.int32),
|
||||
batch_size=100,
|
||||
num_epochs=None,
|
||||
shuffle=True)
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: mnist.train.images},
|
||||
y=mnist.train.labels.astype(np.int32),
|
||||
num_epochs=1,
|
||||
shuffle=False)
|
||||
|
||||
### Linear classifier.
|
||||
feature_columns = [
|
||||
tf.feature_column.numeric_column(
|
||||
X_FEATURE, shape=mnist.train.images.shape[1:])]
|
||||
|
||||
classifier = tf.estimator.LinearClassifier(
|
||||
feature_columns=feature_columns, n_classes=N_DIGITS)
|
||||
classifier.train(input_fn=train_input_fn, steps=200)
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy (LinearClassifier): {0:f}'.format(scores['accuracy']))
|
||||
|
||||
### Convolutional network
|
||||
classifier = tf.estimator.Estimator(model_fn=conv_model)
|
||||
classifier.train(input_fn=train_input_fn, steps=200)
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy (conv_model): {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tf.app.run()
|
@ -1,116 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Example of using Estimator with multiple GPUs to distribute one model.
|
||||
|
||||
This example only runs if you have multiple GPUs to assign to.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
from sklearn import datasets
|
||||
from sklearn import metrics
|
||||
from sklearn import model_selection
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
X_FEATURE = 'x' # Name of the input feature.
|
||||
|
||||
|
||||
def my_model(features, labels, mode):
|
||||
"""DNN with three hidden layers, and dropout of 0.1 probability.
|
||||
|
||||
Note: If you want to run this example with multiple GPUs, Cuda Toolkit 7.0 and
|
||||
CUDNN 6.5 V2 from NVIDIA need to be installed beforehand.
|
||||
|
||||
Args:
|
||||
features: Dict of input `Tensor`.
|
||||
labels: Label `Tensor`.
|
||||
mode: One of `ModeKeys`.
|
||||
|
||||
Returns:
|
||||
`EstimatorSpec`.
|
||||
"""
|
||||
# Create three fully connected layers respectively of size 10, 20, and 10 with
|
||||
# each layer having a dropout probability of 0.1.
|
||||
net = features[X_FEATURE]
|
||||
with tf.device('/device:GPU:1'):
|
||||
for units in [10, 20, 10]:
|
||||
net = tf.layers.dense(net, units=units, activation=tf.nn.relu)
|
||||
net = tf.layers.dropout(net, rate=0.1)
|
||||
|
||||
with tf.device('/device:GPU:2'):
|
||||
# Compute logits (1 per class).
|
||||
logits = tf.layers.dense(net, 3, activation=None)
|
||||
|
||||
# Compute predictions.
|
||||
predicted_classes = tf.argmax(logits, 1)
|
||||
if mode == tf.estimator.ModeKeys.PREDICT:
|
||||
predictions = {
|
||||
'class': predicted_classes,
|
||||
'prob': tf.nn.softmax(logits)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
|
||||
|
||||
# Compute loss.
|
||||
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
|
||||
|
||||
# Create training op.
|
||||
if mode == tf.estimator.ModeKeys.TRAIN:
|
||||
optimizer = tf.train.AdagradOptimizer(learning_rate=0.1)
|
||||
train_op = optimizer.minimize(
|
||||
loss, global_step=tf.train.get_global_step())
|
||||
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
|
||||
|
||||
# Compute evaluation metrics.
|
||||
eval_metric_ops = {
|
||||
'accuracy': tf.metrics.accuracy(
|
||||
labels=labels, predictions=predicted_classes)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode, loss=loss, eval_metric_ops=eval_metric_ops)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
iris = datasets.load_iris()
|
||||
x_train, x_test, y_train, y_test = model_selection.train_test_split(
|
||||
iris.data, iris.target, test_size=0.2, random_state=42)
|
||||
|
||||
classifier = tf.estimator.Estimator(model_fn=my_model)
|
||||
|
||||
# Train.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: x_train}, y=y_train, num_epochs=None, shuffle=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=100)
|
||||
|
||||
# Predict.
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: x_test}, y=y_test, num_epochs=1, shuffle=False)
|
||||
predictions = classifier.predict(input_fn=test_input_fn)
|
||||
y_predicted = np.array(list(p['class'] for p in predictions))
|
||||
y_predicted = y_predicted.reshape(np.array(y_test).shape)
|
||||
|
||||
# Score with sklearn.
|
||||
score = metrics.accuracy_score(y_test, y_predicted)
|
||||
print('Accuracy (sklearn): {0:f}'.format(score))
|
||||
|
||||
# Score with tensorflow.
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tf.app.run()
|
@ -1,137 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""A stand-alone example for tf.learn's random forest model on mnist."""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
import numpy
|
||||
|
||||
from tensorflow.contrib.learn.python.learn import metric_spec
|
||||
from tensorflow.contrib.tensor_forest.client import eval_metrics
|
||||
from tensorflow.contrib.tensor_forest.client import random_forest
|
||||
from tensorflow.contrib.tensor_forest.python import tensor_forest
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
from tensorflow.python.estimator.inputs import numpy_io
|
||||
from tensorflow.python.platform import app
|
||||
|
||||
FLAGS = None
|
||||
|
||||
|
||||
def build_estimator(model_dir):
|
||||
"""Build an estimator."""
|
||||
params = tensor_forest.ForestHParams(
|
||||
num_classes=10,
|
||||
num_features=784,
|
||||
num_trees=FLAGS.num_trees,
|
||||
max_nodes=FLAGS.max_nodes)
|
||||
graph_builder_class = tensor_forest.RandomForestGraphs
|
||||
if FLAGS.use_training_loss:
|
||||
graph_builder_class = tensor_forest.TrainingLossForest
|
||||
return random_forest.TensorForestEstimator(
|
||||
params, graph_builder_class=graph_builder_class, model_dir=model_dir)
|
||||
|
||||
|
||||
def train_and_eval():
|
||||
"""Train and evaluate the model."""
|
||||
model_dir = tempfile.mkdtemp() if not FLAGS.model_dir else FLAGS.model_dir
|
||||
print('model directory = %s' % model_dir)
|
||||
|
||||
est = build_estimator(model_dir)
|
||||
|
||||
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=False)
|
||||
|
||||
train_input_fn = numpy_io.numpy_input_fn(
|
||||
x={'images': mnist.train.images},
|
||||
y=mnist.train.labels.astype(numpy.int32),
|
||||
batch_size=FLAGS.batch_size,
|
||||
num_epochs=None,
|
||||
shuffle=True)
|
||||
est.fit(input_fn=train_input_fn, steps=None)
|
||||
|
||||
metric_name = 'accuracy'
|
||||
metric = {
|
||||
metric_name:
|
||||
metric_spec.MetricSpec(
|
||||
eval_metrics.get_metric(metric_name),
|
||||
prediction_key=eval_metrics.get_prediction_key(metric_name))
|
||||
}
|
||||
|
||||
test_input_fn = numpy_io.numpy_input_fn(
|
||||
x={'images': mnist.test.images},
|
||||
y=mnist.test.labels.astype(numpy.int32),
|
||||
num_epochs=1,
|
||||
batch_size=FLAGS.batch_size,
|
||||
shuffle=False)
|
||||
|
||||
results = est.evaluate(input_fn=test_input_fn, metrics=metric)
|
||||
for key in sorted(results):
|
||||
print('%s: %s' % (key, results[key]))
|
||||
|
||||
|
||||
def main(_):
|
||||
train_and_eval()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--model_dir',
|
||||
type=str,
|
||||
default='',
|
||||
help='Base directory for output models.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--data_dir',
|
||||
type=str,
|
||||
default='/tmp/data/',
|
||||
help='Directory for storing data'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--train_steps',
|
||||
type=int,
|
||||
default=1000,
|
||||
help='Number of training steps.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--batch_size',
|
||||
type=str,
|
||||
default=1000,
|
||||
help='Number of examples in a training batch.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--num_trees',
|
||||
type=int,
|
||||
default=100,
|
||||
help='Number of trees in the forest.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--max_nodes',
|
||||
type=int,
|
||||
default=1000,
|
||||
help='Max total nodes in a single tree.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--use_training_loss',
|
||||
type=bool,
|
||||
default=False,
|
||||
help='If true, use training loss as termination criteria.'
|
||||
)
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,202 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""This example builds deep residual network for mnist data.
|
||||
|
||||
Reference Paper: http://arxiv.org/pdf/1512.03385.pdf
|
||||
|
||||
Note that this is still a work-in-progress. Feel free to submit a PR
|
||||
to make this better.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
from collections import namedtuple
|
||||
from math import sqrt
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
N_DIGITS = 10 # Number of digits.
|
||||
X_FEATURE = 'x' # Name of the input feature.
|
||||
|
||||
|
||||
def res_net_model(features, labels, mode):
|
||||
"""Builds a residual network."""
|
||||
|
||||
# Configurations for each bottleneck group.
|
||||
BottleneckGroup = namedtuple('BottleneckGroup',
|
||||
['num_blocks', 'num_filters', 'bottleneck_size'])
|
||||
groups = [
|
||||
BottleneckGroup(3, 128, 32), BottleneckGroup(3, 256, 64),
|
||||
BottleneckGroup(3, 512, 128), BottleneckGroup(3, 1024, 256)
|
||||
]
|
||||
|
||||
x = features[X_FEATURE]
|
||||
input_shape = x.get_shape().as_list()
|
||||
|
||||
# Reshape the input into the right shape if it's 2D tensor
|
||||
if len(input_shape) == 2:
|
||||
ndim = int(sqrt(input_shape[1]))
|
||||
x = tf.reshape(x, [-1, ndim, ndim, 1])
|
||||
|
||||
training = (mode == tf.estimator.ModeKeys.TRAIN)
|
||||
|
||||
# First convolution expands to 64 channels
|
||||
with tf.variable_scope('conv_layer1'):
|
||||
net = tf.layers.conv2d(
|
||||
x,
|
||||
filters=64,
|
||||
kernel_size=7,
|
||||
activation=tf.nn.relu)
|
||||
net = tf.layers.batch_normalization(net, training=training)
|
||||
|
||||
# Max pool
|
||||
net = tf.layers.max_pooling2d(
|
||||
net, pool_size=3, strides=2, padding='same')
|
||||
|
||||
# First chain of resnets
|
||||
with tf.variable_scope('conv_layer2'):
|
||||
net = tf.layers.conv2d(
|
||||
net,
|
||||
filters=groups[0].num_filters,
|
||||
kernel_size=1,
|
||||
padding='valid')
|
||||
|
||||
# Create the bottleneck groups, each of which contains `num_blocks`
|
||||
# bottleneck groups.
|
||||
for group_i, group in enumerate(groups):
|
||||
for block_i in range(group.num_blocks):
|
||||
name = 'group_%d/block_%d' % (group_i, block_i)
|
||||
|
||||
# 1x1 convolution responsible for reducing dimension
|
||||
with tf.variable_scope(name + '/conv_in'):
|
||||
conv = tf.layers.conv2d(
|
||||
net,
|
||||
filters=group.num_filters,
|
||||
kernel_size=1,
|
||||
padding='valid',
|
||||
activation=tf.nn.relu)
|
||||
conv = tf.layers.batch_normalization(conv, training=training)
|
||||
|
||||
with tf.variable_scope(name + '/conv_bottleneck'):
|
||||
conv = tf.layers.conv2d(
|
||||
conv,
|
||||
filters=group.bottleneck_size,
|
||||
kernel_size=3,
|
||||
padding='same',
|
||||
activation=tf.nn.relu)
|
||||
conv = tf.layers.batch_normalization(conv, training=training)
|
||||
|
||||
# 1x1 convolution responsible for restoring dimension
|
||||
with tf.variable_scope(name + '/conv_out'):
|
||||
input_dim = net.get_shape()[-1].value
|
||||
conv = tf.layers.conv2d(
|
||||
conv,
|
||||
filters=input_dim,
|
||||
kernel_size=1,
|
||||
padding='valid',
|
||||
activation=tf.nn.relu)
|
||||
conv = tf.layers.batch_normalization(conv, training=training)
|
||||
|
||||
# shortcut connections that turn the network into its counterpart
|
||||
# residual function (identity shortcut)
|
||||
net = conv + net
|
||||
|
||||
try:
|
||||
# upscale to the next group size
|
||||
next_group = groups[group_i + 1]
|
||||
with tf.variable_scope('block_%d/conv_upscale' % group_i):
|
||||
net = tf.layers.conv2d(
|
||||
net,
|
||||
filters=next_group.num_filters,
|
||||
kernel_size=1,
|
||||
padding='same',
|
||||
activation=None,
|
||||
bias_initializer=None)
|
||||
except IndexError:
|
||||
pass
|
||||
|
||||
net_shape = net.get_shape().as_list()
|
||||
net = tf.nn.avg_pool(
|
||||
net,
|
||||
ksize=[1, net_shape[1], net_shape[2], 1],
|
||||
strides=[1, 1, 1, 1],
|
||||
padding='VALID')
|
||||
|
||||
net_shape = net.get_shape().as_list()
|
||||
net = tf.reshape(net, [-1, net_shape[1] * net_shape[2] * net_shape[3]])
|
||||
|
||||
# Compute logits (1 per class) and compute loss.
|
||||
logits = tf.layers.dense(net, N_DIGITS, activation=None)
|
||||
|
||||
# Compute predictions.
|
||||
predicted_classes = tf.argmax(logits, 1)
|
||||
if mode == tf.estimator.ModeKeys.PREDICT:
|
||||
predictions = {
|
||||
'class': predicted_classes,
|
||||
'prob': tf.nn.softmax(logits)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
|
||||
|
||||
# Compute loss.
|
||||
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
|
||||
|
||||
# Create training op.
|
||||
if training:
|
||||
optimizer = tf.train.AdagradOptimizer(learning_rate=0.01)
|
||||
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
|
||||
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
|
||||
|
||||
# Compute evaluation metrics.
|
||||
eval_metric_ops = {
|
||||
'accuracy': tf.metrics.accuracy(
|
||||
labels=labels, predictions=predicted_classes)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode, loss=loss, eval_metric_ops=eval_metric_ops)
|
||||
|
||||
|
||||
def main(unused_args):
|
||||
# Download and load MNIST data.
|
||||
mnist = tf.contrib.learn.datasets.DATASETS['mnist']('/tmp/mnist')
|
||||
|
||||
# Create a new resnet classifier.
|
||||
classifier = tf.estimator.Estimator(model_fn=res_net_model)
|
||||
|
||||
tf.logging.set_verbosity(tf.logging.INFO) # Show training logs.
|
||||
|
||||
# Train model and save summaries into logdir.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: mnist.train.images},
|
||||
y=mnist.train.labels.astype(np.int32),
|
||||
batch_size=100,
|
||||
num_epochs=None,
|
||||
shuffle=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=100)
|
||||
|
||||
# Calculate accuracy.
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={X_FEATURE: mnist.test.images},
|
||||
y=mnist.test.labels.astype(np.int32),
|
||||
num_epochs=1,
|
||||
shuffle=False)
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy: {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tf.app.run()
|
@ -1,180 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Example of Estimator for DNN-based text classification with DBpedia data."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
import pandas
|
||||
from sklearn import metrics
|
||||
import tensorflow as tf
|
||||
|
||||
FLAGS = None
|
||||
|
||||
MAX_DOCUMENT_LENGTH = 10
|
||||
EMBEDDING_SIZE = 50
|
||||
n_words = 0
|
||||
MAX_LABEL = 15
|
||||
WORDS_FEATURE = 'words' # Name of the input words feature.
|
||||
|
||||
|
||||
def estimator_spec_for_softmax_classification(logits, labels, mode):
|
||||
"""Returns EstimatorSpec instance for softmax classification."""
|
||||
predicted_classes = tf.argmax(logits, 1)
|
||||
if mode == tf.estimator.ModeKeys.PREDICT:
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode,
|
||||
predictions={
|
||||
'class': predicted_classes,
|
||||
'prob': tf.nn.softmax(logits)
|
||||
})
|
||||
|
||||
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
|
||||
if mode == tf.estimator.ModeKeys.TRAIN:
|
||||
optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
|
||||
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
|
||||
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
|
||||
|
||||
eval_metric_ops = {
|
||||
'accuracy':
|
||||
tf.metrics.accuracy(labels=labels, predictions=predicted_classes)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
|
||||
|
||||
|
||||
def bag_of_words_model(features, labels, mode):
|
||||
"""A bag-of-words model. Note it disregards the word order in the text."""
|
||||
bow_column = tf.feature_column.categorical_column_with_identity(
|
||||
WORDS_FEATURE, num_buckets=n_words)
|
||||
bow_embedding_column = tf.feature_column.embedding_column(
|
||||
bow_column, dimension=EMBEDDING_SIZE)
|
||||
bow = tf.feature_column.input_layer(
|
||||
features, feature_columns=[bow_embedding_column])
|
||||
logits = tf.layers.dense(bow, MAX_LABEL, activation=None)
|
||||
|
||||
return estimator_spec_for_softmax_classification(
|
||||
logits=logits, labels=labels, mode=mode)
|
||||
|
||||
|
||||
def rnn_model(features, labels, mode):
|
||||
"""RNN model to predict from sequence of words to a class."""
|
||||
# Convert indexes of words into embeddings.
|
||||
# This creates embeddings matrix of [n_words, EMBEDDING_SIZE] and then
|
||||
# maps word indexes of the sequence into [batch_size, sequence_length,
|
||||
# EMBEDDING_SIZE].
|
||||
word_vectors = tf.contrib.layers.embed_sequence(
|
||||
features[WORDS_FEATURE], vocab_size=n_words, embed_dim=EMBEDDING_SIZE)
|
||||
|
||||
# Split into list of embedding per word, while removing doc length dim.
|
||||
# word_list results to be a list of tensors [batch_size, EMBEDDING_SIZE].
|
||||
word_list = tf.unstack(word_vectors, axis=1)
|
||||
|
||||
# Create a Gated Recurrent Unit cell with hidden size of EMBEDDING_SIZE.
|
||||
cell = tf.nn.rnn_cell.GRUCell(EMBEDDING_SIZE)
|
||||
|
||||
# Create an unrolled Recurrent Neural Networks to length of
|
||||
# MAX_DOCUMENT_LENGTH and passes word_list as inputs for each unit.
|
||||
_, encoding = tf.nn.static_rnn(cell, word_list, dtype=tf.float32)
|
||||
|
||||
# Given encoding of RNN, take encoding of last step (e.g hidden size of the
|
||||
# neural network of last step) and pass it as features for softmax
|
||||
# classification over output classes.
|
||||
logits = tf.layers.dense(encoding, MAX_LABEL, activation=None)
|
||||
return estimator_spec_for_softmax_classification(
|
||||
logits=logits, labels=labels, mode=mode)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
global n_words
|
||||
tf.logging.set_verbosity(tf.logging.INFO)
|
||||
|
||||
# Prepare training and testing data
|
||||
dbpedia = tf.contrib.learn.datasets.load_dataset(
|
||||
'dbpedia', test_with_fake_data=FLAGS.test_with_fake_data)
|
||||
x_train = pandas.Series(dbpedia.train.data[:, 1])
|
||||
y_train = pandas.Series(dbpedia.train.target)
|
||||
x_test = pandas.Series(dbpedia.test.data[:, 1])
|
||||
y_test = pandas.Series(dbpedia.test.target)
|
||||
|
||||
# Process vocabulary
|
||||
vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(
|
||||
MAX_DOCUMENT_LENGTH)
|
||||
|
||||
x_transform_train = vocab_processor.fit_transform(x_train)
|
||||
x_transform_test = vocab_processor.transform(x_test)
|
||||
|
||||
x_train = np.array(list(x_transform_train))
|
||||
x_test = np.array(list(x_transform_test))
|
||||
|
||||
n_words = len(vocab_processor.vocabulary_)
|
||||
print('Total words: %d' % n_words)
|
||||
|
||||
# Build model
|
||||
# Switch between rnn_model and bag_of_words_model to test different models.
|
||||
model_fn = rnn_model
|
||||
if FLAGS.bow_model:
|
||||
# Subtract 1 because VocabularyProcessor outputs a word-id matrix where word
|
||||
# ids start from 1 and 0 means 'no word'. But
|
||||
# categorical_column_with_identity assumes 0-based count and uses -1 for
|
||||
# missing word.
|
||||
x_train -= 1
|
||||
x_test -= 1
|
||||
model_fn = bag_of_words_model
|
||||
classifier = tf.estimator.Estimator(model_fn=model_fn)
|
||||
|
||||
# Train.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={WORDS_FEATURE: x_train},
|
||||
y=y_train,
|
||||
batch_size=len(x_train),
|
||||
num_epochs=None,
|
||||
shuffle=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=100)
|
||||
|
||||
# Predict.
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={WORDS_FEATURE: x_test}, y=y_test, num_epochs=1, shuffle=False)
|
||||
predictions = classifier.predict(input_fn=test_input_fn)
|
||||
y_predicted = np.array(list(p['class'] for p in predictions))
|
||||
y_predicted = y_predicted.reshape(np.array(y_test).shape)
|
||||
|
||||
# Score with sklearn.
|
||||
score = metrics.accuracy_score(y_test, y_predicted)
|
||||
print('Accuracy (sklearn): {0:f}'.format(score))
|
||||
|
||||
# Score with tensorflow.
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--test_with_fake_data',
|
||||
default=False,
|
||||
help='Test the example code with fake data.',
|
||||
action='store_true')
|
||||
parser.add_argument(
|
||||
'--bow_model',
|
||||
default=False,
|
||||
help='Run with BOW model instead of RNN.',
|
||||
action='store_true')
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,160 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Example of using convolutional networks over characters for DBpedia dataset.
|
||||
|
||||
This model is similar to one described in this paper:
|
||||
"Character-level Convolutional Networks for Text Classification"
|
||||
http://arxiv.org/abs/1509.01626
|
||||
|
||||
and is somewhat alternative to the Lua code from here:
|
||||
https://github.com/zhangxiangxiao/Crepe
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
import pandas
|
||||
import tensorflow as tf
|
||||
|
||||
FLAGS = None
|
||||
|
||||
MAX_DOCUMENT_LENGTH = 100
|
||||
N_FILTERS = 10
|
||||
FILTER_SHAPE1 = [20, 256]
|
||||
FILTER_SHAPE2 = [20, N_FILTERS]
|
||||
POOLING_WINDOW = 4
|
||||
POOLING_STRIDE = 2
|
||||
MAX_LABEL = 15
|
||||
CHARS_FEATURE = 'chars' # Name of the input character feature.
|
||||
|
||||
|
||||
def char_cnn_model(features, labels, mode):
|
||||
"""Character level convolutional neural network model to predict classes."""
|
||||
features_onehot = tf.one_hot(features[CHARS_FEATURE], 256)
|
||||
input_layer = tf.reshape(
|
||||
features_onehot, [-1, MAX_DOCUMENT_LENGTH, 256, 1])
|
||||
with tf.variable_scope('CNN_Layer1'):
|
||||
# Apply Convolution filtering on input sequence.
|
||||
conv1 = tf.layers.conv2d(
|
||||
input_layer,
|
||||
filters=N_FILTERS,
|
||||
kernel_size=FILTER_SHAPE1,
|
||||
padding='VALID',
|
||||
# Add a ReLU for non linearity.
|
||||
activation=tf.nn.relu)
|
||||
# Max pooling across output of Convolution+Relu.
|
||||
pool1 = tf.layers.max_pooling2d(
|
||||
conv1,
|
||||
pool_size=POOLING_WINDOW,
|
||||
strides=POOLING_STRIDE,
|
||||
padding='SAME')
|
||||
# Transpose matrix so that n_filters from convolution becomes width.
|
||||
pool1 = tf.transpose(pool1, [0, 1, 3, 2])
|
||||
with tf.variable_scope('CNN_Layer2'):
|
||||
# Second level of convolution filtering.
|
||||
conv2 = tf.layers.conv2d(
|
||||
pool1,
|
||||
filters=N_FILTERS,
|
||||
kernel_size=FILTER_SHAPE2,
|
||||
padding='VALID')
|
||||
# Max across each filter to get useful features for classification.
|
||||
pool2 = tf.squeeze(tf.reduce_max(conv2, 1), axis=[1])
|
||||
|
||||
# Apply regular WX + B and classification.
|
||||
logits = tf.layers.dense(pool2, MAX_LABEL, activation=None)
|
||||
|
||||
predicted_classes = tf.argmax(logits, 1)
|
||||
if mode == tf.estimator.ModeKeys.PREDICT:
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode,
|
||||
predictions={
|
||||
'class': predicted_classes,
|
||||
'prob': tf.nn.softmax(logits)
|
||||
})
|
||||
|
||||
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
|
||||
if mode == tf.estimator.ModeKeys.TRAIN:
|
||||
optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
|
||||
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
|
||||
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
|
||||
|
||||
eval_metric_ops = {
|
||||
'accuracy': tf.metrics.accuracy(
|
||||
labels=labels, predictions=predicted_classes)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
tf.logging.set_verbosity(tf.logging.INFO)
|
||||
|
||||
# Prepare training and testing data
|
||||
dbpedia = tf.contrib.learn.datasets.load_dataset(
|
||||
'dbpedia', test_with_fake_data=FLAGS.test_with_fake_data, size='large')
|
||||
x_train = pandas.DataFrame(dbpedia.train.data)[1]
|
||||
y_train = pandas.Series(dbpedia.train.target)
|
||||
x_test = pandas.DataFrame(dbpedia.test.data)[1]
|
||||
y_test = pandas.Series(dbpedia.test.target)
|
||||
|
||||
# Process vocabulary
|
||||
char_processor = tf.contrib.learn.preprocessing.ByteProcessor(
|
||||
MAX_DOCUMENT_LENGTH)
|
||||
x_train = np.array(list(char_processor.fit_transform(x_train)))
|
||||
x_test = np.array(list(char_processor.transform(x_test)))
|
||||
|
||||
x_train = x_train.reshape([-1, MAX_DOCUMENT_LENGTH, 1, 1])
|
||||
x_test = x_test.reshape([-1, MAX_DOCUMENT_LENGTH, 1, 1])
|
||||
|
||||
# Build model
|
||||
classifier = tf.estimator.Estimator(model_fn=char_cnn_model)
|
||||
|
||||
# Train.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={CHARS_FEATURE: x_train},
|
||||
y=y_train,
|
||||
batch_size=128,
|
||||
num_epochs=None,
|
||||
shuffle=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=100)
|
||||
|
||||
# Predict.
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={CHARS_FEATURE: x_test},
|
||||
y=y_test,
|
||||
num_epochs=1,
|
||||
shuffle=False)
|
||||
predictions = classifier.predict(input_fn=test_input_fn)
|
||||
y_predicted = np.array(list(p['class'] for p in predictions))
|
||||
y_predicted = y_predicted.reshape(np.array(y_test).shape)
|
||||
|
||||
# Score with tensorflow.
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy: {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--test_with_fake_data',
|
||||
default=False,
|
||||
help='Test the example code with fake data.',
|
||||
action='store_true')
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,122 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Example of recurrent neural networks over characters for DBpedia dataset.
|
||||
|
||||
This model is similar to one described in this paper:
|
||||
"Character-level Convolutional Networks for Text Classification"
|
||||
http://arxiv.org/abs/1509.01626
|
||||
|
||||
and is somewhat alternative to the Lua code from here:
|
||||
https://github.com/zhangxiangxiao/Crepe
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
import pandas
|
||||
import tensorflow as tf
|
||||
|
||||
FLAGS = None
|
||||
|
||||
MAX_DOCUMENT_LENGTH = 100
|
||||
HIDDEN_SIZE = 20
|
||||
MAX_LABEL = 15
|
||||
CHARS_FEATURE = 'chars' # Name of the input character feature.
|
||||
|
||||
|
||||
def char_rnn_model(features, labels, mode):
|
||||
"""Character level recurrent neural network model to predict classes."""
|
||||
byte_vectors = tf.one_hot(features[CHARS_FEATURE], 256, 1., 0.)
|
||||
byte_list = tf.unstack(byte_vectors, axis=1)
|
||||
|
||||
cell = tf.nn.rnn_cell.GRUCell(HIDDEN_SIZE)
|
||||
_, encoding = tf.nn.static_rnn(cell, byte_list, dtype=tf.float32)
|
||||
|
||||
logits = tf.layers.dense(encoding, MAX_LABEL, activation=None)
|
||||
|
||||
predicted_classes = tf.argmax(logits, 1)
|
||||
if mode == tf.estimator.ModeKeys.PREDICT:
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode,
|
||||
predictions={
|
||||
'class': predicted_classes,
|
||||
'prob': tf.nn.softmax(logits)
|
||||
})
|
||||
|
||||
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
|
||||
if mode == tf.estimator.ModeKeys.TRAIN:
|
||||
optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
|
||||
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
|
||||
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
|
||||
|
||||
eval_metric_ops = {
|
||||
'accuracy': tf.metrics.accuracy(
|
||||
labels=labels, predictions=predicted_classes)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
# Prepare training and testing data
|
||||
dbpedia = tf.contrib.learn.datasets.load_dataset(
|
||||
'dbpedia', test_with_fake_data=FLAGS.test_with_fake_data)
|
||||
x_train = pandas.DataFrame(dbpedia.train.data)[1]
|
||||
y_train = pandas.Series(dbpedia.train.target)
|
||||
x_test = pandas.DataFrame(dbpedia.test.data)[1]
|
||||
y_test = pandas.Series(dbpedia.test.target)
|
||||
|
||||
# Process vocabulary
|
||||
char_processor = tf.contrib.learn.preprocessing.ByteProcessor(
|
||||
MAX_DOCUMENT_LENGTH)
|
||||
x_train = np.array(list(char_processor.fit_transform(x_train)))
|
||||
x_test = np.array(list(char_processor.transform(x_test)))
|
||||
|
||||
# Build model
|
||||
classifier = tf.estimator.Estimator(model_fn=char_rnn_model)
|
||||
|
||||
# Train.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={CHARS_FEATURE: x_train},
|
||||
y=y_train,
|
||||
batch_size=128,
|
||||
num_epochs=None,
|
||||
shuffle=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=100)
|
||||
|
||||
# Eval.
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={CHARS_FEATURE: x_test},
|
||||
y=y_test,
|
||||
num_epochs=1,
|
||||
shuffle=False)
|
||||
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy: {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--test_with_fake_data',
|
||||
default=False,
|
||||
help='Test the example code with fake data.',
|
||||
action='store_true')
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,153 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Example of Estimator for CNN-based text classification with DBpedia data."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
import pandas
|
||||
import tensorflow as tf
|
||||
|
||||
FLAGS = None
|
||||
|
||||
MAX_DOCUMENT_LENGTH = 100
|
||||
EMBEDDING_SIZE = 20
|
||||
N_FILTERS = 10
|
||||
WINDOW_SIZE = 20
|
||||
FILTER_SHAPE1 = [WINDOW_SIZE, EMBEDDING_SIZE]
|
||||
FILTER_SHAPE2 = [WINDOW_SIZE, N_FILTERS]
|
||||
POOLING_WINDOW = 4
|
||||
POOLING_STRIDE = 2
|
||||
n_words = 0
|
||||
MAX_LABEL = 15
|
||||
WORDS_FEATURE = 'words' # Name of the input words feature.
|
||||
|
||||
|
||||
def cnn_model(features, labels, mode):
|
||||
"""2 layer ConvNet to predict from sequence of words to a class."""
|
||||
# Convert indexes of words into embeddings.
|
||||
# This creates embeddings matrix of [n_words, EMBEDDING_SIZE] and then
|
||||
# maps word indexes of the sequence into [batch_size, sequence_length,
|
||||
# EMBEDDING_SIZE].
|
||||
word_vectors = tf.contrib.layers.embed_sequence(
|
||||
features[WORDS_FEATURE], vocab_size=n_words, embed_dim=EMBEDDING_SIZE)
|
||||
word_vectors = tf.expand_dims(word_vectors, 3)
|
||||
with tf.variable_scope('CNN_Layer1'):
|
||||
# Apply Convolution filtering on input sequence.
|
||||
conv1 = tf.layers.conv2d(
|
||||
word_vectors,
|
||||
filters=N_FILTERS,
|
||||
kernel_size=FILTER_SHAPE1,
|
||||
padding='VALID',
|
||||
# Add a ReLU for non linearity.
|
||||
activation=tf.nn.relu)
|
||||
# Max pooling across output of Convolution+Relu.
|
||||
pool1 = tf.layers.max_pooling2d(
|
||||
conv1,
|
||||
pool_size=POOLING_WINDOW,
|
||||
strides=POOLING_STRIDE,
|
||||
padding='SAME')
|
||||
# Transpose matrix so that n_filters from convolution becomes width.
|
||||
pool1 = tf.transpose(pool1, [0, 1, 3, 2])
|
||||
with tf.variable_scope('CNN_Layer2'):
|
||||
# Second level of convolution filtering.
|
||||
conv2 = tf.layers.conv2d(
|
||||
pool1,
|
||||
filters=N_FILTERS,
|
||||
kernel_size=FILTER_SHAPE2,
|
||||
padding='VALID')
|
||||
# Max across each filter to get useful features for classification.
|
||||
pool2 = tf.squeeze(tf.reduce_max(conv2, 1), axis=[1])
|
||||
|
||||
# Apply regular WX + B and classification.
|
||||
logits = tf.layers.dense(pool2, MAX_LABEL, activation=None)
|
||||
|
||||
predicted_classes = tf.argmax(logits, 1)
|
||||
if mode == tf.estimator.ModeKeys.PREDICT:
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode,
|
||||
predictions={
|
||||
'class': predicted_classes,
|
||||
'prob': tf.nn.softmax(logits)
|
||||
})
|
||||
|
||||
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
|
||||
if mode == tf.estimator.ModeKeys.TRAIN:
|
||||
optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
|
||||
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
|
||||
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
|
||||
|
||||
eval_metric_ops = {
|
||||
'accuracy': tf.metrics.accuracy(
|
||||
labels=labels, predictions=predicted_classes)
|
||||
}
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
global n_words
|
||||
# Prepare training and testing data
|
||||
dbpedia = tf.contrib.learn.datasets.load_dataset(
|
||||
'dbpedia', test_with_fake_data=FLAGS.test_with_fake_data)
|
||||
x_train = pandas.DataFrame(dbpedia.train.data)[1]
|
||||
y_train = pandas.Series(dbpedia.train.target)
|
||||
x_test = pandas.DataFrame(dbpedia.test.data)[1]
|
||||
y_test = pandas.Series(dbpedia.test.target)
|
||||
|
||||
# Process vocabulary
|
||||
vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(
|
||||
MAX_DOCUMENT_LENGTH)
|
||||
x_train = np.array(list(vocab_processor.fit_transform(x_train)))
|
||||
x_test = np.array(list(vocab_processor.transform(x_test)))
|
||||
n_words = len(vocab_processor.vocabulary_)
|
||||
print('Total words: %d' % n_words)
|
||||
|
||||
# Build model
|
||||
classifier = tf.estimator.Estimator(model_fn=cnn_model)
|
||||
|
||||
# Train.
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={WORDS_FEATURE: x_train},
|
||||
y=y_train,
|
||||
batch_size=len(x_train),
|
||||
num_epochs=None,
|
||||
shuffle=True)
|
||||
classifier.train(input_fn=train_input_fn, steps=100)
|
||||
|
||||
# Evaluate.
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={WORDS_FEATURE: x_test},
|
||||
y=y_test,
|
||||
num_epochs=1,
|
||||
shuffle=False)
|
||||
|
||||
scores = classifier.evaluate(input_fn=test_input_fn)
|
||||
print('Accuracy: {0:f}'.format(scores['accuracy']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--test_with_fake_data',
|
||||
default=False,
|
||||
help='Test the example code with fake data.',
|
||||
action='store_true')
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,22 +0,0 @@
|
||||
# Example Estimator model
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
licenses(["notice"]) # Apache 2.0
|
||||
|
||||
exports_files(["LICENSE"])
|
||||
|
||||
py_binary(
|
||||
name = "abalone",
|
||||
srcs = [
|
||||
"abalone.py",
|
||||
],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//tensorflow/contrib/learn",
|
||||
"//third_party/py/numpy",
|
||||
],
|
||||
)
|
@ -1,185 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""DNNRegressor with custom estimator for abalone dataset."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from six.moves import urllib
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
FLAGS = None
|
||||
|
||||
tf.logging.set_verbosity(tf.logging.INFO)
|
||||
|
||||
# Learning rate for the model
|
||||
LEARNING_RATE = 0.001
|
||||
|
||||
|
||||
def maybe_download(train_data, test_data, predict_data):
|
||||
"""Maybe downloads training data and returns train and test file names."""
|
||||
if train_data:
|
||||
train_file_name = train_data
|
||||
else:
|
||||
train_file = tempfile.NamedTemporaryFile(delete=False)
|
||||
urllib.request.urlretrieve(
|
||||
"http://download.tensorflow.org/data/abalone_train.csv",
|
||||
train_file.name)
|
||||
train_file_name = train_file.name
|
||||
train_file.close()
|
||||
print("Training data is downloaded to %s" % train_file_name)
|
||||
|
||||
if test_data:
|
||||
test_file_name = test_data
|
||||
else:
|
||||
test_file = tempfile.NamedTemporaryFile(delete=False)
|
||||
urllib.request.urlretrieve(
|
||||
"http://download.tensorflow.org/data/abalone_test.csv", test_file.name)
|
||||
test_file_name = test_file.name
|
||||
test_file.close()
|
||||
print("Test data is downloaded to %s" % test_file_name)
|
||||
|
||||
if predict_data:
|
||||
predict_file_name = predict_data
|
||||
else:
|
||||
predict_file = tempfile.NamedTemporaryFile(delete=False)
|
||||
urllib.request.urlretrieve(
|
||||
"http://download.tensorflow.org/data/abalone_predict.csv",
|
||||
predict_file.name)
|
||||
predict_file_name = predict_file.name
|
||||
predict_file.close()
|
||||
print("Prediction data is downloaded to %s" % predict_file_name)
|
||||
|
||||
return train_file_name, test_file_name, predict_file_name
|
||||
|
||||
|
||||
def model_fn(features, labels, mode, params):
|
||||
"""Model function for Estimator."""
|
||||
|
||||
# Connect the first hidden layer to input layer
|
||||
# (features["x"]) with relu activation
|
||||
first_hidden_layer = tf.layers.dense(features["x"], 10, activation=tf.nn.relu)
|
||||
|
||||
# Connect the second hidden layer to first hidden layer with relu
|
||||
second_hidden_layer = tf.layers.dense(
|
||||
first_hidden_layer, 10, activation=tf.nn.relu)
|
||||
|
||||
# Connect the output layer to second hidden layer (no activation fn)
|
||||
output_layer = tf.layers.dense(second_hidden_layer, 1)
|
||||
|
||||
# Reshape output layer to 1-dim Tensor to return predictions
|
||||
predictions = tf.reshape(output_layer, [-1])
|
||||
|
||||
# Provide an estimator spec for `ModeKeys.PREDICT`.
|
||||
if mode == tf.estimator.ModeKeys.PREDICT:
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode,
|
||||
predictions={"ages": predictions})
|
||||
|
||||
# Calculate loss using mean squared error
|
||||
loss = tf.losses.mean_squared_error(labels, predictions)
|
||||
|
||||
optimizer = tf.train.GradientDescentOptimizer(
|
||||
learning_rate=params["learning_rate"])
|
||||
train_op = optimizer.minimize(
|
||||
loss=loss, global_step=tf.train.get_global_step())
|
||||
|
||||
# Calculate root mean squared error as additional eval metric
|
||||
eval_metric_ops = {
|
||||
"rmse": tf.metrics.root_mean_squared_error(
|
||||
tf.cast(labels, tf.float64), predictions)
|
||||
}
|
||||
|
||||
# Provide an estimator spec for `ModeKeys.EVAL` and `ModeKeys.TRAIN` modes.
|
||||
return tf.estimator.EstimatorSpec(
|
||||
mode=mode,
|
||||
loss=loss,
|
||||
train_op=train_op,
|
||||
eval_metric_ops=eval_metric_ops)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
# Load datasets
|
||||
abalone_train, abalone_test, abalone_predict = maybe_download(
|
||||
FLAGS.train_data, FLAGS.test_data, FLAGS.predict_data)
|
||||
|
||||
# Training examples
|
||||
training_set = tf.contrib.learn.datasets.base.load_csv_without_header(
|
||||
filename=abalone_train, target_dtype=np.int, features_dtype=np.float64)
|
||||
|
||||
# Test examples
|
||||
test_set = tf.contrib.learn.datasets.base.load_csv_without_header(
|
||||
filename=abalone_test, target_dtype=np.int, features_dtype=np.float64)
|
||||
|
||||
# Set of 7 examples for which to predict abalone ages
|
||||
prediction_set = tf.contrib.learn.datasets.base.load_csv_without_header(
|
||||
filename=abalone_predict, target_dtype=np.int, features_dtype=np.float64)
|
||||
|
||||
# Set model params
|
||||
model_params = {"learning_rate": LEARNING_RATE}
|
||||
|
||||
# Instantiate Estimator
|
||||
nn = tf.estimator.Estimator(model_fn=model_fn, params=model_params)
|
||||
|
||||
train_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={"x": np.array(training_set.data)},
|
||||
y=np.array(training_set.target),
|
||||
num_epochs=None,
|
||||
shuffle=True)
|
||||
|
||||
# Train
|
||||
nn.train(input_fn=train_input_fn, steps=5000)
|
||||
|
||||
# Score accuracy
|
||||
test_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={"x": np.array(test_set.data)},
|
||||
y=np.array(test_set.target),
|
||||
num_epochs=1,
|
||||
shuffle=False)
|
||||
|
||||
ev = nn.evaluate(input_fn=test_input_fn)
|
||||
print("Loss: %s" % ev["loss"])
|
||||
print("Root Mean Squared Error: %s" % ev["rmse"])
|
||||
|
||||
# Print out predictions
|
||||
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
|
||||
x={"x": prediction_set.data},
|
||||
num_epochs=1,
|
||||
shuffle=False)
|
||||
predictions = nn.predict(input_fn=predict_input_fn)
|
||||
for i, p in enumerate(predictions):
|
||||
print("Prediction %s: %s" % (i + 1, p["ages"]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.register("type", "bool", lambda v: v.lower() == "true")
|
||||
parser.add_argument(
|
||||
"--train_data", type=str, default="", help="Path to the training data.")
|
||||
parser.add_argument(
|
||||
"--test_data", type=str, default="", help="Path to the test data.")
|
||||
parser.add_argument(
|
||||
"--predict_data",
|
||||
type=str,
|
||||
default="",
|
||||
help="Path to the prediction data.")
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,79 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""DNNRegressor with custom input_fn for Housing dataset."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import itertools
|
||||
|
||||
import pandas as pd
|
||||
import tensorflow as tf
|
||||
|
||||
tf.logging.set_verbosity(tf.logging.INFO)
|
||||
|
||||
COLUMNS = ["crim", "zn", "indus", "nox", "rm", "age",
|
||||
"dis", "tax", "ptratio", "medv"]
|
||||
FEATURES = ["crim", "zn", "indus", "nox", "rm",
|
||||
"age", "dis", "tax", "ptratio"]
|
||||
LABEL = "medv"
|
||||
|
||||
|
||||
def get_input_fn(data_set, num_epochs=None, shuffle=True):
|
||||
return tf.estimator.inputs.pandas_input_fn(
|
||||
x=pd.DataFrame({k: data_set[k].values for k in FEATURES}),
|
||||
y=pd.Series(data_set[LABEL].values),
|
||||
num_epochs=num_epochs,
|
||||
shuffle=shuffle)
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
# Load datasets
|
||||
training_set = pd.read_csv("boston_train.csv", skipinitialspace=True,
|
||||
skiprows=1, names=COLUMNS)
|
||||
test_set = pd.read_csv("boston_test.csv", skipinitialspace=True,
|
||||
skiprows=1, names=COLUMNS)
|
||||
|
||||
# Set of 6 examples for which to predict median house values
|
||||
prediction_set = pd.read_csv("boston_predict.csv", skipinitialspace=True,
|
||||
skiprows=1, names=COLUMNS)
|
||||
|
||||
# Feature cols
|
||||
feature_cols = [tf.feature_column.numeric_column(k) for k in FEATURES]
|
||||
|
||||
# Build 2 layer fully connected DNN with 10, 10 units respectively.
|
||||
regressor = tf.estimator.DNNRegressor(feature_columns=feature_cols,
|
||||
hidden_units=[10, 10],
|
||||
model_dir="/tmp/boston_model")
|
||||
|
||||
# Train
|
||||
regressor.train(input_fn=get_input_fn(training_set), steps=5000)
|
||||
|
||||
# Evaluate loss over one epoch of test_set.
|
||||
ev = regressor.evaluate(
|
||||
input_fn=get_input_fn(test_set, num_epochs=1, shuffle=False))
|
||||
loss_score = ev["loss"]
|
||||
print("Loss: {0:f}".format(loss_score))
|
||||
|
||||
# Print out predictions over a slice of prediction_set.
|
||||
y = regressor.predict(
|
||||
input_fn=get_input_fn(prediction_set, num_epochs=1, shuffle=False))
|
||||
# .predict() returns an iterator of dicts; convert to a list and print
|
||||
# predictions
|
||||
predictions = list(p["predictions"] for p in itertools.islice(y, 6))
|
||||
print("Predictions: {}".format(str(predictions)))
|
||||
|
||||
if __name__ == "__main__":
|
||||
tf.app.run()
|
@ -71,18 +71,6 @@ py_binary(
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "mnist_softmax",
|
||||
srcs = [
|
||||
"mnist_softmax.py",
|
||||
],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
":input_data",
|
||||
"//tensorflow:tensorflow_py",
|
||||
],
|
||||
)
|
||||
|
||||
# Note: We need to set the evironment variable to use CPU JIT.
|
||||
# The way to achieve this is via setting the following:
|
||||
# TF_XLA_FLAGS='--tf_xla_cpu_global_jit=true'
|
||||
@ -101,18 +89,6 @@ py_binary(
|
||||
],
|
||||
)
|
||||
|
||||
py_binary(
|
||||
name = "mnist_deep",
|
||||
srcs = [
|
||||
"mnist_deep.py",
|
||||
],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
":input_data",
|
||||
"//tensorflow:tensorflow_py",
|
||||
],
|
||||
)
|
||||
|
||||
py_test(
|
||||
name = "fully_connected_feed_test",
|
||||
size = "medium",
|
||||
|
@ -1,185 +0,0 @@
|
||||
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
|
||||
"""A deep MNIST classifier using convolutional layers.
|
||||
|
||||
See extensive documentation at
|
||||
https://www.tensorflow.org/get_started/mnist/pros
|
||||
"""
|
||||
# Disable linter warnings to maintain consistency with tutorial.
|
||||
# pylint: disable=invalid-name
|
||||
# pylint: disable=g-bad-import-order
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
import numpy
|
||||
|
||||
FLAGS = None
|
||||
|
||||
|
||||
def deepnn(x):
|
||||
"""deepnn builds the graph for a deep net for classifying digits.
|
||||
|
||||
Args:
|
||||
x: an input tensor with the dimensions (N_examples, 784), where 784 is the
|
||||
number of pixels in a standard MNIST image.
|
||||
|
||||
Returns:
|
||||
A tuple (y, keep_prob). y is a tensor of shape (N_examples, 10), with values
|
||||
equal to the logits of classifying the digit into one of 10 classes (the
|
||||
digits 0-9). keep_prob is a scalar placeholder for the probability of
|
||||
dropout.
|
||||
"""
|
||||
# Reshape to use within a convolutional neural net.
|
||||
# Last dimension is for "features" - there is only one here, since images are
|
||||
# grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.
|
||||
with tf.name_scope('reshape'):
|
||||
x_image = tf.reshape(x, [-1, 28, 28, 1])
|
||||
|
||||
# First convolutional layer - maps one grayscale image to 32 feature maps.
|
||||
with tf.name_scope('conv1'):
|
||||
W_conv1 = weight_variable([5, 5, 1, 32])
|
||||
b_conv1 = bias_variable([32])
|
||||
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
|
||||
|
||||
# Pooling layer - downsamples by 2X.
|
||||
with tf.name_scope('pool1'):
|
||||
h_pool1 = max_pool_2x2(h_conv1)
|
||||
|
||||
# Second convolutional layer -- maps 32 feature maps to 64.
|
||||
with tf.name_scope('conv2'):
|
||||
W_conv2 = weight_variable([5, 5, 32, 64])
|
||||
b_conv2 = bias_variable([64])
|
||||
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
|
||||
|
||||
# Second pooling layer.
|
||||
with tf.name_scope('pool2'):
|
||||
h_pool2 = max_pool_2x2(h_conv2)
|
||||
|
||||
# Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image
|
||||
# is down to 7x7x64 feature maps -- maps this to 1024 features.
|
||||
with tf.name_scope('fc1'):
|
||||
W_fc1 = weight_variable([7 * 7 * 64, 1024])
|
||||
b_fc1 = bias_variable([1024])
|
||||
|
||||
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
|
||||
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
|
||||
|
||||
# Dropout - controls the complexity of the model, prevents co-adaptation of
|
||||
# features.
|
||||
with tf.name_scope('dropout'):
|
||||
keep_prob = tf.placeholder(tf.float32)
|
||||
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
|
||||
|
||||
# Map the 1024 features to 10 classes, one for each digit
|
||||
with tf.name_scope('fc2'):
|
||||
W_fc2 = weight_variable([1024, 10])
|
||||
b_fc2 = bias_variable([10])
|
||||
|
||||
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
|
||||
return y_conv, keep_prob
|
||||
|
||||
|
||||
def conv2d(x, W):
|
||||
"""conv2d returns a 2d convolution layer with full stride."""
|
||||
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
|
||||
|
||||
|
||||
def max_pool_2x2(x):
|
||||
"""max_pool_2x2 downsamples a feature map by 2X."""
|
||||
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
|
||||
strides=[1, 2, 2, 1], padding='SAME')
|
||||
|
||||
|
||||
def weight_variable(shape):
|
||||
"""weight_variable generates a weight variable of a given shape."""
|
||||
initial = tf.truncated_normal(shape, stddev=0.1)
|
||||
return tf.Variable(initial)
|
||||
|
||||
|
||||
def bias_variable(shape):
|
||||
"""bias_variable generates a bias variable of a given shape."""
|
||||
initial = tf.constant(0.1, shape=shape)
|
||||
return tf.Variable(initial)
|
||||
|
||||
|
||||
def main(_):
|
||||
# Import data
|
||||
mnist = input_data.read_data_sets(FLAGS.data_dir)
|
||||
|
||||
# Create the model
|
||||
x = tf.placeholder(tf.float32, [None, 784])
|
||||
|
||||
# Define loss and optimizer
|
||||
y_ = tf.placeholder(tf.int64, [None])
|
||||
|
||||
# Build the graph for the deep net
|
||||
y_conv, keep_prob = deepnn(x)
|
||||
|
||||
with tf.name_scope('loss'):
|
||||
cross_entropy = tf.losses.sparse_softmax_cross_entropy(
|
||||
labels=y_, logits=y_conv)
|
||||
cross_entropy = tf.reduce_mean(cross_entropy)
|
||||
|
||||
with tf.name_scope('adam_optimizer'):
|
||||
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
|
||||
|
||||
with tf.name_scope('accuracy'):
|
||||
correct_prediction = tf.equal(tf.argmax(y_conv, 1), y_)
|
||||
correct_prediction = tf.cast(correct_prediction, tf.float32)
|
||||
accuracy = tf.reduce_mean(correct_prediction)
|
||||
|
||||
graph_location = tempfile.mkdtemp()
|
||||
print('Saving graph to: %s' % graph_location)
|
||||
train_writer = tf.summary.FileWriter(graph_location)
|
||||
train_writer.add_graph(tf.get_default_graph())
|
||||
|
||||
with tf.Session() as sess:
|
||||
sess.run(tf.global_variables_initializer())
|
||||
for i in range(20000):
|
||||
batch = mnist.train.next_batch(50)
|
||||
if i % 100 == 0:
|
||||
train_accuracy = accuracy.eval(feed_dict={
|
||||
x: batch[0], y_: batch[1], keep_prob: 1.0})
|
||||
print('step %d, training accuracy %g' % (i, train_accuracy))
|
||||
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
|
||||
|
||||
# compute in batches to avoid OOM on GPUs
|
||||
accuracy_l = []
|
||||
for _ in range(20):
|
||||
batch = mnist.test.next_batch(500, shuffle=False)
|
||||
accuracy_l.append(accuracy.eval(feed_dict={x: batch[0],
|
||||
y_: batch[1],
|
||||
keep_prob: 1.0}))
|
||||
print('test accuracy %g' % numpy.mean(accuracy_l))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--data_dir', type=str,
|
||||
default='/tmp/tensorflow/mnist/input_data',
|
||||
help='Directory for storing input data')
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,84 +0,0 @@
|
||||
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""A very simple MNIST classifier.
|
||||
|
||||
See extensive documentation at
|
||||
https://www.tensorflow.org/get_started/mnist/beginners
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
FLAGS = None
|
||||
|
||||
|
||||
def main(_):
|
||||
# Import data
|
||||
mnist = input_data.read_data_sets(FLAGS.data_dir)
|
||||
|
||||
# Create the model
|
||||
x = tf.placeholder(tf.float32, [None, 784])
|
||||
W = tf.Variable(tf.zeros([784, 10]))
|
||||
b = tf.Variable(tf.zeros([10]))
|
||||
y = tf.matmul(x, W) + b
|
||||
|
||||
# Define loss and optimizer
|
||||
y_ = tf.placeholder(tf.int64, [None])
|
||||
|
||||
# The raw formulation of cross-entropy,
|
||||
#
|
||||
# tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
|
||||
# reduction_indices=[1]))
|
||||
#
|
||||
# can be numerically unstable.
|
||||
#
|
||||
# So here we use tf.losses.sparse_softmax_cross_entropy on the raw
|
||||
# outputs of 'y', and then average across the batch.
|
||||
cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y_, logits=y)
|
||||
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
|
||||
|
||||
sess = tf.InteractiveSession()
|
||||
tf.global_variables_initializer().run()
|
||||
# Train
|
||||
for _ in range(1000):
|
||||
batch_xs, batch_ys = mnist.train.next_batch(100)
|
||||
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
|
||||
|
||||
# Test trained model
|
||||
correct_prediction = tf.equal(tf.argmax(y, 1), y_)
|
||||
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
|
||||
print(sess.run(
|
||||
accuracy, feed_dict={
|
||||
x: mnist.test.images,
|
||||
y_: mnist.test.labels
|
||||
}))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'--data_dir',
|
||||
type=str,
|
||||
default='/tmp/tensorflow/mnist/input_data',
|
||||
help='Directory for storing input data')
|
||||
FLAGS, unparsed = parser.parse_known_args()
|
||||
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
|
@ -1,25 +0,0 @@
|
||||
# Example Estimator model
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
licenses(["notice"]) # Apache 2.0
|
||||
|
||||
exports_files(["LICENSE"])
|
||||
|
||||
py_binary(
|
||||
name = "iris_monitors",
|
||||
srcs = [
|
||||
"iris_monitors.py",
|
||||
],
|
||||
data = [
|
||||
"iris_test.csv",
|
||||
"iris_training.csv",
|
||||
],
|
||||
srcs_version = "PY2AND3",
|
||||
deps = [
|
||||
"//tensorflow:tensorflow_py",
|
||||
"//third_party/py/numpy",
|
||||
],
|
||||
)
|
@ -1,92 +0,0 @@
|
||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
"""Model training for Iris data set using Validation Monitor."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
tf.logging.set_verbosity(tf.logging.INFO)
|
||||
|
||||
# Data sets
|
||||
IRIS_TRAINING = os.path.join(os.path.dirname(__file__), "iris_training.csv")
|
||||
IRIS_TEST = os.path.join(os.path.dirname(__file__), "iris_test.csv")
|
||||
|
||||
|
||||
def main(unused_argv):
|
||||
# Load datasets.
|
||||
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
|
||||
filename=IRIS_TRAINING, target_dtype=np.int, features_dtype=np.float32)
|
||||
test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
|
||||
filename=IRIS_TEST, target_dtype=np.int, features_dtype=np.float32)
|
||||
|
||||
validation_metrics = {
|
||||
"accuracy":
|
||||
tf.contrib.learn.MetricSpec(
|
||||
metric_fn=tf.contrib.metrics.streaming_accuracy,
|
||||
prediction_key="classes"),
|
||||
"precision":
|
||||
tf.contrib.learn.MetricSpec(
|
||||
metric_fn=tf.contrib.metrics.streaming_precision,
|
||||
prediction_key="classes"),
|
||||
"recall":
|
||||
tf.contrib.learn.MetricSpec(
|
||||
metric_fn=tf.contrib.metrics.streaming_recall,
|
||||
prediction_key="classes")
|
||||
}
|
||||
validation_monitor = tf.contrib.learn.monitors.ValidationMonitor(
|
||||
test_set.data,
|
||||
test_set.target,
|
||||
every_n_steps=50,
|
||||
metrics=validation_metrics,
|
||||
early_stopping_metric="loss",
|
||||
early_stopping_metric_minimize=True,
|
||||
early_stopping_rounds=200)
|
||||
|
||||
# Specify that all features have real-value data
|
||||
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
|
||||
|
||||
# Build 3 layer DNN with 10, 20, 10 units respectively.
|
||||
classifier = tf.contrib.learn.DNNClassifier(
|
||||
feature_columns=feature_columns,
|
||||
hidden_units=[10, 20, 10],
|
||||
n_classes=3,
|
||||
model_dir="/tmp/iris_model",
|
||||
config=tf.contrib.learn.RunConfig(save_checkpoints_secs=1))
|
||||
|
||||
# Fit model.
|
||||
classifier.fit(x=training_set.data,
|
||||
y=training_set.target,
|
||||
steps=2000,
|
||||
monitors=[validation_monitor])
|
||||
|
||||
# Evaluate accuracy.
|
||||
accuracy_score = classifier.evaluate(
|
||||
x=test_set.data, y=test_set.target)["accuracy"]
|
||||
print("Accuracy: {0:f}".format(accuracy_score))
|
||||
|
||||
# Classify two new flower samples.
|
||||
new_samples = np.array(
|
||||
[[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)
|
||||
y = list(classifier.predict(new_samples))
|
||||
print("Predictions: {}".format(str(y)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
tf.app.run()
|
@ -1,31 +0,0 @@
|
||||
30,4,setosa,versicolor,virginica
|
||||
5.9,3.0,4.2,1.5,1
|
||||
6.9,3.1,5.4,2.1,2
|
||||
5.1,3.3,1.7,0.5,0
|
||||
6.0,3.4,4.5,1.6,1
|
||||
5.5,2.5,4.0,1.3,1
|
||||
6.2,2.9,4.3,1.3,1
|
||||
5.5,4.2,1.4,0.2,0
|
||||
6.3,2.8,5.1,1.5,2
|
||||
5.6,3.0,4.1,1.3,1
|
||||
6.7,2.5,5.8,1.8,2
|
||||
7.1,3.0,5.9,2.1,2
|
||||
4.3,3.0,1.1,0.1,0
|
||||
5.6,2.8,4.9,2.0,2
|
||||
5.5,2.3,4.0,1.3,1
|
||||
6.0,2.2,4.0,1.0,1
|
||||
5.1,3.5,1.4,0.2,0
|
||||
5.7,2.6,3.5,1.0,1
|
||||
4.8,3.4,1.9,0.2,0
|
||||
5.1,3.4,1.5,0.2,0
|
||||
5.7,2.5,5.0,2.0,2
|
||||
5.4,3.4,1.7,0.2,0
|
||||
5.6,3.0,4.5,1.5,1
|
||||
6.3,2.9,5.6,1.8,2
|
||||
6.3,2.5,4.9,1.5,1
|
||||
5.8,2.7,3.9,1.2,1
|
||||
6.1,3.0,4.6,1.4,1
|
||||
5.2,4.1,1.5,0.1,0
|
||||
6.7,3.1,4.7,1.5,1
|
||||
6.7,3.3,5.7,2.5,2
|
||||
6.4,2.9,4.3,1.3,1
|
|
@ -1,121 +0,0 @@
|
||||
120,4,setosa,versicolor,virginica
|
||||
6.4,2.8,5.6,2.2,2
|
||||
5.0,2.3,3.3,1.0,1
|
||||
4.9,2.5,4.5,1.7,2
|
||||
4.9,3.1,1.5,0.1,0
|
||||
5.7,3.8,1.7,0.3,0
|
||||
4.4,3.2,1.3,0.2,0
|
||||
5.4,3.4,1.5,0.4,0
|
||||
6.9,3.1,5.1,2.3,2
|
||||
6.7,3.1,4.4,1.4,1
|
||||
5.1,3.7,1.5,0.4,0
|
||||
5.2,2.7,3.9,1.4,1
|
||||
6.9,3.1,4.9,1.5,1
|
||||
5.8,4.0,1.2,0.2,0
|
||||
5.4,3.9,1.7,0.4,0
|
||||
7.7,3.8,6.7,2.2,2
|
||||
6.3,3.3,4.7,1.6,1
|
||||
6.8,3.2,5.9,2.3,2
|
||||
7.6,3.0,6.6,2.1,2
|
||||
6.4,3.2,5.3,2.3,2
|
||||
5.7,4.4,1.5,0.4,0
|
||||
6.7,3.3,5.7,2.1,2
|
||||
6.4,2.8,5.6,2.1,2
|
||||
5.4,3.9,1.3,0.4,0
|
||||
6.1,2.6,5.6,1.4,2
|
||||
7.2,3.0,5.8,1.6,2
|
||||
5.2,3.5,1.5,0.2,0
|
||||
5.8,2.6,4.0,1.2,1
|
||||
5.9,3.0,5.1,1.8,2
|
||||
5.4,3.0,4.5,1.5,1
|
||||
6.7,3.0,5.0,1.7,1
|
||||
6.3,2.3,4.4,1.3,1
|
||||
5.1,2.5,3.0,1.1,1
|
||||
6.4,3.2,4.5,1.5,1
|
||||
6.8,3.0,5.5,2.1,2
|
||||
6.2,2.8,4.8,1.8,2
|
||||
6.9,3.2,5.7,2.3,2
|
||||
6.5,3.2,5.1,2.0,2
|
||||
5.8,2.8,5.1,2.4,2
|
||||
5.1,3.8,1.5,0.3,0
|
||||
4.8,3.0,1.4,0.3,0
|
||||
7.9,3.8,6.4,2.0,2
|
||||
5.8,2.7,5.1,1.9,2
|
||||
6.7,3.0,5.2,2.3,2
|
||||
5.1,3.8,1.9,0.4,0
|
||||
4.7,3.2,1.6,0.2,0
|
||||
6.0,2.2,5.0,1.5,2
|
||||
4.8,3.4,1.6,0.2,0
|
||||
7.7,2.6,6.9,2.3,2
|
||||
4.6,3.6,1.0,0.2,0
|
||||
7.2,3.2,6.0,1.8,2
|
||||
5.0,3.3,1.4,0.2,0
|
||||
6.6,3.0,4.4,1.4,1
|
||||
6.1,2.8,4.0,1.3,1
|
||||
5.0,3.2,1.2,0.2,0
|
||||
7.0,3.2,4.7,1.4,1
|
||||
6.0,3.0,4.8,1.8,2
|
||||
7.4,2.8,6.1,1.9,2
|
||||
5.8,2.7,5.1,1.9,2
|
||||
6.2,3.4,5.4,2.3,2
|
||||
5.0,2.0,3.5,1.0,1
|
||||
5.6,2.5,3.9,1.1,1
|
||||
6.7,3.1,5.6,2.4,2
|
||||
6.3,2.5,5.0,1.9,2
|
||||
6.4,3.1,5.5,1.8,2
|
||||
6.2,2.2,4.5,1.5,1
|
||||
7.3,2.9,6.3,1.8,2
|
||||
4.4,3.0,1.3,0.2,0
|
||||
7.2,3.6,6.1,2.5,2
|
||||
6.5,3.0,5.5,1.8,2
|
||||
5.0,3.4,1.5,0.2,0
|
||||
4.7,3.2,1.3,0.2,0
|
||||
6.6,2.9,4.6,1.3,1
|
||||
5.5,3.5,1.3,0.2,0
|
||||
7.7,3.0,6.1,2.3,2
|
||||
6.1,3.0,4.9,1.8,2
|
||||
4.9,3.1,1.5,0.1,0
|
||||
5.5,2.4,3.8,1.1,1
|
||||
5.7,2.9,4.2,1.3,1
|
||||
6.0,2.9,4.5,1.5,1
|
||||
6.4,2.7,5.3,1.9,2
|
||||
5.4,3.7,1.5,0.2,0
|
||||
6.1,2.9,4.7,1.4,1
|
||||
6.5,2.8,4.6,1.5,1
|
||||
5.6,2.7,4.2,1.3,1
|
||||
6.3,3.4,5.6,2.4,2
|
||||
4.9,3.1,1.5,0.1,0
|
||||
6.8,2.8,4.8,1.4,1
|
||||
5.7,2.8,4.5,1.3,1
|
||||
6.0,2.7,5.1,1.6,1
|
||||
5.0,3.5,1.3,0.3,0
|
||||
6.5,3.0,5.2,2.0,2
|
||||
6.1,2.8,4.7,1.2,1
|
||||
5.1,3.5,1.4,0.3,0
|
||||
4.6,3.1,1.5,0.2,0
|
||||
6.5,3.0,5.8,2.2,2
|
||||
4.6,3.4,1.4,0.3,0
|
||||
4.6,3.2,1.4,0.2,0
|
||||
7.7,2.8,6.7,2.0,2
|
||||
5.9,3.2,4.8,1.8,1
|
||||
5.1,3.8,1.6,0.2,0
|
||||
4.9,3.0,1.4,0.2,0
|
||||
4.9,2.4,3.3,1.0,1
|
||||
4.5,2.3,1.3,0.3,0
|
||||
5.8,2.7,4.1,1.0,1
|
||||
5.0,3.4,1.6,0.4,0
|
||||
5.2,3.4,1.4,0.2,0
|
||||
5.3,3.7,1.5,0.2,0
|
||||
5.0,3.6,1.4,0.2,0
|
||||
5.6,2.9,3.6,1.3,1
|
||||
4.8,3.1,1.6,0.2,0
|
||||
6.3,2.7,4.9,1.8,2
|
||||
5.7,2.8,4.1,1.3,1
|
||||
5.0,3.0,1.6,0.2,0
|
||||
6.3,3.3,6.0,2.5,2
|
||||
5.0,3.5,1.6,0.6,0
|
||||
5.5,2.6,4.4,1.2,1
|
||||
5.7,3.0,4.2,1.2,1
|
||||
4.4,2.9,1.4,0.2,0
|
||||
4.8,3.0,1.4,0.1,0
|
||||
5.5,2.4,3.7,1.0,1
|
|
Loading…
Reference in New Issue
Block a user