本文整理汇总了Python中tensorflow.python.ops.ragged.constant函数的典型用法代码示例。如果您正苦于以下问题:Python constant函数的具体用法?Python constant怎么用?Python constant使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了constant函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testArithmeticOperators
def testArithmeticOperators(self):
x = ragged.constant([[1.0, -2.0], [8.0]])
y = ragged.constant([[4.0, 4.0], [2.0]])
with self.test_session():
self.assertEqual(abs(x).eval().tolist(), [[1.0, 2.0], [8.0]])
self.assertEqual((-x).eval().tolist(), [[-1.0, 2.0], [-8.0]])
self.assertEqual((x + y).eval().tolist(), [[5.0, 2.0], [10.0]])
self.assertEqual((3.0 + y).eval().tolist(), [[7.0, 7.0], [5.0]])
self.assertEqual((x + 3.0).eval().tolist(), [[4.0, 1.0], [11.0]])
self.assertEqual((x - y).eval().tolist(), [[-3.0, -6.0], [6.0]])
self.assertEqual((3.0 - y).eval().tolist(), [[-1.0, -1.0], [1.0]])
self.assertEqual((x + 3.0).eval().tolist(), [[4.0, 1.0], [11.0]])
self.assertEqual((x * y).eval().tolist(), [[4.0, -8.0], [16.0]])
self.assertEqual((3.0 * y).eval().tolist(), [[12.0, 12.0], [6.0]])
self.assertEqual((x * 3.0).eval().tolist(), [[3.0, -6.0], [24.0]])
self.assertEqual((x / y).eval().tolist(), [[0.25, -0.5], [4.0]])
self.assertEqual((y / x).eval().tolist(), [[4.0, -2.0], [0.25]])
self.assertEqual((2.0 / y).eval().tolist(), [[0.5, 0.5], [1.0]])
self.assertEqual((x / 2.0).eval().tolist(), [[0.5, -1.0], [4.0]])
self.assertEqual((x // y).eval().tolist(), [[0.0, -1.0], [4.0]])
self.assertEqual((y // x).eval().tolist(), [[4.0, -2.0], [0.0]])
self.assertEqual((2.0 // y).eval().tolist(), [[0.0, 0.0], [1.0]])
self.assertEqual((x // 2.0).eval().tolist(), [[0.0, -1.0], [4.0]])
self.assertEqual((x % y).eval().tolist(), [[1.0, 2.0], [0.0]])
self.assertEqual((y % x).eval().tolist(), [[0.0, -0.0], [2.0]])
self.assertEqual((2.0 % y).eval().tolist(), [[2.0, 2.0], [0.0]])
self.assertEqual((x % 2.0).eval().tolist(), [[1.0, 0.0], [0.0]])
示例2: testShapeMismatch
def testShapeMismatch(self):
x = ragged.constant([[1, 2, 3], [4, 5]])
y = ragged.constant([[1, 2, 3], [4, 5, 6]])
with self.assertRaisesRegexp(errors.InvalidArgumentError,
'Incompatible shapes'):
with self.cached_session():
ragged.add(x, y).eval()
示例3: testOpWithRaggedRankThree
def testOpWithRaggedRankThree(self):
x = ragged.constant([[[3, 1, 4]], [], [[], [1, 5]]])
y = ragged.constant([[[1, 2, 3]], [], [[], [4, 5]]])
self.assertRaggedMapInnerValuesReturns(
op=math_ops.multiply,
args=(x, y),
expected=[[[3, 2, 12]], [], [[], [4, 25]]])
示例4: testOpWithKeywordArgs
def testOpWithKeywordArgs(self):
x = ragged.constant([[3, 1, 4], [], [1, 5]])
y = ragged.constant([[1, 2, 3], [], [4, 5]])
self.assertRaggedMapInnerValuesReturns(
op=math_ops.multiply,
kwargs=dict(x=x, y=y),
expected=[[3, 2, 12], [], [4, 25]])
示例5: testOpWithRaggedTensorListArg
def testOpWithRaggedTensorListArg(self):
x = ragged.constant([[1, 2, 3], [], [4, 5]])
y = ragged.constant([[10, 20, 30], [], [40, 50]])
self.assertRaggedMapInnerValuesReturns(
op=math_ops.add_n,
args=([x, y, x],),
expected=[[12, 24, 36], [], [48, 60]])
示例6: testOrderingOperators
def testOrderingOperators(self):
x = ragged.constant([[1, 5], [3]])
y = ragged.constant([[4, 5], [1]])
with self.test_session():
self.assertEqual((x > y).eval().tolist(), [[False, False], [True]])
self.assertEqual((x >= y).eval().tolist(), [[False, True], [True]])
self.assertEqual((x < y).eval().tolist(), [[True, False], [False]])
self.assertEqual((x <= y).eval().tolist(), [[True, True], [False]])
示例7: testOpWithThreeRaggedTensorArgs
def testOpWithThreeRaggedTensorArgs(self):
condition = ragged.constant(
[[True, True, False], [], [True, False]]) # pyformat: disable
x = ragged.constant([['a', 'b', 'c'], [], ['d', 'e']])
y = ragged.constant([['A', 'B', 'C'], [], ['D', 'E']])
self.assertRaggedMapInnerValuesReturns(
op=array_ops.where,
args=(condition, x, y),
expected=[[b'a', b'b', b'C'], [], [b'd', b'E']])
示例8: testRaggedParamsAndRaggedIndices
def testRaggedParamsAndRaggedIndices(self):
params = ragged.constant([['a', 'b'], ['c', 'd', 'e'], ['f'], [], ['g']])
indices = ragged.constant([[2, 1], [1, 2, 0], [3]])
with self.test_session():
self.assertEqual(
ragged.gather(params, indices).eval().tolist(),
[[[b'f'], [b'c', b'd', b'e']], # [[p[2], p[1] ],
[[b'c', b'd', b'e'], [b'f'], [b'a', b'b']], # [p[1], p[2], p[0]],
[[]]] # [p[3] ]]
) # pyformat: disable
示例9: testRaggedSegmentIds
def testRaggedSegmentIds(self):
rt = ragged.constant([
[[111, 112, 113, 114], [121],], # row 0
[], # row 1
[[], [321, 322], [331]], # row 2
[[411, 412]] # row 3
]) # pyformat: disable
segment_ids = ragged.constant([[1, 2], [], [1, 1, 2], [2]])
segmented = ragged.segment_sum(rt, segment_ids, 3)
expected = [[],
[111+321, 112+322, 113, 114],
[121+331+411, 412]] # pyformat: disable
self.assertEqual(self.evaluate(segmented).tolist(), expected)
示例10: testGradient
def testGradient(self):
# rt1.shape == rt2.shape == [2, (D2), (D3), 2].
rt1 = ragged.constant([[[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0]]]],
ragged_rank=2)
rt2 = ragged.constant([[[[9.0, 8.0], [7.0, 6.0]], [[5.0, 4.0]]]],
ragged_rank=2)
rt = ragged.map_inner_values(math_ops.add, rt1, rt2 * 2.0)
st = ragged.to_sparse(rt)
g1, g2 = gradients_impl.gradients(st.values, [rt1.inner_values,
rt2.inner_values])
print(g1, g2)
with self.test_session():
self.assertEqual(g1.eval().tolist(), [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]])
self.assertEqual(g2.eval().tolist(), [[2.0, 2.0], [2.0, 2.0], [2.0, 2.0]])
示例11: testOutOfBoundsError
def testOutOfBoundsError(self):
tensor_params = ['a', 'b', 'c']
tensor_indices = [0, 1, 2]
ragged_params = ragged.constant([['a', 'b'], ['c']])
ragged_indices = ragged.constant([[0, 3]])
with self.test_session():
self.assertRaisesRegexp(errors.InvalidArgumentError,
r'indices\[1\] = 3 is not in \[0, 3\)',
ragged.gather(tensor_params, ragged_indices).eval)
self.assertRaisesRegexp(errors.InvalidArgumentError,
r'indices\[2\] = 2 is not in \[0, 2\)',
ragged.gather(ragged_params, tensor_indices).eval)
self.assertRaisesRegexp(errors.InvalidArgumentError,
r'indices\[1\] = 3 is not in \[0, 2\)',
ragged.gather(ragged_params, ragged_indices).eval)
示例12: testDocStringExamples
def testDocStringExamples(self):
params = constant_op.constant(['a', 'b', 'c', 'd', 'e'])
indices = constant_op.constant([3, 1, 2, 1, 0])
ragged_params = ragged.constant([['a', 'b', 'c'], ['d'], [], ['e']])
ragged_indices = ragged.constant([[3, 1, 2], [1], [], [0]])
with self.test_session():
self.assertEqual(
ragged.gather(params, ragged_indices).eval().tolist(),
[[b'd', b'b', b'c'], [b'b'], [], [b'a']])
self.assertEqual(
ragged.gather(ragged_params, indices).eval().tolist(),
[[b'e'], [b'd'], [], [b'd'], [b'a', b'b', b'c']])
self.assertEqual(
ragged.gather(ragged_params, ragged_indices).eval().tolist(),
[[[b'e'], [b'd'], []], [[b'd']], [], [[b'a', b'b', b'c']]])
示例13: testUnknownRankError
def testUnknownRankError(self):
x = ragged.constant([[1, 2], [3]])
y = ragged.from_row_splits(
array_ops.placeholder_with_default([1, 2, 3], shape=None), x.row_splits)
with self.assertRaisesRegexp(
ValueError, r'Unable to broadcast: unknown rank'):
ragged.add(x, y)
示例14: testRaggedMapOnStructure_RaggedOutputs
def testRaggedMapOnStructure_RaggedOutputs(self):
batman = ragged.constant([[1, 2, 3], [4], [5, 6, 7]])
# [[10, 20, 30], [40], [50, 60, 70]]
robin = ragged.map_inner_values(mo.multiply, batman, 10)
features = {'batman': batman, 'robin': robin}
def _increment(f):
return {
'batman': ragged.add(f['batman'], 1),
'robin': ragged.add(f['robin'], 1),
}
output = ragged.map_fn(
fn=_increment,
elems=features,
infer_shape=False,
dtype={
'batman':
ragged.RaggedTensorType(dtype=dtypes.int32, ragged_rank=1),
'robin':
ragged.RaggedTensorType(dtype=dtypes.int32, ragged_rank=1)
},
)
with self.test_session():
self.assertAllEqual(output['batman'].eval().tolist(),
[[2, 3, 4], [5], [6, 7, 8]])
self.assertAllEqual(output['robin'].eval().tolist(),
[[11, 21, 31], [41], [51, 61, 71]])
示例15: test4DRaggedTensorWithTwoRaggedDimensions
def test4DRaggedTensorWithTwoRaggedDimensions(self):
rt = ragged.constant([[[[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]]],
[[[11, 12]], [], [[13, 14]]], []],
ragged_rank=2)
with self.test_session():
st = ragged.to_sparse(rt).eval()
self.assertAllEqual(
st.indices,
[
[0, 0, 0, 0], # index for value=1
[0, 0, 0, 1], # index for value=2
[0, 0, 1, 0], # index for value=3
[0, 0, 1, 1], # index for value=4
[0, 1, 0, 0], # index for value=5
[0, 1, 0, 1], # index for value=6
[0, 1, 1, 0], # index for value=7
[0, 1, 1, 1], # index for value=8
[0, 1, 2, 0], # index for value=9
[0, 1, 2, 1], # index for value=10
[1, 0, 0, 0], # index for value=11
[1, 0, 0, 1], # index for value=12
[1, 2, 0, 0], # index for value=13
[1, 2, 0, 1], # index for value=14
])
self.assertAllEqual(st.values,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
self.assertAllEqual(st.dense_shape, [3, 3, 3, 2])