我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用tensorflow.python.framework.dtypes.int64()。
def in_top_k(predictions, targets, k): """Returns whether the `targets` are in the top `k` `predictions`. Arguments: predictions: A tensor of shape `(batch_size, classes)` and type `float32`. targets: A 1D tensor of length `batch_size` and type `int32` or `int64`. k: An `int`, number of top elements to consider. Returns: A 1D tensor of length `batch_size` and type `bool`. `output[i]` is `True` if `predictions[i, targets[i]]` is within top-`k` values of `predictions[i]`. """ return nn.in_top_k(predictions, targets, k) # CONVOLUTIONS
def _tensor_shape_tensor_conversion_function(s, dtype=None, name=None, as_ref=False): _ = as_ref if not s.is_fully_defined(): raise ValueError( "Cannot convert a partially known TensorShape to a Tensor: %s" % s) s_list = s.as_list() int64_value = 0 for dim in s_list: if dim >= 2**31: int64_value = dim break if dtype is not None: if dtype not in (dtypes.int32, dtypes.int64): raise TypeError("Cannot convert a TensorShape to dtype: %s" % dtype) if dtype == dtypes.int32 and int64_value: raise ValueError("Cannot convert a TensorShape to dtype int32; " "a dimension is too large (%s)" % int64_value) else: dtype = dtypes.int64 if int64_value else dtypes.int32 if name is None: name = "shape_as_tensor" return constant(s_list, dtype=dtype, name=name)
def assert_scalar_int(tensor): """Assert `tensor` is 0-D, of type `tf.int32` or `tf.int64`. Args: tensor: Tensor to test. Returns: `tensor`, for chaining. Raises: ValueError: if `tensor` is not 0-D, of type `tf.int32` or `tf.int64`. """ data_type = tensor.dtype if data_type.base_dtype not in [dtypes.int32, dtypes.int64]: raise ValueError('Unexpected type %s for %s.' % (data_type, tensor.name)) shape = tensor.get_shape() if shape.ndims != 0: raise ValueError('Unexpected shape %s for %s.' % (shape, tensor.name)) return tensor
def create_global_step(graph=None): """Create global step tensor in graph. Args: graph: The graph in which to create the global step. If missing, use default graph. Returns: Global step tensor. Raises: ValueError: if global step key is already defined. """ graph = ops.get_default_graph() if graph is None else graph if get_global_step(graph) is not None: raise ValueError('"global_step" already exists.') # Create in proper graph and base name_scope. with graph.as_default() as g, g.name_scope(None): collections = [ops.GraphKeys.VARIABLES, ops.GraphKeys.GLOBAL_STEP] return variable(ops.GraphKeys.GLOBAL_STEP, shape=[], dtype=dtypes.int64, initializer=init_ops.zeros_initializer, trainable=False, collections=collections)
def one_hot_matrix(tensor_in, num_classes, on_value=1.0, off_value=0.0): """Encodes indices from given tensor as one-hot tensor. TODO(ilblackdragon): Ideally implementation should be part of TensorFlow with Eigen-native operation. Args: tensor_in: Input tensor of shape [N1, N2]. num_classes: Number of classes to expand index into. on_value: Tensor or float, value to fill-in given index. off_value: Tensor or float, value to fill-in everything else. Returns: Tensor of shape [N1, N2, num_classes] with 1.0 for each id in original tensor. """ return array_ops_.one_hot( math_ops.cast(tensor_in, dtypes.int64), num_classes, on_value, off_value)
def _maybe_select_class_id(labels, predictions_idx, selected_id=None): """If class ID is specified, filter all other classes. Args: labels: `int64` `Tensor` or `SparseTensor` with shape [D1, ... DN, num_labels], where N >= 1 and num_labels is the number of target classes for the associated prediction. Commonly, N=1 and `labels` has shape [batch_size, num_labels]. [D1, ... DN] must match `predictions_idx`. predictions_idx: `int64` `Tensor` of class IDs, with shape [D1, ... DN, k] where N >= 1. Commonly, N=1 and `predictions_idx` has shape [batch size, k]. selected_id: Int id to select. Returns: Tuple of `labels` and `predictions_idx`, possibly with classes removed. """ if selected_id is None: return labels, predictions_idx return (_select_class_id(labels, selected_id), _select_class_id(predictions_idx, selected_id))
def _make_auc_histograms(boolean_labels, scores, score_range, nbins): """Create histogram tensors from one batch of labels/scores.""" with variable_scope.variable_scope( None, 'make_auc_histograms', [boolean_labels, scores, nbins]): # Histogram of scores for records in this batch with True label. hist_true = histogram_ops.histogram_fixed_width( array_ops.boolean_mask(scores, boolean_labels), score_range, nbins=nbins, dtype=dtypes.int64, name='hist_true') # Histogram of scores for records in this batch with False label. hist_false = histogram_ops.histogram_fixed_width( array_ops.boolean_mask(scores, math_ops.logical_not(boolean_labels)), score_range, nbins=nbins, dtype=dtypes.int64, name='hist_false') return hist_true, hist_false
def test_decode_example_with_iInt64_tensor(self): np_array = np.random.randint(1, 10, size=(2, 3, 1)) example = example_pb2.Example(features=feature_pb2.Features(feature={ 'array': self._encode_int64_feature(np_array), })) serialized_example = example.SerializeToString() with self.test_session(): serialized_example = array_ops.reshape(serialized_example, shape=[]) keys_to_features = { 'array': parsing_ops.FixedLenFeature(np_array.shape, dtypes.int64) } items_to_handlers = {'array': tfexample_decoder.Tensor('array'), } decoder = TFExampleDecoder(keys_to_features, items_to_handlers) [tf_array] = decoder.decode(serialized_example, ['array']) self.assertAllEqual(tf_array.eval(), np_array)
def test_decode_example_with_var_len_tensor(self): np_array = np.array([[[1], [2], [3]], [[4], [5], [6]]]) example = example_pb2.Example(features=feature_pb2.Features(feature={ 'labels': self._encode_int64_feature(np_array), })) serialized_example = example.SerializeToString() with self.test_session(): serialized_example = array_ops.reshape(serialized_example, shape=[]) keys_to_features = { 'labels': parsing_ops.VarLenFeature(dtype=dtypes.int64), } items_to_handlers = {'labels': tfexample_decoder.Tensor('labels'), } decoder = TFExampleDecoder(keys_to_features, items_to_handlers) [tf_labels] = decoder.decode(serialized_example, ['labels']) labels = tf_labels.eval() self.assertAllEqual(labels, np_array.flatten())
def test_decode_example_with_fix_len_tensor_with_shape(self): np_array = np.array([[1, 2, 3], [4, 5, 6]]) example = example_pb2.Example(features=feature_pb2.Features(feature={ 'labels': self._encode_int64_feature(np_array), })) serialized_example = example.SerializeToString() with self.test_session(): serialized_example = array_ops.reshape(serialized_example, shape=[]) keys_to_features = { 'labels': parsing_ops.FixedLenFeature(np_array.shape, dtype=dtypes.int64), } items_to_handlers = { 'labels': tfexample_decoder.Tensor('labels', shape=np_array.shape), } decoder = TFExampleDecoder(keys_to_features, items_to_handlers) [tf_labels] = decoder.decode(serialized_example, ['labels']) labels = tf_labels.eval() self.assertAllEqual(labels, np_array)
def test_decode_example_with_var_len_tensor_to_dense(self): np_array = np.array([[1, 2, 3], [4, 5, 6]]) example = example_pb2.Example(features=feature_pb2.Features(feature={ 'labels': self._encode_int64_feature(np_array), })) serialized_example = example.SerializeToString() with self.test_session(): serialized_example = array_ops.reshape(serialized_example, shape=[]) keys_to_features = { 'labels': parsing_ops.VarLenFeature(dtype=dtypes.int64), } items_to_handlers = { 'labels': tfexample_decoder.Tensor( 'labels', shape=np_array.shape), } decoder = TFExampleDecoder(keys_to_features, items_to_handlers) [tf_labels] = decoder.decode(serialized_example, ['labels']) labels = tf_labels.eval() self.assertAllEqual(labels, np_array)
def test_decode_example_with_sparse_tensor(self): np_indices = np.array([[1], [2], [5]]) np_values = np.array([0.1, 0.2, 0.6]).astype('f') example = example_pb2.Example(features=feature_pb2.Features(feature={ 'indices': self._encode_int64_feature(np_indices), 'values': self._encode_float_feature(np_values), })) serialized_example = example.SerializeToString() with self.test_session(): serialized_example = array_ops.reshape(serialized_example, shape=[]) keys_to_features = { 'indices': parsing_ops.VarLenFeature(dtype=dtypes.int64), 'values': parsing_ops.VarLenFeature(dtype=dtypes.float32), } items_to_handlers = {'labels': tfexample_decoder.SparseTensor()} decoder = TFExampleDecoder(keys_to_features, items_to_handlers) [tf_labels] = decoder.decode(serialized_example, ['labels']) labels = tf_labels.eval() self.assertAllEqual(labels.indices, np_indices) self.assertAllEqual(labels.values, np_values) self.assertAllEqual(labels.dense_shape, np_values.shape)
def test_decode_example_with_sparse_tensor_to_dense(self): np_indices = np.array([1, 2, 5]) np_values = np.array([0.1, 0.2, 0.6]).astype('f') np_shape = np.array([6]) np_dense = np.array([0.0, 0.1, 0.2, 0.0, 0.0, 0.6]).astype('f') example = example_pb2.Example(features=feature_pb2.Features(feature={ 'indices': self._encode_int64_feature(np_indices), 'values': self._encode_float_feature(np_values), })) serialized_example = example.SerializeToString() with self.test_session(): serialized_example = array_ops.reshape(serialized_example, shape=[]) keys_to_features = { 'indices': parsing_ops.VarLenFeature(dtype=dtypes.int64), 'values': parsing_ops.VarLenFeature(dtype=dtypes.float32), } items_to_handlers = { 'labels': tfexample_decoder.SparseTensor(shape=np_shape, densify=True), } decoder = TFExampleDecoder(keys_to_features, items_to_handlers) [tf_labels] = decoder.decode(serialized_example, ['labels']) labels = tf_labels.eval() self.assertAllClose(labels, np_dense)
def _create_tfrecord_dataset(tmpdir): if not gfile.Exists(tmpdir): gfile.MakeDirs(tmpdir) data_sources = test_utils.create_tfrecord_files(tmpdir, num_files=1) keys_to_features = { 'image/encoded': tf.FixedLenFeature(shape=(), dtype=dtypes.string, default_value=''), 'image/format': tf.FixedLenFeature(shape=(), dtype=dtypes.string, default_value='jpeg'), 'image/class/label': tf.FixedLenFeature( shape=[1], dtype=dtypes.int64, default_value=array_ops.zeros([1], dtype=dtypes.int64)) } items_to_handlers = { 'image': tfslim.tfexample_decoder.Image(), 'label': tfslim.tfexample_decoder.Tensor('image/class/label'), } decoder = TFExampleDecoder(keys_to_features, items_to_handlers) return Dataset( data_sources=data_sources, reader=tf.TFRecordReader, decoder=decoder, num_samples=100)
def assert_scalar_int(tensor, name=None): """Assert `tensor` is 0-D, of type `tf.int32` or `tf.int64`. Args: tensor: `Tensor` to test. name: Name of the op and of the new `Tensor` if one is created. Returns: `tensor`, for chaining. Raises: ValueError: if `tensor` is not 0-D, of type `tf.int32` or `tf.int64`. """ with ops.name_scope(name, 'assert_scalar_int', [tensor]) as name_scope: tensor = ops.convert_to_tensor(tensor) data_type = tensor.dtype if data_type.base_dtype not in [dtypes.int32, dtypes.int64]: raise ValueError('Unexpected type %s for %s.' % (data_type, tensor.name)) return assert_scalar(tensor, name=name_scope)
def __init__(self, example_indices, feature_indices, feature_values): """Creates a `SparseFeatureColumn` representation. Args: example_indices: A 1-D int64 tensor of shape `[N]`. Also, accepts python lists, or numpy arrays. feature_indices: A 1-D int64 tensor of shape `[N]`. Also, accepts python lists, or numpy arrays. feature_values: An optional 1-D tensor float tensor of shape `[N]`. Also, accepts python lists, or numpy arrays. Returns: A `SparseFeatureColumn` """ with name_scope(None, 'SparseFeatureColumn', [example_indices, feature_indices]): self._example_indices = internal_convert_to_tensor( example_indices, name='example_indices', dtype=dtypes.int64) self._feature_indices = internal_convert_to_tensor( feature_indices, name='feature_indices', dtype=dtypes.int64) self._feature_values = None if feature_values is not None: with name_scope(None, 'SparseFeatureColumn', [feature_values]): self._feature_values = internal_convert_to_tensor( feature_values, name='feature_values', dtype=dtypes.float32)
def testHashTable(self): with self.test_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) table = lookup.HashTable( lookup.KeyValueTensorInitializer(keys, values), default_val) table.init.run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant(["brain", "salad", "tank"]) output = table.lookup(input_string) self.assertAllEqual([3], output.get_shape()) result = output.eval() self.assertAllEqual([0, 1, -1], result)
def testHashTableFindHighRank(self): with self.test_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) table = lookup.HashTable( lookup.KeyValueTensorInitializer(keys, values), default_val) table.init.run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant( [["brain", "salad"], ["tank", "tarkus"]]) output = table.lookup(input_string) result = output.eval() self.assertAllEqual([[0, 1], [-1, -1]], result)
def testHashTableInitWithPythonArrays(self): with self.test_session(): default_val = -1 keys = ["brain", "salad", "surgery"] values = [0, 1, 2] table = lookup.HashTable( lookup.KeyValueTensorInitializer( keys, values, value_dtype=dtypes.int64), default_val) table.init.run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant(["brain", "salad", "tank"]) output = table.lookup(input_string) result = output.eval() self.assertAllEqual([0, 1, -1], result)
def testHashTableInitWithNumPyArrays(self): with self.test_session(): default_val = -1 keys = np.array(["brain", "salad", "surgery"], dtype=np.str) values = np.array([0, 1, 2], dtype=np.int64) table = lookup.HashTable( lookup.KeyValueTensorInitializer(keys, values), default_val) table.init.run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant(["brain", "salad", "tank"]) output = table.lookup(input_string) result = output.eval() self.assertAllEqual([0, 1, -1], result)
def testHashTableWithSparseTensorInput(self): with self.test_session() as sess: default_val = constant_op.constant(-1, dtypes.int64) keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) table = lookup.HashTable( lookup.KeyValueTensorInitializer(keys, values), default_val) table.init.run() sp_indices = [[0, 0], [0, 1], [1, 0]] sp_shape = [2, 2] input_tensor = sparse_tensor.SparseTensor( constant_op.constant(sp_indices, dtypes.int64), constant_op.constant(["brain", "salad", "tank"]), constant_op.constant(sp_shape, dtypes.int64)) output = table.lookup(input_tensor) out_indices, out_values, out_shape = sess.run(output) self.assertAllEqual([0, 1, -1], out_values) self.assertAllEqual(sp_indices, out_indices) self.assertAllEqual(sp_shape, out_shape)
def testSignatureMismatch(self): with self.test_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) table = lookup.HashTable( lookup.KeyValueTensorInitializer(keys, values), default_val) table.init.run() input_string = constant_op.constant([1, 2, 3], dtypes.int64) with self.assertRaises(TypeError): table.lookup(input_string) with self.assertRaises(TypeError): lookup.HashTable( lookup.KeyValueTensorInitializer(keys, values), "UNK")
def testMutableHashTableDuplicateInsert(self): with self.test_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery", "brain"]) values = constant_op.constant([0, 1, 2, 3], dtypes.int64) table = lookup.MutableHashTable(dtypes.string, dtypes.int64, default_val) self.assertAllEqual(0, table.size().eval()) table.insert(keys, values).run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant(["brain", "salad", "tank"]) output = table.lookup(input_string) result = output.eval() self.assertAllEqual([3, 1, -1], result)
def testMutableHashTableFindHighRank(self): with self.test_session(): default_val = -1 keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) table = lookup.MutableHashTable(dtypes.string, dtypes.int64, default_val) table.insert(keys, values).run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant( [["brain", "salad"], ["tank", "tarkus"]]) output = table.lookup(input_string) self.assertAllEqual([2, 2], output.get_shape()) result = output.eval() self.assertAllEqual([[0, 1], [-1, -1]], result)
def testMutableHashTableOfTensorsFindHighRank(self): with self.test_session(): default_val = constant_op.constant([-1, -1, -1], dtypes.int64) keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([[0, 1, 2], [2, 3, 4], [4, 5, 6]], dtypes.int64) table = lookup.MutableHashTable(dtypes.string, dtypes.int64, default_val) table.insert(keys, values).run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant( [["brain", "salad"], ["tank", "tarkus"]]) output = table.lookup(input_string) self.assertAllEqual([2, 2, 3], output.get_shape()) result = output.eval() self.assertAllEqual( [[[0, 1, 2], [2, 3, 4]], [[-1, -1, -1], [-1, -1, -1]]], result)
def testMutableHashTableWithTensorDefault(self): with self.test_session(): default_val = constant_op.constant(-1, dtypes.int64) keys = constant_op.constant(["brain", "salad", "surgery"]) values = constant_op.constant([0, 1, 2], dtypes.int64) table = lookup.MutableHashTable(dtypes.string, dtypes.int64, default_val) table.insert(keys, values).run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant(["brain", "salad", "tank"]) output = table.lookup(input_string) result = output.eval() self.assertAllEqual([0, 1, -1], result)
def testMutableHashTableInt64String(self): with self.test_session(): default_val = "n/a" keys = constant_op.constant([0, 1, 2], dtypes.int64) values = constant_op.constant(["brain", "salad", "surgery"]) table = lookup.MutableHashTable(dtypes.int64, dtypes.string, default_val) self.assertAllEqual(0, table.size().eval()) table.insert(keys, values).run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant([0, 1, 3], dtypes.int64) output = table.lookup(input_string) result = output.eval() self.assertAllEqual((b"brain", b"salad", b"n/a"), result)
def testMapInt64ToFloat(self): for float_dtype in [dtypes.float32, dtypes.float64]: with self.test_session(): keys = constant_op.constant([11, 12, 13], dtypes.int64) values = constant_op.constant([0.0, 1.1, 2.2], float_dtype) default_value = constant_op.constant(-1.5, float_dtype) table = lookup.MutableDenseHashTable( dtypes.int64, float_dtype, default_value=default_value, empty_key=0) self.assertAllEqual(0, table.size().eval()) table.insert(keys, values).run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant([11, 12, 15], dtypes.int64) output = table.lookup(input_string) self.assertAllEqual([3], output.get_shape()) result = output.eval() self.assertAllClose([0, 1.1, -1.5], result)
def testReprobe(self): with self.test_session(): # Insert 6 keys into a table with 8 buckets. # The values are chosen to make sure collisions occur when using GCC STL keys = constant_op.constant([11, 12, 13, 19, 20, 21], dtypes.int64) values = constant_op.constant([51, 52, 53, 54, 55, 56], dtypes.int64) table = lookup.MutableDenseHashTable( dtypes.int64, dtypes.int64, default_value=-1, empty_key=0, initial_num_buckets=8) self.assertAllEqual(0, table.size().eval()) table.insert(keys, values).run() self.assertAllEqual(6, table.size().eval()) input_string = constant_op.constant([10, 11, 12, 13, 14, 19, 20, 21, 22], dtypes.int64) output = table.lookup(input_string) self.assertAllEqual([9], output.get_shape()) result = output.eval() self.assertAllEqual([-1, 51, 52, 53, -1, 54, 55, 56, -1], result)
def testCustomEmptyKey(self): with self.test_session(): keys = constant_op.constant([11, 0, 13], dtypes.int64) values = constant_op.constant([0, 1, 2], dtypes.int64) table = lookup.MutableDenseHashTable( dtypes.int64, dtypes.int64, default_value=-1, empty_key=12) self.assertAllEqual(0, table.size().eval()) table.insert(keys, values).run() self.assertAllEqual(3, table.size().eval()) input_string = constant_op.constant([11, 0, 15], dtypes.int64) output = table.lookup(input_string) self.assertAllEqual([3], output.get_shape()) result = output.eval() self.assertAllEqual([0, 1, -1], result)
def testInitializeIndexTable(self): vocabulary_file = self._createVocabFile("one_column_2.txt") with self.test_session(): default_value = "UNK" key_index = lookup.TextFileIndex.LINE_NUMBER value_index = lookup.TextFileIndex.WHOLE_LINE table = lookup.HashTable( lookup.TextFileInitializer(vocabulary_file, dtypes.int64, key_index, dtypes.string, value_index), default_value) table.init.run() input_values = constant_op.constant([0, 1, 2, 3], dtypes.int64) output = table.lookup(input_values) result = output.eval() self.assertAllEqual([b"brain", b"salad", b"surgery", b"UNK"], result)
def testMultiColumn(self): vocabulary_file = os.path.join(self.get_temp_dir(), "three_columns.txt") with open(vocabulary_file, "w") as f: f.write("\n".join(["0\tbrain\t1", "1\tsalad\t5", "2\tsurgery\t6"]) + "\n") with self.test_session(): default_value = -1 key_index = 1 value_index = 2 table = lookup.HashTable( lookup.TextFileInitializer(vocabulary_file, dtypes.string, key_index, dtypes.int64, value_index), default_value) table.init.run() input_string = constant_op.constant(["brain", "salad", "surgery"]) output = table.lookup(input_string) result = output.eval() self.assertAllEqual([1, 5, 6], result)
def testInvalidFilenames(self): vocabulary_file = self._createVocabFile("filename_shape.txt") with self.test_session(): default_value = -1 # Invalid data type other_type = constant_op.constant(1) with self.assertRaises(ValueError): lookup.HashTable( lookup.TextFileInitializer( other_type, dtypes.string, lookup.TextFileIndex.WHOLE_LINE, dtypes.int64, lookup.TextFileIndex.LINE_NUMBER), default_value) # Non-scalar filename filenames = constant_op.constant([vocabulary_file, vocabulary_file]) with self.assertRaises(ValueError): lookup.HashTable( lookup.TextFileInitializer( filenames, dtypes.string, lookup.TextFileIndex.WHOLE_LINE, dtypes.int64, lookup.TextFileIndex.LINE_NUMBER), default_value)
def testIdToStringTable(self): vocab_file = self._createVocabFile("feat_to_id_1.txt") with self.test_session(): default_value = "UNK" vocab_size = 3 table = lookup.HashTable( lookup.TextFileStringTableInitializer( vocab_file, vocab_size=vocab_size), default_value) table.init.run() input_values = constant_op.constant([0, 1, 2, 3], dtypes.int64) out = table.lookup(input_values) self.assertAllEqual([b"brain", b"salad", b"surgery", b"UNK"], out.eval()) self.assertEquals(vocab_size, table.size().eval())
def test_integer_mixed_string_dense(self): """Tests mixed dense inputs. """ op = sparse_feature_cross_op.sparse_feature_cross([ constant_op.constant([[11, 333], [55555, 999999]], dtypes.int64), constant_op.constant([['batch1-FC2-F1', 'batch1-FC2-F2'], ['batch2-FC2-F1', 'batch2-FC2-F2']], dtypes.string), ]) expected_out = self._sparse_tensor([[ '11_X_batch1-FC2-F1', '11_X_batch1-FC2-F2', '333_X_batch1-FC2-F1', '333_X_batch1-FC2-F2' ], [ '55555_X_batch2-FC2-F1', '55555_X_batch2-FC2-F2', '999999_X_batch2-FC2-F1', '999999_X_batch2-FC2-F2' ]]) with self.test_session() as sess: self._assert_sparse_tensor_equals(expected_out, sess.run(op))
def testSparseColumnWithHashBucket(self): hashed_sparse = feature_column.sparse_column_with_hash_bucket("wire", 10) wire_tensor = sparse_tensor.SparseTensor( values=["omar", "stringer", "marlo"], indices=[[0, 0], [1, 0], [1, 1]], dense_shape=[2, 2]) features = {"wire": wire_tensor} # Test transform features. output = feature_column_ops.transform_features( features=features, feature_columns=[hashed_sparse]) self.assertEqual(len(output), 1) self.assertIn(hashed_sparse, output) with self.test_session(): self.assertEqual(output[hashed_sparse].values.dtype, dtypes.int64) self.assertTrue( all(x < 10 and x >= 0 for x in output[hashed_sparse].values.eval())) self.assertAllEqual(output[hashed_sparse].indices.eval(), wire_tensor.indices.eval()) self.assertAllEqual(output[hashed_sparse].dense_shape.eval(), wire_tensor.dense_shape.eval())
def testSparseIntColumnWithHashBucket(self): """Tests a sparse column with int values.""" hashed_sparse = feature_column.sparse_column_with_hash_bucket( "wire", 10, dtype=dtypes.int64) wire_tensor = sparse_tensor.SparseTensor( values=[101, 201, 301], indices=[[0, 0], [1, 0], [1, 1]], dense_shape=[2, 2]) features = {"wire": wire_tensor} # Test transform features. output = feature_column_ops.transform_features( features=features, feature_columns=[hashed_sparse]) self.assertEqual(len(output), 1) self.assertIn(hashed_sparse, output) with self.test_session(): self.assertEqual(output[hashed_sparse].values.dtype, dtypes.int64) self.assertTrue( all(x < 10 and x >= 0 for x in output[hashed_sparse].values.eval())) self.assertAllEqual(output[hashed_sparse].indices.eval(), wire_tensor.indices.eval()) self.assertAllEqual(output[hashed_sparse].dense_shape.eval(), wire_tensor.dense_shape.eval())
def testSparseColumnWithKeysWithDenseInputTensor(self): keys_sparse = feature_column.sparse_column_with_keys( "wire", ["marlo", "omar", "stringer", "rick"]) wire_tensor = constant_op.constant( [["omar", "stringer"], ["marlo", "rick"]]) features = {"wire": wire_tensor} output = feature_column_ops._Transformer(features).transform(keys_sparse) with self.test_session(): data_flow_ops.tables_initializer().run() # While the input is a dense Tensor, the output should be a SparseTensor. self.assertIsInstance(output, sparse_tensor.SparseTensor) self.assertEqual(output.dtype, dtypes.int64) self.assertAllEqual(output.values.eval(), [1, 2, 0, 3]) self.assertAllEqual(output.indices.eval(), [[0, 0], [0, 1], [1, 0], [1, 1]]) self.assertAllEqual(output.dense_shape.eval(), [2, 2])
def testSparseColumnWithVocabulary(self): vocabulary_file = os.path.join(self.get_temp_dir(), "movies.txt") with open(vocabulary_file, "w") as f: f.write("\n".join(["marlo", "omar", "stringer"]) + "\n") vocab_sparse = feature_column.sparse_column_with_vocabulary_file( "wire", vocabulary_file, vocab_size=3) wire_tensor = sparse_tensor.SparseTensor( values=["omar", "stringer", "marlo"], indices=[[0, 0], [1, 0], [1, 1]], dense_shape=[2, 2]) features = {"wire": wire_tensor} output = feature_column_ops.transform_features( features=features, feature_columns=[vocab_sparse]) self.assertEqual(len(output), 1) self.assertIn(vocab_sparse, output) with self.test_session(): data_flow_ops.tables_initializer().run() self.assertEqual(output[vocab_sparse].values.dtype, dtypes.int64) self.assertAllEqual(output[vocab_sparse].values.eval(), [1, 2, 0]) self.assertAllEqual(output[vocab_sparse].indices.eval(), wire_tensor.indices.eval()) self.assertAllEqual(output[vocab_sparse].dense_shape.eval(), wire_tensor.dense_shape.eval())
def testSparseColumnWithVocabularyWithDenseInputTensor(self): vocabulary_file = os.path.join(self.get_temp_dir(), "movies.txt") with open(vocabulary_file, "w") as f: f.write("\n".join(["marlo", "omar", "stringer"]) + "\n") vocab_sparse = feature_column.sparse_column_with_vocabulary_file( "wire", vocabulary_file, vocab_size=3) wire_tensor = constant_op.constant( [["omar", "stringer"], ["marlo", "omar"]]) features = {"wire": wire_tensor} output = feature_column_ops.transform_features( features=features, feature_columns=[vocab_sparse]) self.assertEqual(len(output), 1) self.assertIn(vocab_sparse, output) with self.test_session(): data_flow_ops.tables_initializer().run() self.assertEqual(output[vocab_sparse].values.dtype, dtypes.int64) self.assertAllEqual(output[vocab_sparse].values.eval(), [1, 2, 0, 1]) self.assertAllEqual(output[vocab_sparse].indices.eval(), [[0, 0], [0, 1], [1, 0], [1, 1]]) self.assertAllEqual(output[vocab_sparse].dense_shape.eval(), [2, 2])
def testSparseIntColumnWithVocabulary(self): """Tests a sparse integer column with vocabulary.""" vocabulary_file = os.path.join(self.get_temp_dir(), "courses.txt") with open(vocabulary_file, "w") as f: f.write("\n".join(["101", "201", "301"]) + "\n") vocab_sparse = feature_column.sparse_column_with_vocabulary_file( "wire", vocabulary_file, vocab_size=3, dtype=dtypes.int64) wire_tensor = sparse_tensor.SparseTensor( values=[201, 301, 101], indices=[[0, 0], [1, 0], [1, 1]], dense_shape=[2, 2]) features = {"wire": wire_tensor} output = feature_column_ops.transform_features( features=features, feature_columns=[vocab_sparse]) self.assertEqual(len(output), 1) self.assertIn(vocab_sparse, output) with self.test_session(): data_flow_ops.tables_initializer().run() self.assertEqual(output[vocab_sparse].values.dtype, dtypes.int64) self.assertAllEqual(output[vocab_sparse].values.eval(), [1, 2, 0]) self.assertAllEqual(output[vocab_sparse].indices.eval(), wire_tensor.indices.eval()) self.assertAllEqual(output[vocab_sparse].dense_shape.eval(), wire_tensor.dense_shape.eval())
def testSparseIntColumnWithVocabularyWithDenseInputTensor(self): """Tests a sparse integer column with vocabulary.""" vocabulary_file = os.path.join(self.get_temp_dir(), "courses.txt") with open(vocabulary_file, "w") as f: f.write("\n".join(["101", "201", "301"]) + "\n") vocab_sparse = feature_column.sparse_column_with_vocabulary_file( "wire", vocabulary_file, vocab_size=3, dtype=dtypes.int64) wire_tensor = constant_op.constant([[201, 301], [101, 201]]) features = {"wire": wire_tensor} output = feature_column_ops.transform_features( features=features, feature_columns=[vocab_sparse]) self.assertEqual(len(output), 1) self.assertIn(vocab_sparse, output) with self.test_session(): data_flow_ops.tables_initializer().run() self.assertEqual(output[vocab_sparse].values.dtype, dtypes.int64) self.assertAllEqual(output[vocab_sparse].values.eval(), [1, 2, 0, 1]) self.assertAllEqual(output[vocab_sparse].indices.eval(), [[0, 0], [0, 1], [1, 0], [1, 1]]) self.assertAllEqual(output[vocab_sparse].dense_shape.eval(), [2, 2])
def testCrossWithBucketizedColumn(self): price_bucket = feature_column.bucketized_column( feature_column.real_valued_column("price"), boundaries=[0., 10., 100.]) country = feature_column.sparse_column_with_hash_bucket( "country", hash_bucket_size=5) country_price = feature_column.crossed_column( [country, price_bucket], hash_bucket_size=15) features = { "price": constant_op.constant([[20.]]), "country": sparse_tensor.SparseTensor( values=["US", "SV"], indices=[[0, 0], [0, 1]], dense_shape=[1, 2]) } # Test transform features. output = feature_column_ops.transform_features( features=features, feature_columns=[country_price]) self.assertEqual(len(output), 1) self.assertIn(country_price, output) with self.test_session(): self.assertEqual(output[country_price].values.dtype, dtypes.int64) self.assertTrue( all(x < 15 and x >= 0 for x in output[country_price].values.eval()))
def _ids_and_weights_2d(self): # Each row demonstrates a test case: # Row 0: multiple valid ids, 1 invalid id, weighted mean # Row 1: all ids are invalid (leaving no valid ids after pruning) # Row 2: no ids to begin with # Row 3: single id # Row 4: all ids have <=0 weight indices = [[0, 0], [0, 1], [0, 2], [1, 0], [3, 0], [4, 0], [4, 1]] ids = [0, 1, -1, -1, 2, 0, 1] weights = [1.0, 2.0, 1.0, 1.0, 3.0, 0.0, -0.5] shape = [5, 4] sparse_ids = sparse_tensor_lib.SparseTensor( constant_op.constant(indices, dtypes.int64), constant_op.constant(ids, dtypes.int64), constant_op.constant(shape, dtypes.int64)) sparse_weights = sparse_tensor_lib.SparseTensor( constant_op.constant(indices, dtypes.int64), constant_op.constant(weights, dtypes.float32), constant_op.constant(shape, dtypes.int64)) return sparse_ids, sparse_weights
def testValueTensorIsIdempotent(self): predictions = random_ops.random_uniform( (10, 3), maxval=3, dtype=dtypes_lib.int64, seed=1) labels = random_ops.random_uniform( (10, 3), maxval=3, dtype=dtypes_lib.int64, seed=1) accuracy, update_op = metrics.streaming_accuracy(predictions, labels) with self.test_session() as sess: sess.run(variables.local_variables_initializer()) # Run several updates. for _ in range(10): sess.run(update_op) # Then verify idempotency. initial_accuracy = accuracy.eval() for _ in range(10): self.assertEqual(initial_accuracy, accuracy.eval())