def testMultipleBatchesOfSizeOne(self):
with self.test_session() as sess:
# Create the queue that populates the predictions.
preds_queue = tf.FIFOQueue(2, dtypes=tf.float32, shapes=(1, 3))
_enqueue_vector(sess, preds_queue, [10, 8, 6])
_enqueue_vector(sess, preds_queue, [-4, 3, -1])
predictions = preds_queue.dequeue()
# Create the queue that populates the labels.
labels_queue = tf.FIFOQueue(2, dtypes=tf.float32, shapes=(1, 3))
_enqueue_vector(sess, labels_queue, [1, 3, 2])
_enqueue_vector(sess, labels_queue, [2, 4, 6])
labels = labels_queue.dequeue()
error, update_op = metrics.streaming_mean_squared_error(
predictions, labels)
sess.run(tf.initialize_local_variables())
sess.run(update_op)
self.assertAlmostEqual(208.0 / 6, sess.run(update_op), 5)
self.assertAlmostEqual(208.0 / 6, error.eval(), 5)
python类initialize_local_variables()的实例源码
def testValueTensorIsIdempotent(self):
predictions = tf.random_normal((10, 3), seed=1)
labels = tf.random_normal((10, 3), seed=2)
error, update_op = metrics.streaming_root_mean_squared_error(
predictions, labels)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
# Run several updates.
for _ in range(10):
sess.run(update_op)
# Then verify idempotency.
initial_error = error.eval()
for _ in range(10):
self.assertEqual(initial_error, error.eval())
def testValueTensorIsIdempotent(self):
labels = tf.random_normal((10, 3), seed=2)
predictions = labels * 0.5 + tf.random_normal((10, 3), seed=1) * 0.5
cov, update_op = metrics.streaming_covariance(predictions, labels)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
# Run several updates.
for _ in range(10):
sess.run(update_op)
# Then verify idempotency.
initial_cov = cov.eval()
for _ in range(10):
self.assertEqual(initial_cov, cov.eval())
def testValueTensorIsIdempotent(self):
labels = tf.random_normal((10, 3), seed=2)
predictions = labels * 0.5 + tf.random_normal((10, 3), seed=1) * 0.5
pearson_r, update_op = metrics.streaming_pearson_correlation(predictions,
labels)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
# Run several updates.
for _ in range(10):
sess.run(update_op)
# Then verify idempotency.
initial_r = pearson_r.eval()
for _ in range(10):
self.assertEqual(initial_r, pearson_r.eval())
def testSingleUpdateWithErrorAndWeights(self):
with self.test_session() as sess:
predictions = np.array([2, 4, 6, 8])
labels = np.array([1, 3, 2, 7])
weights = np.array([0, 1, 3, 1])
predictions_t = tf.constant(predictions, shape=(1, 4), dtype=tf.float32)
labels_t = tf.constant(labels, shape=(1, 4), dtype=tf.float32)
weights_t = tf.constant(weights, shape=(1, 4), dtype=tf.float32)
pearson_r, update_op = metrics.streaming_pearson_correlation(
predictions_t, labels_t, weights=weights_t)
p, l = _reweight(predictions, labels, weights)
cmat = np.cov(p, l)
expected_r = cmat[0, 1] / np.sqrt(cmat[0, 0] * cmat[1, 1])
sess.run(tf.initialize_local_variables())
self.assertAlmostEqual(expected_r, sess.run(update_op))
self.assertAlmostEqual(expected_r, pearson_r.eval())
def testValueTensorIsIdempotent(self):
predictions = tf.random_normal((10, 3), seed=1)
labels = tf.random_normal((10, 3), seed=2)
error, update_op = metrics.streaming_mean_cosine_distance(
predictions, labels, dim=1)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
# Run several updates.
for _ in range(10):
sess.run(update_op)
# Then verify idempotency.
initial_error = error.eval()
for _ in range(10):
self.assertEqual(initial_error, error.eval())
def testSingleUpdateWithError1(self):
np_labels = np.matrix(('1 0 0;'
'0 0 1;'
'0 1 0'))
np_predictions = np.matrix(('1 0 0;'
'0 0 -1;'
'1 0 0'))
predictions = tf.constant(np_predictions, shape=(3, 1, 3), dtype=tf.float32)
labels = tf.constant(np_labels, shape=(3, 1, 3), dtype=tf.float32)
error, update_op = metrics.streaming_mean_cosine_distance(
predictions, labels, dim=2)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
self.assertAlmostEqual(1, sess.run(update_op), 5)
self.assertAlmostEqual(1, error.eval(), 5)
def testSingleUpdateWithError2(self):
np_predictions = np.matrix((
'0.819031913261206 0.567041924552012 0.087465312324590;'
'-0.665139432070255 -0.739487441769973 -0.103671883216994;'
'0.707106781186548 -0.707106781186548 0'))
np_labels = np.matrix((
'0.819031913261206 0.567041924552012 0.087465312324590;'
'0.665139432070255 0.739487441769973 0.103671883216994;'
'0.707106781186548 0.707106781186548 0'))
predictions = tf.constant(np_predictions, shape=(3, 1, 3), dtype=tf.float32)
labels = tf.constant(np_labels, shape=(3, 1, 3), dtype=tf.float32)
error, update_op = metrics.streaming_mean_cosine_distance(
predictions, labels, dim=2)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
self.assertAlmostEqual(1.0, sess.run(update_op), 5)
self.assertAlmostEqual(1.0, error.eval(), 5)
def testSingleUpdateWithErrorAndWeights1(self):
np_predictions = np.matrix(('1 0 0;'
'0 0 -1;'
'1 0 0'))
np_labels = np.matrix(('1 0 0;'
'0 0 1;'
'0 1 0'))
predictions = tf.constant(np_predictions, shape=(3, 1, 3), dtype=tf.float32)
labels = tf.constant(np_labels, shape=(3, 1, 3), dtype=tf.float32)
weights = tf.constant([1, 0, 0], shape=(3, 1, 1), dtype=tf.float32)
error, update_op = metrics.streaming_mean_cosine_distance(
predictions, labels, dim=2, weights=weights)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
self.assertEqual(0, sess.run(update_op))
self.assertEqual(0, error.eval())
def testSingleUpdateWithErrorAndWeights2(self):
np_predictions = np.matrix(('1 0 0;'
'0 0 -1;'
'1 0 0'))
np_labels = np.matrix(('1 0 0;'
'0 0 1;'
'0 1 0'))
predictions = tf.constant(np_predictions, shape=(3, 1, 3), dtype=tf.float32)
labels = tf.constant(np_labels, shape=(3, 1, 3), dtype=tf.float32)
weights = tf.constant([0, 1, 1], shape=(3, 1, 1), dtype=tf.float32)
error, update_op = metrics.streaming_mean_cosine_distance(
predictions, labels, dim=2, weights=weights)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
self.assertEqual(1.5, update_op.eval())
self.assertEqual(1.5, error.eval())
def testOneUpdate(self):
with self.test_session() as sess:
values = tf.constant([2, 4, 6, 8], shape=(1, 4), dtype=tf.float32)
pcnt0, update_op0 = metrics.streaming_percentage_less(
values, 100, name='high')
pcnt1, update_op1 = metrics.streaming_percentage_less(
values, 7, name='medium')
pcnt2, update_op2 = metrics.streaming_percentage_less(
values, 1, name='low')
sess.run(tf.initialize_local_variables())
sess.run([update_op0, update_op1, update_op2])
pcnt0, pcnt1, pcnt2 = sess.run([pcnt0, pcnt1, pcnt2])
self.assertAlmostEqual(1.0, pcnt0, 5)
self.assertAlmostEqual(0.75, pcnt1, 5)
self.assertAlmostEqual(0.0, pcnt2, 5)
def testValueTensorIsIdempotent(self):
num_classes = 3
predictions = tf.random_uniform([10], maxval=num_classes,
dtype=tf.int64, seed=1)
labels = tf.random_uniform([10], maxval=num_classes,
dtype=tf.int64, seed=1)
miou, update_op = metrics.streaming_mean_iou(
predictions, labels, num_classes=num_classes)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
# Run several updates.
for _ in range(10):
sess.run(update_op)
# Then verify idempotency.
initial_miou = miou.eval()
for _ in range(10):
self.assertEqual(initial_miou, miou.eval())
def testUpdateOpEvalIsAccumulatedConfusionMatrix(self):
predictions = tf.concat(0,
[tf.constant(0, shape=[5]),
tf.constant(1, shape=[5])])
labels = tf.concat(0,
[tf.constant(0, shape=[3]),
tf.constant(1, shape=[7])])
num_classes = 2
with self.test_session() as sess:
miou, update_op = metrics.streaming_mean_iou(
predictions, labels, num_classes)
sess.run(tf.initialize_local_variables())
confusion_matrix = update_op.eval()
self.assertAllEqual([[3, 2], [0, 5]], confusion_matrix)
desired_miou = np.mean([3./5., 5./7.])
self.assertAlmostEqual(desired_miou, miou.eval())
def testResultsWithSomeMissing(self):
predictions = tf.concat(0, [tf.constant(0, shape=[5]),
tf.constant(1, shape=[5])])
labels = tf.concat(0, [tf.constant(0, shape=[3]),
tf.constant(1, shape=[7])])
num_classes = 2
mask = tf.concat(0, [tf.constant(False, shape=[9]),
tf.constant(True, shape=[1])])
weights = tf.concat(0, [tf.constant(0, shape=[1]),
tf.constant(1, shape=[9])])
with self.test_session() as sess:
miou, update_op = metrics.streaming_mean_iou(
predictions, labels, num_classes, ignore_mask=mask, weights=weights)
sess.run(tf.initialize_local_variables())
self.assertAllEqual([[2, 2], [0, 4]], update_op.eval())
desired_miou = np.mean([2./4., 4./6.])
self.assertAlmostEqual(desired_miou, miou.eval())
def testAggregateMultipleMetricsReturnsListsInOrder(self):
predictions = tf.ones((10, 4))
labels = tf.ones((10, 4)) * 3
value_tensors, update_ops = metrics.aggregate_metrics(
metrics.streaming_mean_absolute_error(
predictions, labels),
metrics.streaming_mean_squared_error(
predictions, labels))
self.assertEqual(len(value_tensors), 2)
self.assertEqual(len(update_ops), 2)
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
self.assertEqual(2, update_ops[0].eval())
self.assertEqual(4, update_ops[1].eval())
self.assertEqual(2, value_tensors[0].eval())
self.assertEqual(4, value_tensors[1].eval())
def testAggregateMultipleMetricsReturnsListsInOrder(self):
predictions = tf.ones((10, 4))
labels = tf.ones((10, 4)) * 3
names_to_values, names_to_updates = metrics.aggregate_metric_map(
{
'm1': metrics.streaming_mean_absolute_error(
predictions, labels),
'm2': metrics.streaming_mean_squared_error(
predictions, labels),
})
self.assertEqual(2, len(names_to_values))
self.assertEqual(2, len(names_to_updates))
with self.test_session() as sess:
sess.run(tf.initialize_local_variables())
self.assertEqual(2, names_to_updates['m1'].eval())
self.assertEqual(4, names_to_updates['m2'].eval())
self.assertEqual(2, names_to_values['m1'].eval())
self.assertEqual(4, names_to_values['m2'].eval())
def test_lm(self):
hps = get_test_hparams()
with tf.variable_scope("model"):
model = LM(hps)
with self.test_session() as sess:
tf.initialize_all_variables().run()
tf.initialize_local_variables().run()
loss = 1e5
for i in range(50):
x, y, w = simple_data_generator(hps.batch_size, hps.num_steps)
loss, _ = sess.run([model.loss, model.train_op], {model.x: x, model.y: y, model.w: w})
print("%d: %.3f %.3f" % (i, loss, np.exp(loss)))
if np.isnan(loss):
print("NaN detected")
break
self.assertLess(loss, 1.0)
wide_deep_evaluate_predict.py 文件源码
项目:provectus-final-project
作者: eds-uga
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def evaluate_and_prdict(model_dir):
"""
Method evaluate validation dataset and predict target class for test dataset
:param model_dir:
:return:
"""
m=build_estimator(model_dir=model_dir)
results = m.evaluate(input_fn=lambda: input_fn(5000,test_data), steps=2000)
for key in sorted(results):
print("%s: %s" % (key, results[key]))
y = m.predict(input_fn=lambda :input_fn_eval(5000,test_data),as_iterable=True)
file_test= open("prediction_final.txt", "w")
for x in y:
file_test.write('%s' % x+"\n")
with tf.Session() as sess:
init = tf.group(tf.initialize_all_variables(), tf.initialize_local_variables())
sess = tf.Session(config=tf.ConfigProto())
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
coord.request_stop()
coord.join(threads)
sess.close()
def test_input_producer(fname):
import pdb
pdb.set_trace()
with tf.Session() as sess:
strings = [b"to", b"be", b"or", b"not", b"to", b"be"]
num_epochs = 3
queue = tf.train.string_input_producer(
strings, num_epochs=num_epochs, shuffle=False)
dequeue_many = queue.dequeue_many(len(strings) * num_epochs)
dequeue = queue.dequeue()
tf.initialize_all_variables().run()
tf.initialize_local_variables().run()
threads = tf.train.start_queue_runners()
# No randomness, so just see repeated copies of the input.
output = dequeue_many.eval()
self.assertAllEqual(strings * num_epochs, output)
# Reached the limit.
with self.assertRaises(tf.errors.OutOfRangeError):
dequeue.eval()
for thread in threads:
thread.join()
def initialize_session(sess, task_params):
if task_params['verbose']:
print("Initalizing tensorflow session ...")
saver = tf.train.Saver()
if task_params['restore_from_checkpoint']:
saver.restore(
sess=sess,
save_path=task_params['save_path'])
if task_params['verbose']:
print("Restoring variables from '{}'".format(task_params['save_path']))
else:
sess.run(tf.initialize_all_variables())
sess.run(tf.initialize_local_variables())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
logdir=task_params['summaries_dir'] + '/train_' + time.strftime("%Y%m%d_%H-%M-%S")
train_writer = tf.train.SummaryWriter(logdir=logdir, graph=sess.graph)
summaries = tf.merge_all_summaries()
return coord, threads, saver, train_writer, summaries