本文整理汇总了Python中tensorflow.contrib.layers.python.layers.feature_column.sparse_column_with_keys函数的典型用法代码示例。如果您正苦于以下问题:Python sparse_column_with_keys函数的具体用法?Python sparse_column_with_keys怎么用?Python sparse_column_with_keys使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sparse_column_with_keys函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSharedEmbeddingColumn
def testSharedEmbeddingColumn(self):
a1 = fc.sparse_column_with_keys("a1", ["marlo", "omar", "stringer"])
a2 = fc.sparse_column_with_keys("a2", ["marlo", "omar", "stringer"])
b = fc.shared_embedding_columns([a1, a2], dimension=4, combiner="mean")
self.assertEqual(len(b), 2)
self.assertEqual(b[0].shared_embedding_name, "a1_a2_shared_embedding")
self.assertEqual(b[1].shared_embedding_name, "a1_a2_shared_embedding")
# Create a sparse id tensor for a1.
input_tensor_c1 = sparse_tensor_lib.SparseTensor(
indices=[[0, 0], [1, 1], [2, 2]], values=[0, 1, 2], dense_shape=[3, 3])
# Create a sparse id tensor for a2.
input_tensor_c2 = sparse_tensor_lib.SparseTensor(
indices=[[0, 0], [1, 1], [2, 2]], values=[0, 1, 2], dense_shape=[3, 3])
with variable_scope.variable_scope("run_1"):
b1 = feature_column_ops.input_from_feature_columns({
b[0]: input_tensor_c1
}, [b[0]])
b2 = feature_column_ops.input_from_feature_columns({
b[1]: input_tensor_c2
}, [b[1]])
with self.test_session() as sess:
sess.run(variables.global_variables_initializer())
b1_value = b1.eval()
b2_value = b2.eval()
for i in range(len(b1_value)):
self.assertAllClose(b1_value[i], b2_value[i])
# Test the case when a shared_embedding_name is explictly specified.
d = fc.shared_embedding_columns(
[a1, a2],
dimension=4,
combiner="mean",
shared_embedding_name="my_shared_embedding")
# a3 is a completely different sparse column with a1 and a2, but since the
# same shared_embedding_name is passed in, a3 will have the same embedding
# as a1 and a2
a3 = fc.sparse_column_with_keys("a3", ["cathy", "tom", "anderson"])
e = fc.shared_embedding_columns(
[a3],
dimension=4,
combiner="mean",
shared_embedding_name="my_shared_embedding")
with variable_scope.variable_scope("run_2"):
d1 = feature_column_ops.input_from_feature_columns({
d[0]: input_tensor_c1
}, [d[0]])
e1 = feature_column_ops.input_from_feature_columns({
e[0]: input_tensor_c1
}, [e[0]])
with self.test_session() as sess:
sess.run(variables.global_variables_initializer())
d1_value = d1.eval()
e1_value = e1.eval()
for i in range(len(d1_value)):
self.assertAllClose(d1_value[i], e1_value[i])
示例2: testSharedEmbeddingColumnDeepCopy
def testSharedEmbeddingColumnDeepCopy(self):
a1 = fc.sparse_column_with_keys("a1", ["marlo", "omar", "stringer"])
a2 = fc.sparse_column_with_keys("a2", ["marlo", "omar", "stringer"])
columns = fc.shared_embedding_columns(
[a1, a2], dimension=4, combiner="mean")
columns_copy = copy.deepcopy(columns)
self.assertEqual(
columns_copy[0].shared_embedding_name, "a1_a2_shared_embedding")
self.assertEqual(
columns_copy[1].shared_embedding_name, "a1_a2_shared_embedding")
示例3: testSharedEmbeddingColumnErrors
def testSharedEmbeddingColumnErrors(self):
# Tries passing in a string.
with self.assertRaises(TypeError):
invalid_string = "Invalid string."
fc.shared_embedding_columns(invalid_string, dimension=2, combiner="mean")
# Tries passing in a set of sparse columns.
with self.assertRaises(TypeError):
invalid_set = set([
fc.sparse_column_with_keys("a", ["foo", "bar"]),
fc.sparse_column_with_keys("b", ["foo", "bar"]),
])
fc.shared_embedding_columns(invalid_set, dimension=2, combiner="mean")
示例4: testFloat32WeightedSparseStringColumnDtypes
def testFloat32WeightedSparseStringColumnDtypes(self):
ids = fc.sparse_column_with_keys("ids", ["marlo", "omar", "stringer"])
weighted_ids = fc.weighted_sparse_column(ids, "weights")
self.assertDictEqual({
"ids": parsing_ops.VarLenFeature(dtypes.string),
"weights": parsing_ops.VarLenFeature(dtypes.float32)
}, weighted_ids.config)
示例5: testFloat32WeightedSparseInt32ColumnDtypes
def testFloat32WeightedSparseInt32ColumnDtypes(self):
ids = fc.sparse_column_with_keys("ids", [42, 1, -1000], dtype=dtypes.int32)
weighted_ids = fc.weighted_sparse_column(ids, "weights")
self.assertDictEqual({
"ids": parsing_ops.VarLenFeature(dtypes.int32),
"weights": parsing_ops.VarLenFeature(dtypes.float32)
}, weighted_ids.config)
示例6: testOneHotColumnDeepCopy
def testOneHotColumnDeepCopy(self):
a = fc.sparse_column_with_keys("a", ["a", "b", "c", "d"])
column = fc.one_hot_column(a)
column_copy = copy.deepcopy(column)
self.assertEqual(column_copy.sparse_id_column.name, "a")
self.assertEqual(column.name, "a_one_hot")
self.assertEqual(column.length, 4)
示例7: testWeightedSparseColumnDeepCopy
def testWeightedSparseColumnDeepCopy(self):
ids = fc.sparse_column_with_keys("ids", ["marlo", "omar", "stringer"])
weighted = fc.weighted_sparse_column(ids, "weights")
weighted_copy = copy.deepcopy(weighted)
self.assertEqual(weighted_copy.sparse_id_column.name, "ids")
self.assertEqual(weighted_copy.weight_column_name, "weights")
self.assertEqual(weighted_copy.name, "ids_weighted_by_weights")
示例8: test_exogenous_input
def test_exogenous_input(self):
"""Test that no errors are raised when using exogenous features."""
dtype = dtypes.float64
times = [1, 2, 3, 4, 5, 6]
values = [[0.01], [5.10], [5.21], [0.30], [5.41], [0.50]]
feature_a = [["off"], ["on"], ["on"], ["off"], ["on"], ["off"]]
sparse_column_a = feature_column.sparse_column_with_keys(
column_name="feature_a", keys=["on", "off"])
one_hot_a = layers.one_hot_column(sparse_id_column=sparse_column_a)
regressor = estimators.StructuralEnsembleRegressor(
periodicities=[],
num_features=1,
moving_average_order=0,
exogenous_feature_columns=[one_hot_a],
dtype=dtype)
features = {TrainEvalFeatures.TIMES: times,
TrainEvalFeatures.VALUES: values,
"feature_a": feature_a}
train_input_fn = input_pipeline.RandomWindowInputFn(
input_pipeline.NumpyReader(features),
window_size=6, batch_size=1)
regressor.train(input_fn=train_input_fn, steps=1)
eval_input_fn = input_pipeline.WholeDatasetInputFn(
input_pipeline.NumpyReader(features))
evaluation = regressor.evaluate(input_fn=eval_input_fn, steps=1)
predict_input_fn = input_pipeline.predict_continuation_input_fn(
evaluation, times=[[7, 8, 9]],
exogenous_features={"feature_a": [[["on"], ["off"], ["on"]]]})
regressor.predict(input_fn=predict_input_fn)
示例9: setUp
def setUp(self):
super(DynamicRnnEstimatorTest, self).setUp()
self.rnn_cell = core_rnn_cell_impl.BasicRNNCell(self.NUM_RNN_CELL_UNITS)
self.mock_target_column = MockTargetColumn(
num_label_columns=self.NUM_LABEL_COLUMNS)
location = feature_column.sparse_column_with_keys(
'location', keys=['west_side', 'east_side', 'nyc'])
location_onehot = feature_column.one_hot_column(location)
self.context_feature_columns = [location_onehot]
wire_cast = feature_column.sparse_column_with_keys(
'wire_cast', ['marlo', 'omar', 'stringer'])
wire_cast_embedded = feature_column.embedding_column(wire_cast, dimension=8)
measurements = feature_column.real_valued_column(
'measurements', dimension=2)
self.sequence_feature_columns = [measurements, wire_cast_embedded]
示例10: testMissingValueInOneHotColumnForSparseColumnWithKeys
def testMissingValueInOneHotColumnForSparseColumnWithKeys(self):
ids = fc.sparse_column_with_keys("ids", ["marlo", "omar", "stringer"])
one_hot = fc.one_hot_column(ids)
features = {"ids": constant_op.constant([["marlo", "unknown", "omar"]])}
one_hot_tensor = feature_column_ops.input_from_feature_columns(
features, [one_hot])
with self.test_session() as sess:
sess.run(variables.global_variables_initializer())
sess.run(lookup_ops.tables_initializer())
self.assertAllEqual([[1., 1., 0.]], one_hot_tensor.eval())
示例11: testOneHotColumn
def testOneHotColumn(self):
a = fc.sparse_column_with_keys("a", ["a", "b", "c", "d"])
onehot_a = fc.one_hot_column(a)
self.assertEqual(onehot_a.sparse_id_column.name, "a")
self.assertEqual(onehot_a.length, 4)
b = fc.sparse_column_with_hash_bucket(
"b", hash_bucket_size=100, combiner="sum")
onehot_b = fc.one_hot_column(b)
self.assertEqual(onehot_b.sparse_id_column.name, "b")
self.assertEqual(onehot_b.length, 100)
示例12: testSharedEmbeddingColumnDeterminism
def testSharedEmbeddingColumnDeterminism(self):
# Tests determinism in auto-generated shared_embedding_name.
sparse_id_columns = tuple([
fc.sparse_column_with_keys(k, ["foo", "bar"])
for k in ["07", "02", "00", "03", "05", "01", "09", "06", "04", "08"]
])
output = fc.shared_embedding_columns(
sparse_id_columns, dimension=2, combiner="mean")
self.assertEqual(len(output), 10)
for x in output:
self.assertEqual(x.shared_embedding_name,
"00_01_02_plus_7_others_shared_embedding")
示例13: testInt32WeightedSparseInt64ColumnDtypes
def testInt32WeightedSparseInt64ColumnDtypes(self):
ids = fc.sparse_column_with_keys("ids", [42, 1, -1000], dtype=dtypes.int64)
weighted_ids = fc.weighted_sparse_column(ids, "weights", dtype=dtypes.int32)
self.assertDictEqual({
"ids": parsing_ops.VarLenFeature(dtypes.int64),
"weights": parsing_ops.VarLenFeature(dtypes.int32)
}, weighted_ids.config)
with self.assertRaisesRegexp(ValueError,
"dtype is not convertible to float"):
weighted_ids = fc.weighted_sparse_column(
ids, "weights", dtype=dtypes.string)
示例14: testSparseColumnKeysDeepCopy
def testSparseColumnKeysDeepCopy(self):
"""Tests deepcopy of sparse_column_with_keys."""
column = fc.sparse_column_with_keys("a", keys=["key0", "key1", "key2"])
self.assertEqual("a", column.name)
column_copy = copy.deepcopy(column)
self.assertEqual("a", column_copy.name)
self.assertEqual(
fc._SparseIdLookupConfig( # pylint: disable=protected-access
keys=("key0", "key1", "key2"),
vocab_size=3,
default_value=-1),
column_copy.lookup_config)
self.assertFalse(column_copy.is_integerized)
示例15: testSharedEmbeddingColumnWithWeightedSparseColumn
def testSharedEmbeddingColumnWithWeightedSparseColumn(self):
# Tests creation of shared embeddings containing weighted sparse columns.
sparse_col = fc.sparse_column_with_keys("a1", ["marlo", "omar", "stringer"])
ids = fc.sparse_column_with_keys("ids", ["marlo", "omar", "stringer"])
weighted_sparse_col = fc.weighted_sparse_column(ids, "weights")
self.assertEqual(weighted_sparse_col.name, "ids_weighted_by_weights")
b = fc.shared_embedding_columns([sparse_col, weighted_sparse_col],
dimension=4, combiner="mean")
self.assertEqual(len(b), 2)
self.assertEqual(b[0].shared_embedding_name,
"a1_ids_weighted_by_weights_shared_embedding")
self.assertEqual(b[1].shared_embedding_name,
"a1_ids_weighted_by_weights_shared_embedding")
# Tries reversing order to check compatibility condition.
b = fc.shared_embedding_columns([weighted_sparse_col, sparse_col],
dimension=4, combiner="mean")
self.assertEqual(len(b), 2)
self.assertEqual(b[0].shared_embedding_name,
"a1_ids_weighted_by_weights_shared_embedding")
self.assertEqual(b[1].shared_embedding_name,
"a1_ids_weighted_by_weights_shared_embedding")
# Tries adding two weighted columns to check compatibility between them.
weighted_sparse_col_2 = fc.weighted_sparse_column(ids, "weights_2")
b = fc.shared_embedding_columns([weighted_sparse_col,
weighted_sparse_col_2],
dimension=4, combiner="mean")
self.assertEqual(len(b), 2)
self.assertEqual(
b[0].shared_embedding_name,
"ids_weighted_by_weights_ids_weighted_by_weights_2_shared_embedding"
)
self.assertEqual(
b[1].shared_embedding_name,
"ids_weighted_by_weights_ids_weighted_by_weights_2_shared_embedding"
)