本文整理汇总了Python中tensorflow.contrib.learn.python.learn.estimators.head._multi_class_head函数的典型用法代码示例。如果您正苦于以下问题:Python _multi_class_head函数的具体用法?Python _multi_class_head怎么用?Python _multi_class_head使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_multi_class_head函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testMultiClassWithInvalidNClass
def testMultiClassWithInvalidNClass(self):
try:
head_lib._multi_class_head(n_classes=1)
self.fail("Softmax with no n_classes did not raise error.")
except ValueError:
# Expected
pass
示例2: testTrain_withNoHeadWeights
def testTrain_withNoHeadWeights(self):
head1 = head_lib._multi_class_head(n_classes=3, label_name="label1",
head_name="head1")
head2 = head_lib._multi_class_head(n_classes=4, label_name="label2",
head_name="head2")
head = head_lib._multi_head([head1, head2])
logits = tf.constant([[-0.7, 0.2, .1, .1, .1, .1, .1]])
labels = {
"label1": tf.constant([1]),
"label2": tf.constant([1])
}
features = {"weights": tf.constant([2.0, 10.0])}
model_fn_ops = head.head_ops(features, labels,
tf.contrib.learn.ModeKeys.TRAIN,
_noop_train_op, logits=logits)
self.assertEquals(None, model_fn_ops.predictions)
self.assertTrue(model_fn_ops.loss is not None)
self.assertTrue(model_fn_ops.train_op is not None)
self.assertFalse(model_fn_ops.eval_metric_ops)
self.assertEquals(None, model_fn_ops.signature_fn)
self.assertEquals(None, model_fn_ops.output_alternatives)
with tf.Session() as sess:
self.assertAlmostEqual(2.224, sess.run(model_fn_ops.loss), places=3)
示例3: testEval
def testEval(self):
head1 = head_lib._multi_class_head(n_classes=3, label_name="label1",
head_name="head1")
head2 = head_lib._multi_class_head(n_classes=4, label_name="label2",
head_name="head2")
head = head_lib._multi_head([head1, head2], [1, .5])
logits = tf.constant([[-0.7, 0.2, .1, .1, .1, .1, .1]])
labels = {
"label1": tf.constant([1]),
"label2": tf.constant([1])
}
features = {"weights": tf.constant([2.0, 10.0])}
model_fn_ops = head.head_ops(features, labels,
tf.contrib.learn.ModeKeys.EVAL,
_noop_train_op, logits=logits)
self.assertTrue(model_fn_ops.predictions)
self.assertTrue(model_fn_ops.loss is not None)
self.assertEquals(None, model_fn_ops.train_op)
self.assertTrue(model_fn_ops.eval_metric_ops)
self.assertEquals(None, model_fn_ops.signature_fn)
self.assertEquals(None, model_fn_ops.output_alternatives)
metric_ops = model_fn_ops.eval_metric_ops
# Tests eval keys
self.assertTrue("accuracy/head1" in metric_ops.keys())
self.assertTrue("accuracy/head2" in metric_ops.keys())
示例4: testInfer
def testInfer(self):
head1 = head_lib._multi_class_head(
n_classes=3, label_name="label1", head_name="head1")
head2 = head_lib._multi_class_head(
n_classes=4, label_name="label2", head_name="head2")
head = head_lib._multi_head([head1, head2], [1, .5])
logits = constant_op.constant([[-0.7, 0.2, .1, .1, .1, .1, .1]])
labels = {
"label1": constant_op.constant([1]),
"label2": constant_op.constant([1])
}
features = {"weights": constant_op.constant([2.0, 10.0])}
model_fn_ops = head.head_ops(
features,
labels,
model_fn.ModeKeys.INFER,
_noop_train_op,
logits=logits)
self.assertTrue(model_fn_ops.predictions)
self.assertEquals(None, model_fn_ops.loss)
self.assertEquals(None, model_fn_ops.train_op)
self.assertFalse(model_fn_ops.eval_metric_ops)
self.assertEquals(None, model_fn_ops.signature_fn)
self.assertTrue(len(model_fn_ops.output_alternatives) == 2)
# Tests predictions keys
pred_keys = model_fn_ops.predictions.keys()
self.assertTrue(
("head1", prediction_key.PredictionKey.PROBABILITIES) in pred_keys)
self.assertTrue(
("head1", prediction_key.PredictionKey.CLASSES) in pred_keys)
self.assertTrue(
("head2", prediction_key.PredictionKey.PROBABILITIES) in pred_keys)
self.assertTrue(
("head2", prediction_key.PredictionKey.CLASSES) in pred_keys)
# Tests output alternative
out_alts = model_fn_ops.output_alternatives
self.assertEquals(constants.ProblemType.CLASSIFICATION,
out_alts["head1"][0])
self.assertTrue(prediction_key.PredictionKey.PROBABILITIES in
out_alts["head1"][1].keys())
self.assertTrue(
prediction_key.PredictionKey.CLASSES in out_alts["head1"][1].keys())
self.assertEquals(constants.ProblemType.CLASSIFICATION,
out_alts["head2"][0])
self.assertTrue(prediction_key.PredictionKey.PROBABILITIES in
out_alts["head2"][1].keys())
self.assertTrue(
prediction_key.PredictionKey.CLASSES in out_alts["head2"][1].keys())
示例5: testJointLinearModel
def testJointLinearModel(self):
"""Tests that loss goes down with training."""
def input_fn():
return {
'age':
sparse_tensor.SparseTensor(
values=['1'], indices=[[0, 0]], dense_shape=[1, 1]),
'language':
sparse_tensor.SparseTensor(
values=['english'], indices=[[0, 0]], dense_shape=[1, 1])
}, constant_op.constant([[1]])
language = feature_column.sparse_column_with_hash_bucket('language', 100)
age = feature_column.sparse_column_with_hash_bucket('age', 2)
head = head_lib._multi_class_head(n_classes=2)
classifier = _joint_linear_estimator(head, feature_columns=[age, language])
classifier.fit(input_fn=input_fn, steps=1000)
loss1 = classifier.evaluate(input_fn=input_fn, steps=1)['loss']
classifier.fit(input_fn=input_fn, steps=2000)
loss2 = classifier.evaluate(input_fn=input_fn, steps=1)['loss']
self.assertLess(loss2, loss1)
self.assertLess(loss2, 0.01)
示例6: get_conv_classifier
def get_conv_classifier():
n_classes = 5
feature_columns = [layers.real_valued_column("", dimension=3)]
# learning_rate = 1.0
# optimizer = AdagradOptimizer(learning_rate)
#
# learning_rate = 1.0
# optimizer = AdadeltaOptimizer(learning_rate=learning_rate)
# ~ 62.55%
learning_rate = 0.01
optimizer = AdamOptimizer(learning_rate, epsilon=0.1)
# learning_rate = 0.05
# optimizer = GradientDescentOptimizer(learning_rate)
# learning_rate = 0.1
# optimizer = RMSPropOptimizer(learning_rate, momentum=0.1)
# learning_rate = 0.1
# optimizer = FtrlOptimizer(learning_rate)
return SKCompat(Estimator(
model_fn=get_conv_model,
params={
'head': head_lib._multi_class_head( # pylint: disable=protected-access
n_classes,
enable_centered_bias=False),
'feature_columns': feature_columns,
'activation_fn': tf.nn.relu,
'learning_rate': learning_rate,
'optimizer': optimizer
},
model_dir='saved_model'))
示例7: testBinaryClassificationWithWeights
def testBinaryClassificationWithWeights(self):
n_classes = 2
head = head_lib._multi_class_head(
n_classes=n_classes, weight_column_name="label_weight")
with tf.Graph().as_default(), tf.Session():
weights = ((1.,), (0.,))
features = {"label_weight": tf.constant(weights)}
logits = tf.constant(self._logits)
labels = tf.constant(self._labels)
# logloss: z:label, x:logit
# z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
model_fn_ops = head.head_ops(features, labels,
tf.contrib.learn.ModeKeys.TRAIN,
_noop_train_op, logits=logits)
_assert_no_variables(self)
_assert_summary_tags(self, ["loss"])
expected_total_loss = .31326166
_assert_metrics(
self, expected_total_loss / len(weights), {
"accuracy": 1. / 1,
"accuracy/baseline_label_mean": 1. / 1,
"accuracy/threshold_0.500000_mean": 1. / 1,
"auc": 0. / 1,
"labels/actual_label_mean": 1. / 1,
"labels/prediction_mean": .731059, # softmax
# TODO(ptucker): Is this the correct eval loss, sum not average?
"loss": expected_total_loss,
"precision/positive_threshold_0.500000_mean": 1. / 1,
"recall/positive_threshold_0.500000_mean": 1. / 1,
}, model_fn_ops)
示例8: testRaisesNonEmbeddingColumn
def testRaisesNonEmbeddingColumn(self):
one_hot_language = tf.contrib.layers.one_hot_column(
tf.contrib.layers.sparse_column_with_hash_bucket('language', 10))
params = {
'feature_columns': [one_hot_language],
'head': head_lib._multi_class_head(2),
'hidden_units': [1],
# Set lr mult to 0. to keep embeddings constant.
'embedding_lr_multipliers': {
one_hot_language: 0.0
},
}
features = {
'language':
tf.SparseTensor(
values=['en', 'fr', 'zh'],
indices=[[0, 0], [1, 0], [2, 0]],
dense_shape=[3, 1]),
}
labels = tf.constant([[0], [0], [0]], dtype=tf.int32)
with self.assertRaisesRegexp(
ValueError, 'can only be defined for embedding columns'):
dnn._dnn_model_fn(features, labels,
tf.contrib.learn.ModeKeys.TRAIN, params)
示例9: testMultiClass
def testMultiClass(self):
head = head_lib._multi_class_head(n_classes=3)
with tf.Graph().as_default(), tf.Session() as sess:
logits = tf.constant([[1.0, 0.0, 0.0]])
labels = tf.constant([2])
# logloss: z:label, x:logit
# z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
model_fn_ops = head.head_ops({}, labels, tf.contrib.learn.ModeKeys.TRAIN, _noop_train_op, logits=logits)
self.assertAlmostEqual(1.5514446, sess.run(model_fn_ops.loss))
示例10: testDNNModel
def testDNNModel(self):
"""Tests multi-class classification using matrix data as input."""
cont_features = [tf.contrib.layers.real_valued_column("feature", dimension=4)]
head = head_lib._multi_class_head(n_classes=3)
classifier = DNNEstimator(head, feature_columns=cont_features, hidden_units=[3, 3])
classifier.fit(input_fn=_iris_input_fn, steps=1000)
classifier.evaluate(input_fn=_iris_input_fn, steps=100)
示例11: testBinaryClassification
def testBinaryClassification(self):
head = head_lib._multi_class_head(n_classes=2)
with tf.Graph().as_default(), tf.Session() as sess:
logits = tf.constant([[1.0], [1.0]])
labels = tf.constant([[1.0], [0.0]])
# logloss: z:label, x:logit
# z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
model_fn_ops = head.head_ops({}, labels, tf.contrib.learn.ModeKeys.TRAIN, _noop_train_op, logits=logits)
self.assertAlmostEqual(0.81326175, sess.run(model_fn_ops.loss), delta=1e-6)
示例12: testErrorInSparseTensorLabels
def testErrorInSparseTensorLabels(self):
head = head_lib._multi_class_head(n_classes=2)
with tf.Graph().as_default():
prediction = tf.constant([[1.0], [1.0], [3.0]])
labels = tf.SparseTensor(
indices=tf.constant([[0, 0], [1, 0], [2, 0]], dtype=tf.int64),
values=tf.constant([0, 1, 1]),
shape=[3, 1],
)
with self.assertRaisesRegexp(ValueError, "SparseTensor is not supported as labels."):
head.head_ops({}, labels, tf.contrib.learn.ModeKeys.TRAIN, _noop_train_op, logits=prediction)
示例13: testBinaryClassification
def testBinaryClassification(self):
head = head_lib._multi_class_head(n_classes=2)
with tf.Graph().as_default(), tf.Session() as sess:
logits = tf.constant([[1.], [1.]])
targets = tf.constant([[1.], [0.]])
# logloss: z:label, x:logit
# z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
model_fn_ops = head.head_ops({}, targets,
tf.contrib.learn.ModeKeys.TRAIN,
None, logits=logits)
self.assertAlmostEqual(.81326163, sess.run(model_fn_ops.loss))
示例14: testErrorInSparseTensorTarget
def testErrorInSparseTensorTarget(self):
head = head_lib._multi_class_head(n_classes=2)
with tf.Graph().as_default():
prediction = tf.constant([[1.], [1.], [3.]])
targets = tf.SparseTensor(
indices=tf.constant([[0, 0], [1, 0], [2, 0]], dtype=tf.int64),
values=tf.constant([0, 1, 1]),
shape=[3, 1])
with self.assertRaisesRegexp(
ValueError, "SparseTensor is not supported as a target"):
head.head_ops({}, targets, tf.contrib.learn.ModeKeys.TRAIN, None,
logits=prediction)
示例15: testMultiClassWithWeight
def testMultiClassWithWeight(self):
head = head_lib._multi_class_head(n_classes=3, weight_column_name="label_weight")
with tf.Graph().as_default(), tf.Session() as sess:
features = {"label_weight": tf.constant([0.1])}
logits = tf.constant([[1.0, 0.0, 0.0]])
labels = tf.constant([2])
# logloss: z:label, x:logit
# z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
model_fn_ops = head.head_ops(
features, labels, tf.contrib.learn.ModeKeys.TRAIN, _noop_train_op, logits=logits
)
self.assertAlmostEqual(0.15514446, sess.run(model_fn_ops.loss))