本文整理汇总了Python中tensorflow.sparse_reshape方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.sparse_reshape方法的具体用法?Python tensorflow.sparse_reshape怎么用?Python tensorflow.sparse_reshape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.sparse_reshape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testFeedPartialShapes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedPartialShapes(self):
with self.test_session(use_gpu=False):
# Incorporate new rank into shape information if known
sp_input = self._SparseTensorPlaceholder()
sp_output = tf.sparse_reshape(sp_input, [2, 3, 5])
self.assertListEqual(sp_output.indices.get_shape().as_list(), [None, 3])
self.assertListEqual(sp_output.shape.get_shape().as_list(), [3])
# Incorporate known shape information about input indices in output
# indices
sp_input = self._SparseTensorPlaceholder()
sp_input.indices.set_shape([5, None])
sp_output = tf.sparse_reshape(sp_input, [2, 3, 5])
self.assertListEqual(sp_output.indices.get_shape().as_list(), [5, 3])
self.assertListEqual(sp_output.shape.get_shape().as_list(), [3])
# Even if new_shape has no shape information, we know the ranks of
# output indices and shape
sp_input = self._SparseTensorPlaceholder()
sp_input.indices.set_shape([5, None])
new_shape = tf.placeholder(tf.int64)
sp_output = tf.sparse_reshape(sp_input, new_shape)
self.assertListEqual(sp_output.indices.get_shape().as_list(), [5, None])
self.assertListEqual(sp_output.shape.get_shape().as_list(), [None])
示例2: get_sp_topk
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def get_sp_topk(adj_pred, sp_adj_train, nb_nodes, k):
"""Returns binary matrix with topK."""
_, indices = tf.nn.top_k(tf.reshape(adj_pred, (-1,)), k)
indices = tf.reshape(tf.cast(indices, tf.int64), (-1, 1))
sp_adj_pred = tf.SparseTensor(
indices=indices,
values=tf.ones(k),
dense_shape=(nb_nodes * nb_nodes,))
sp_adj_pred = tf.sparse_reshape(sp_adj_pred,
shape=(nb_nodes, nb_nodes, 1))
sp_adj_train = tf.SparseTensor(
indices=sp_adj_train.indices,
values=tf.ones_like(sp_adj_train.values),
dense_shape=sp_adj_train.dense_shape)
sp_adj_train = tf.sparse_reshape(sp_adj_train,
shape=(nb_nodes, nb_nodes, 1))
sp_adj_pred = tf.sparse_concat(
sp_inputs=[sp_adj_pred, sp_adj_train], axis=-1)
return tf.sparse_reduce_max(sp_adj_pred, axis=-1)
示例3: tensors_to_item
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def tensors_to_item(self, keys_to_tensors):
"""Maps the given dictionary of tensors to a concatenated list of bboxes.
Args:
keys_to_tensors: a mapping of TF-Example keys to parsed tensors.
Returns:
[time, num_boxes, 4] tensor of bounding box coordinates, in order
[y_min, x_min, y_max, x_max]. Whether the tensor is a SparseTensor
or a dense Tensor is determined by the return_dense parameter. Empty
positions in the sparse tensor are filled with -1.0 values.
"""
sides = []
for key in self._full_keys:
value = keys_to_tensors[key]
expanded_dims = tf.concat(
[tf.to_int64(tf.shape(value)),
tf.constant([1], dtype=tf.int64)], 0)
side = tf.sparse_reshape(value, expanded_dims)
sides.append(side)
bounding_boxes = tf.sparse_concat(2, sides)
if self._return_dense:
bounding_boxes = tf.sparse_tensor_to_dense(
bounding_boxes, default_value=self._default_value)
return bounding_boxes
示例4: testSameShape
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testSameShape(self):
with self.test_session(use_gpu=False) as sess:
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(input_val, [5, 6])
output_val = sess.run(sp_output)
self.assertAllEqual(output_val.indices, input_val.indices)
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, input_val.shape)
示例5: testFeedSameShape
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedSameShape(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(sp_input, [5, 6])
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, input_val.indices)
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, input_val.shape)
示例6: testFeedSameShapeWithInferredDim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedSameShapeWithInferredDim(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(sp_input, [-1, 6])
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, input_val.indices)
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, input_val.shape)
示例7: testFeedNewShapeSameRank
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedNewShapeSameRank(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(sp_input, [3, 10])
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, np.array([
[0, 0], [0, 6], [0, 9], [1, 0], [2, 0], [2, 1]
]))
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, [3, 10])
示例8: testFeedNewShapeSameRankWithInferredDim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedNewShapeSameRankWithInferredDim(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(sp_input, [3, -1])
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, np.array([
[0, 0], [0, 6], [0, 9], [1, 0], [2, 0], [2, 1]
]))
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, [3, 10])
示例9: testFeedUpRank
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedUpRank(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(sp_input, [2, 3, 5])
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, np.array([
[0, 0, 0], [0, 1, 1], [0, 1, 4], [0, 2, 0], [1, 1, 0], [1, 1, 1]
]))
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, [2, 3, 5])
示例10: testFeedUpRankWithInferredDim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedUpRankWithInferredDim(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(sp_input, [2, -1, 5])
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, np.array([
[0, 0, 0], [0, 1, 1], [0, 1, 4], [0, 2, 0], [1, 1, 0], [1, 1, 1]
]))
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, [2, 3, 5])
示例11: testFeedDownRank
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedDownRank(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_2x3x4()
sp_output = tf.sparse_reshape(sp_input, [6, 4])
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, np.array([
[0, 1], [1, 0], [1, 2], [3, 3], [4, 1], [4, 3], [5, 2]
]))
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, [6, 4])
示例12: testFeedDownRankWithInferredDim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedDownRankWithInferredDim(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_2x3x4()
sp_output = tf.sparse_reshape(sp_input, [6, -1])
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, np.array([
[0, 1], [1, 0], [1, 2], [3, 3], [4, 1], [4, 3], [5, 2]
]))
self.assertAllEqual(output_val.values, input_val.values)
self.assertAllEqual(output_val.shape, [6, 4])
示例13: testFeedMultipleInferredDims
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedMultipleInferredDims(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(sp_input, [4, -1, -1])
with self.assertRaisesOpError("only one output shape size may be -1"):
sess.run(sp_output, {sp_input: input_val})
示例14: testFeedMismatchedSizesWithInferredDim
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedMismatchedSizesWithInferredDim(self):
with self.test_session(use_gpu=False) as sess:
sp_input = self._SparseTensorPlaceholder()
input_val = self._SparseTensorValue_5x6()
sp_output = tf.sparse_reshape(sp_input, [4, -1])
with self.assertRaisesOpError("requested shape requires a multiple"):
sess.run(sp_output, {sp_input: input_val})
示例15: testFeedDenseReshapeSemantics
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_reshape [as 别名]
def testFeedDenseReshapeSemantics(self):
with self.test_session(use_gpu=False) as sess:
# Compute a random rank-5 initial shape and new shape, randomly sparsify
# it, and check that the output of SparseReshape has the same semantics
# as a dense reshape.
factors = np.array([2] * 4 + [3] * 4 + [5] * 4) # 810k total elements
orig_rank = np.random.randint(2, 7)
orig_map = np.random.randint(orig_rank, size=factors.shape)
orig_shape = [np.prod(factors[orig_map == d]) for d in range(orig_rank)]
new_rank = np.random.randint(2, 7)
new_map = np.random.randint(new_rank, size=factors.shape)
new_shape = [np.prod(factors[new_map == d]) for d in range(new_rank)]
orig_dense = np.random.uniform(size=orig_shape)
orig_indices = np.transpose(np.nonzero(orig_dense < 0.5))
orig_values = orig_dense[orig_dense < 0.5]
new_dense = np.reshape(orig_dense, new_shape)
new_indices = np.transpose(np.nonzero(new_dense < 0.5))
new_values = new_dense[new_dense < 0.5]
sp_input = self._SparseTensorPlaceholder()
input_val = tf.SparseTensorValue(orig_indices, orig_values, orig_shape)
sp_output = tf.sparse_reshape(sp_input, new_shape)
output_val = sess.run(sp_output, {sp_input: input_val})
self.assertAllEqual(output_val.indices, new_indices)
self.assertAllEqual(output_val.values, new_values)
self.assertAllEqual(output_val.shape, new_shape)