Beispiel #1
0
 def test_multi_dim_tensor_from_numpy(self):
     v = math.tensor(np.ones([1, 4, 3, 2]), batch('batch'), spatial('x,y'),
                     channel('vector'))
     self.assertEqual((1, 4, 3, 2), v.shape.sizes)
     v = math.tensor(np.ones([10, 4, 3, 2]), batch('batch'), spatial('x,y'),
                     channel('vector'))
     self.assertEqual((10, 4, 3, 2), v.shape.sizes)
Beispiel #2
0
 def test_slice_by_item_name(self):
     t = math.tensor(spatial(x=4, y=3))
     math.assert_close(t.dims['x'], 4)
     math.assert_close(t.dims['y'], 3)
     math.assert_close(t.dims['y,x'], (3, 4))
     math.assert_close(t.dims[('y', 'x')], (3, 4))
     math.assert_close(t.dims[spatial('x,y')], (4, 3))
Beispiel #3
0
 def test_zeros_nonuniform(self):
     nonuniform = shape_stack(batch('stack'),
                              batch(time=1) & spatial(x=3, y=3),
                              spatial(x=3, y=4), channel())
     self.assertEqual(math.zeros(nonuniform).shape, nonuniform)
     self.assertEqual(math.ones(nonuniform).shape, nonuniform)
     self.assertEqual(math.random_normal(nonuniform).shape, nonuniform)
     self.assertEqual(math.random_uniform(nonuniform).shape, nonuniform)
Beispiel #4
0
 def test_tensor_from_tensor(self):
     ref = math.stack([math.zeros(spatial(x=5)),
                       math.zeros(spatial(x=4))], batch('stack'))
     for backend in BACKENDS:
         with backend:
             tens = math.tensor(ref, convert=False)
             self.assertEqual(math.NUMPY, math.choose_backend(tens))
             self.assertEqual(2, tens.shape.get_size('stack'))
             self.assertEqual(('stack', 'x'), tens.shape.names)
             tens = math.tensor(ref)
             self.assertEqual(backend, math.choose_backend(tens))
             self.assertEqual(backend, math.choose_backend(tens.stack[0]))
             self.assertEqual(backend, math.choose_backend(tens.stack[1]))
             tens = math.tensor(ref, batch('n1', 'n2'))
             self.assertEqual(backend, math.choose_backend(tens))
Beispiel #5
0
 def test_semi_collapsed(self):
     scalar = math.ones(spatial(x=4, y=3))
     scalar = CollapsedTensor(scalar, scalar.shape._expand(batch(batch=10)))
     self.assertEqual((10, 4, 3), scalar.shape.sizes)
     self.assertEqual(4, len(scalar.x.unstack()))
     self.assertEqual(10, len(scalar.batch.unstack()))
     self.assertEqual(0, scalar.y[0].batch[0].x[0].shape.rank)
Beispiel #6
0
 def test_collapsed(self):
     scalar = math.zeros(spatial(x=4, y=3))
     math.assert_close(scalar, 0)
     self.assertEqual((4, 3), scalar.shape.sizes)
     self.assertEqual(4, scalar.y[0].shape.size)
     self.assertEqual(0, scalar.y[0].x[0].shape.rank)
     self.assertEqual(3, len(scalar.y.unstack()))
Beispiel #7
0
 def test_flip_item_names(self):
     t = math.zeros(spatial(x=4, y=3), channel(vector='x,y'))
     self.assertEqual(('x', 'y'), t.vector.item_names)
     t_ = t.vector.flip()
     self.assertEqual(('y', 'x'), t_.vector.item_names)
     t_ = t.vector[::-1]
     self.assertEqual(('y', 'x'), t_.vector.item_names)
Beispiel #8
0
 def test_stacked_shapes(self):
     t0 = math.ones(batch(batch=10) & spatial(x=4, y=3) & channel(vector=2))
     for dim in t0.shape.names:
         tensors = t0.unstack(dim)
         stacked = math.stack(tensors, t0.shape[dim].with_sizes([None]))
         self.assertEqual(set(t0.shape.names), set(stacked.shape.names))
         self.assertEqual(t0.shape.volume, stacked.shape.volume)
Beispiel #9
0
 def test_Dict(self):
     d1 = math.Dict(a=1, b=math.ones(), c=math.ones(spatial(x=3)))
     math.assert_close(d1 * 2, d1 + d1, 2 * d1, 2 / d1)
     math.assert_close(0 + d1, d1, d1 - 0, abs(d1), round(d1))
     math.assert_close(-d1, 0 - d1)
     math.assert_close(d1 // 2, d1 * 0, d1 % 1)
     math.assert_close(d1 / 2, d1 * 0.5, 0.5 * d1)
     math.assert_close(math.sin(d1 * 0), d1 * 0)
Beispiel #10
0
 def test_stacked_get(self):
     t0 = math.ones(batch(batch=10) & spatial(x=4, y=3) & channel(vector=2))
     tensors = t0.unstack('vector')
     stacked = math.stack(tensors, channel('channel'))
     self.assertEqual(tensors, stacked.channel.unstack())
     assert tensors[0] is stacked.channel[0]
     assert tensors[1] is stacked.channel[1:2].channel.unstack()[0]
     self.assertEqual(4, len(stacked.x.unstack()))
Beispiel #11
0
 def test_native_constant_ops(self):
     v = math.tensor(np.ones([1, 4, 3, 2]), batch('batch'), spatial('x,y'),
                     channel('vector'))
     math.assert_close(v + 1, 2)
     math.assert_close(v * 3, 3)
     math.assert_close(v / 2, 0.5)
     math.assert_close(v**2, 1)
     math.assert_close(2**v, 2)
     math.assert_close(v + [0, 1], [1, 2])
Beispiel #12
0
 def test_stacked_native(self):
     t0 = math.ones(batch(batch=10) & spatial(x=4, y=3) & channel(vector=2))
     tensors = t0.unstack('vector')
     stacked = math.stack(tensors, channel('vector2'))
     math.assert_close(stacked, t0)
     self.assertEqual((10, 4, 3, 2), stacked.native(stacked.shape).shape)
     self.assertEqual(
         (4, 3, 2, 10),
         stacked.native(order=('x', 'y', 'vector2', 'batch')).shape)
     self.assertEqual(
         (2, 10, 3, 4),
         stacked.native(order=('vector2', 'batch', 'y', 'x')).shape
     )  # this should re-stack since only the stacked dimension position is different
Beispiel #13
0
 def test_serialize_tensor(self):
     t = math.random_normal(batch(batch=10), spatial(x=4, y=3),
                            channel(vector=2))
     math.assert_close(t, math.from_dict(math.to_dict(t)))
Beispiel #14
0
 def test_slice_by_int_tensor(self):
     indices = math.meshgrid(x=2, y=2)
     sel = indices.x[wrap((1, 0), spatial('x'))]
     math.assert_close((1, 0), sel.vector['x'].y[0])
Beispiel #15
0
 def test_slice_by_bool_tensor(self):
     indices = math.meshgrid(x=2, y=2)
     sel = indices.x[wrap((True, False), spatial('x'))]
     self.assertEqual(1, sel.x.size)
Beispiel #16
0
 def test_native_native_ops(self):
     v = math.ones(batch(batch=10) & spatial(x=4, y=3) & channel(vector=2))
     d = v.unstack('vector')[0]
     math.assert_close(v + d, d + v, 2)
     math.assert_close(v * d, d * v, 1)
Beispiel #17
0
 def test_collapsed_non_uniform_tensor(self):
     non_uniform = math.stack(
         [math.zeros(spatial(a=2)),
          math.ones(spatial(a=3))], batch('b'))
     e = math.expand(non_uniform, channel('vector'))
     assert e.shape.without('vector') == non_uniform.shape
Beispiel #18
0
 def test_native_unstack(self):
     v = math.ones(batch(batch=10), spatial(x=4, y=3), channel(vector=2))
     vx, vy = v.vector.unstack()
     self.assertEqual((10, 4, 3), vx.shape.sizes)
     self.assertEqual(4, len(v.x.unstack()))
     self.assertEqual(10, len(v.batch.unstack()))
Beispiel #19
0
 def test_shape_math(self):
     vector = math.ones(spatial(x=4, y=3) & channel(vector=2))
     vector *= vector.shape.spatial
     math.assert_close(vector.vector[0], 4)
     math.assert_close(vector.vector[1], 3)
Beispiel #20
0
 def test_native_slice(self):
     v = math.ones(batch(batch=10), spatial(x=4, y=3), channel(vector=2))
     self.assertEqual((10, 4, 3), v.vector[0].shape.sizes)
     self.assertEqual((10, 2, 2), v.y[0:2].x[0].shape.sizes)
Beispiel #21
0
 def test_tensor_from_shape(self):
     s = spatial(x=4, y=3)
     t = math.tensor(s)
     math.assert_close(t, [4, 3])
     self.assertEqual(t.shape.get_item_names('dims'), ('x', 'y'))
# Implicit names
zeros(x=5, y=4, vector=2)
tensor(array, 'x, y, vector')

# Explicit names
zeros(x_spatial=5, y_spatial=4, vector_channel=2)
tensor(array, 'x: spatial, y: spatial, vector: channel')

# Mixed
zeros(x=5, y=4, vector_channel=2)
tensor(array, 'x, y, vector: channel')

# Explicit types, automatic order
zeros(spatial=dict(x=5, y=4), channel=dict(vector=2))
zeros(spatial=[('x', 5), ('y', 4)], channel=('vector', 2))
zeros(spatial(x=5), channel(vector=2))
tensor(array, 'x, y, vector', [spatial, spatial, channel])
tensor(array, [('x', spatial), ('y', spatial), ('vector', channel)])

# Explicit types, manual order
zeros(x=(5, spatial), y=(4, spatial), vector=(2, channel))
tensor(array, x=spatial, y=spatial, vector=channel)

# Python types
zeros(spatial('x', 5), spatial('y', 4), channel('vector', 2))
zeros(x=spatial(5), y=spatial(4), vector=channel(2))
tensor(array, x=spatial, y=spatial, vector=channel)

# Icons
zeros(batch_ᵇ=10, particles_ᵛ=34, x_ˢ=32, y_ˢ=32, vector_ᵛ=2)
ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖʳˢᵗᵘᵛʷˣʸᶻ = 5