def test_base(self): a = sail.random.uniform(10, 11, (4, 1)) a_np = a.numpy() b = sail.broadcast_to(a, (4, 4)) for i in range(4): for j in range(4): self.assert_eq_np_sail(a_np[i][0], b[i][j]) a = sail.random.uniform(10, 11, (4)) a_np = a.numpy() b = sail.broadcast_to(a, (4, 4)) for i in range(4): self.assert_eq_np_sail(a_np, b[i]) a = sail.random.uniform(10, 11, (10, 12, 4, 1, 45)) b = sail.broadcast_to(a, (10, 12, 4, 10, 45)) a_np = a.numpy() b_np = np.broadcast_to(a_np, (10, 12, 4, 10, 45)) self.assert_eq_np_sail(a_np, a) self.assert_eq_np_sail(b_np, b)
def test_broadcast(self): choices = elementwise_options times = [] for c in choices: for i in range(0, len(c)): b = list(c) c_ = list(c) c_[i - 1] = 1 x1 = sail.random.uniform(10, 20, c_) x1 = sail.broadcast_to(x1, c) x2 = sail.random.uniform(10, 20, c) x3 = sail.stack([x1, x2], axis=i) arr3 = np.stack([x1.numpy(), x2.numpy()], axis=i) self.assert_eq_np_sail(arr3, x3)
def test_broadcast(self): choices = elementwise_options times = [] for c in choices: for i in range(0, len(c)): b = list(c) b[i] = random.randint(min(b), max(c) + 10) c_ = list(c) c_[i - 1] = 1 x1 = sail.random.uniform(10, 20, c_) x1 = sail.broadcast_to(x1, c) x2 = sail.random.uniform(10, 20, b) x3 = sail.cat([x1, x2], axis=i) arr3 = np.concatenate([x1.numpy(), x2.numpy()], axis=i) self.assert_eq_np_sail(arr3, x3)
def test_broadcast_float(self, rq): choices = broadcasted_options times = [] for c in choices: for i in range(len(c)): b = list(c) b[i] = 1 arr1 = np.random.uniform(0, 1, (b)) #.astype(np.float32) x1 = sail.Tensor(arr1, requires_grad=rq) x1 = sail.broadcast_to(x1, c) arr1 = np.broadcast_to(arr1, c) x3 = 1.1 - x1 - 2.2 arr3 = 1.1 - arr1 - 2.2 self.assert_eq_np_sail(arr3, x3) self.assert_eq(x3.requires_grad, rq) return
def test_broadcast(self): choices = unary_broadcasted_options times = [] for c in choices: for i in range(len(c)): b = list(c) b[i] = 1 arr1 = np.random.uniform(1, 2, (b)) x1 = sail.Tensor(arr1, requires_grad=False) x2 = sail.broadcast_to(x1, c) arr2 = np.broadcast_to(arr1, c) t = time.time() x3 = -x2 times.append(time.time() - t) arr3 = -arr2 self.assert_eq_np_sail(arr3, x3, eps=1e-6) return
Please reference :ref:`view-or-copy` to understand the view and copy semantics of mutation ops. Args: tensor (Tensor): Input tensor axis ((tuple of ints)): New order of axes Examples: >>> x = sail.random.uniform(2, 4, (2, 3, 4)) >>> y = sail.transpose(x, (2, 0, 1)) >>> y.shape (4, 2, 3) """ add_docstring(sail.transpose, descr) descr = r""" sail.broadcast_to(tensor, shape) -> Tensor Returns a broadcasted view of `tensor` .. note:: In order for broadcasting to work, `tensor`'s shape must be compatible with `shape`. Compatability means that, starting from the last shape to the first, the size of the shapes between ``tensor.shape`` and `shape` are either equal, or one of the shapes is 1 or doesn't exist. Please reference :ref:`view-or-copy` to understand the view and copy semantics of mutation ops. Args: tensor (Tensor): Input tensor shape (int or tuple of ints): Shape to broadcast `tensor` to Examples: >>> x = sail.random.uniform(0, 1, (1, 5)) >>> y = sail.broadcast_to(x, (5, 5))
def test_general(self): x = np.ones((32, 32)).astype(np.int32) x *= 10 x[16:] *= 20 comparison = ''' tensor([[ 10 10 10 ... 10 10 10] [ 10 10 10 ... 10 10 10] [ 10 10 10 ... 10 10 10] ... [200 200 200 ... 200 200 200] [200 200 200 ... 200 200 200] [200 200 200 ... 200 200 200]], shape=(32, 32))''' comparison = comparison[1:] comparison = comparison x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.ones((32)).astype(np.float64) x *= -25.6 x[16:] *= 13.8 comparison = ''' tensor([ -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -25.6 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28 -353.28], shape=(32))''' comparison = comparison[1:] x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.ones((32)).astype(np.int32) x *= -1000000000 comparison = ''' tensor([-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000], shape=(32))''' comparison = comparison[1:] x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.ones(1) comparison = ''' tensor([[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]], shape=(10, 10))''' comparison = comparison[1:] x = sail.broadcast_to(sail.Tensor(x), (10, 10)) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.ones(10) comparison = ''' tensor([[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]], shape=(10, 10))''' comparison = comparison[1:] x = sail.broadcast_to(sail.Tensor(x), (10, 10)) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.array([1.25234312345]) comparison = ''' tensor([[1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312] [1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312 1.25234312]], shape=(10, 10))''' comparison = comparison[1:] x = sail.broadcast_to(sail.Tensor(x), (10, 10)) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.array([1.25234312345]) comparison = '''tensor([1.25234312], shape=(1))''' x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) comparison = ''' tensor([ True False True True True False False False True True False False True True False False True True False True True False False False False False True], shape=(27))''' comparison = comparison[1:] x = sail.Tensor(np.array([1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0,0,0,0,1])).astype(sail.bool_) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", ""))