def check_gradients_vector(forward_fcn, param_dictionary, rtol=1e-5, atol=1e-8, eps=1e-3): params = [ sail.Tensor(param_dictionary[a], requires_grad=True) for a in param_dictionary ] output = forward_fcn(*params) output.backward() grads_dic = {} for i, p in enumerate(param_dictionary): grads_dic[p] = params[i].grad.numpy() parameters = dictionary_to_vector(param_dictionary) grads = dictionary_to_vector(grads_dic) num_params = len(parameters) j_plus = np.zeros(num_params) j_minus = np.zeros(num_params) grad_approx = np.zeros(num_params) for i in range(len(parameters)): # pass params_plus = np.copy(parameters) params_plus[i] += eps params_plus_dict = vector_to_dictionary(params_plus, param_dictionary) params_plus_ = [ sail.Tensor(params_plus_dict[a]) for a in params_plus_dict ] z = forward_fcn(*params_plus_) j_plus[i] = z.numpy() params_minus = np.copy(parameters) params_minus[i] -= eps params_minus_dict = vector_to_dictionary(params_minus, param_dictionary) params_minus_ = [ sail.Tensor(params_minus_dict[a]) for a in params_minus_dict ] z = forward_fcn(*params_minus_) j_minus[i] = z.numpy() grad_approx[i] = (j_plus[i] - j_minus[i]) / (2 * eps) return np.allclose(np.array(grad_approx), np.array(grads), rtol=rtol, atol=atol)
def test_base(self): choices = [(12, 13, 3), (2, 5), (18, 15), (1, 3, 2, 3), (2,)] times = [] for c in choices: for i in range(-len(c)-1, len(c)): c = list(c) arr1 = np.random.uniform(0, 1, (c)) x1 = sail.Tensor(arr1, requires_grad=False) arr3 = np.expand_dims(arr1, i) t = time.time() x3 = sail.expand_dims(x1, i) times.append(time.time() - t) self.assert_eq_np_sail(arr3, x3) self.assert_eq(arr3.shape, x3.shape) c_ = list(c) a = i if (i < 0): i += len(c) + 1 if (i == len(c)): c_.append(1) else: c_.insert(i, 1) self.assert_eq(list(x3.shape), c_) return
def test_base(self, rq): choices = elementwise_options times = [] for c in choices: arr1 = np.random.uniform(0, 1, (c)).astype(np.float32) arr2 = np.random.uniform(0, 1, (c)).astype(np.float32) x1 = sail.Tensor(arr1, requires_grad=rq) x2 = sail.Tensor(arr2, requires_grad=rq) t = time.time() x3 = sail.subtract(x1, x2) times.append(time.time() - t) arr3 = arr1 - arr2 self.assert_eq_np_sail(arr3, x3) self.assert_eq(x3.requires_grad, rq) return
def test_base(self, rq): choices = elementwise_options times = [] for c in choices: arr1 = np.random.uniform(1, 2, (c)) arr2 = np.random.uniform(1, 2, (c)) x1 = sail.Tensor(arr1, requires_grad=rq) x2 = sail.Tensor(arr2, requires_grad=rq) t = time.time() x3 = sail.power(x1, x2) times.append(time.time() - t) arr3 = np.power(arr1, arr2) self.assert_eq_np_sail(arr3, x3) self.assert_eq(x3.requires_grad, rq) return
def test(self, rq): choices = [(3, 3), (12, 18), (2, 33), (32, 64)] times = [] for c in choices: arr1 = np.random.uniform(0, 1, c).astype(np.float32) targs = np.random.uniform(0, 1, c).astype(np.float32) x1 = sail.Tensor(arr1, requires_grad=rq) x2 = sail.Tensor(targs, requires_grad=rq) lin = sail.losses.MeanSquaredError() loss_sail = lin(x1, x2) loss_numpy = np.mean((arr1 - targs) ** 2) self.assert_eq_np(loss_sail.numpy(), loss_numpy, eps=1e-5) return
def test_broadcast(self, rq): choices = broadcasted_options times = [] for c in choices: c = list(c) for i in range(len(c)): arr1 = np.random.uniform(0, 1, (c)) c[i] = 1 arr2 = np.random.uniform(0, 1, (c)) x1 = sail.Tensor(arr1, requires_grad=rq) x2 = sail.Tensor(arr2, requires_grad=rq) t = time.time() x3 = sail.subtract(x1, x2) times.append(time.time() - t) arr3 = arr1 - arr2 self.assert_eq_np_sail(arr3, x3) self.assert_eq(x3.requires_grad, rq) return
def test_base(self, rq): choices = unary_elementwise_options for c in choices: arr1 = np.random.normal(5, 2, (c)) x1 = sail.Tensor(arr1, requires_grad=rq) arr2 = np.clip(arr1, 4, 6) x2 = sail.clip(x1, 4, 6) self.assert_eq_np_sail(arr2, x2) self.assert_eq(x2.requires_grad, rq) return
def test_integer(self): choices = unary_elementwise_options times = [] for c in choices: arr1 = np.random.uniform(0, 1, (c)).astype(np.int32) x1 = sail.Tensor(arr1) x3 = sail.exp(x1) arr3 = np.exp(arr1) self.assert_eq_np_sail(arr3, x3, eps=1e-6) return
def test_integer(self): choices = unary_elementwise_options times = [] for c in choices: arr1 = np.random.uniform(1, 50000, (c)).astype(np.int16) x1 = sail.Tensor(arr1) x3 = sail.log(x1) arr3 = np.log(arr1) self.assert_eq_np_sail(arr3.astype(x3.numpy().dtype), x3, eps=1e-1) return
def test_base(self, rq): choices = [(3, 3), (12, 18), (2, 33), (32, 64)] choices_2 = [[(3, 3), (3, 1), (3, 10)], [(18, 12), (18, 2)], [(33, 1), (33, 33)], [(64, 12)]] times = [] for ca, cbs in zip(choices, choices_2): for cb in cbs: verif_shape = (ca[0], cb[1]) arr1 = np.random.uniform(0, 1, (ca)) arr2 = np.random.uniform(0, 1, (cb)) x1 = sail.Tensor(arr1, requires_grad=rq) x2 = sail.Tensor(arr2, requires_grad=rq) x3 = sail.matmul(x1, x2) arr3 = np.matmul(arr1, arr2) self.assert_eq(x3.shape, verif_shape) self.assert_eq_np_sail(arr3, x3, eps=1e-7) self.assert_eq(x3.requires_grad, rq) return
def test(self): choices = [(3, 3), (12, 18), (2, 33), (32, 64)] times = [] for c in choices: arr1 = np.random.uniform(0, 1, c).astype(np.float32) targs = np.random.randint(0, c[1], c[0]).astype(np.int64) x1 = sail.Tensor(arr1, requires_grad=False) x2 = sail.Tensor(targs, requires_grad=False) lin = sail.losses.SoftmaxCrossEntropy() loss_sail = lin(x1, x2) t_loss_fcn = torch.nn.CrossEntropyLoss() t1 = torch.from_numpy(arr1) t2 = torch.from_numpy(targs) loss_torch = t_loss_fcn(t1, t2) self.assert_eq_np(loss_sail.numpy(), loss_torch.detach().numpy(), eps=1e-5) return
def test_base_int(self, rq): choices = elementwise_options times = [] for c in choices: arr1 = np.random.uniform(0, 1, (c)) #.astype(np.float32) x1 = sail.Tensor(arr1, requires_grad=rq) x3 = 1 - x1 - 2 arr3 = 1 - arr1 - 2 self.assert_eq_np_sail(arr3, x3, eps=1e-7) self.assert_eq(x3.requires_grad, rq) return
def test_min(self, rq): times = [] for c in choices: arr1 = np.random.uniform(0, 1, (c["shape"])) x1 = sail.Tensor(arr1, requires_grad=rq) x3 = sail.min(x1, c["axis"], keepdims=c["keepdims"]) arr3 = np.min(arr1, c["axis"], keepdims=c["keepdims"]) self.assert_eq(x3.shape, c["result_shape"]) self.assert_eq_np_sail(arr3, x3, 1e-7) self.assert_eq(x3.requires_grad, rq) return
def test_base_float(self, rq): choices = elementwise_options times = [] for c in choices: arr1 = np.random.uniform(0, 1, (c)) #.astype(np.float32) x1 = sail.Tensor(arr1, requires_grad=rq) x3 = 1.2 / x1 / 1.3 arr3 = 1.2 / arr1 / 1.3 self.assert_eq_np_sail(arr3, x3) self.assert_eq(x3.requires_grad, rq) return
def test_base(self, rq): choices = unary_elementwise_options times = [] for c in choices: arr1 = np.random.uniform(1, 2, (c)) x1 = sail.Tensor(arr1, requires_grad=rq) t = time.time() x3 = sail.log(x1) times.append(time.time() - t) arr3 = np.log(arr1) self.assert_eq_np_sail(arr3, x3, eps=1e-6) self.assert_eq(x3.requires_grad, rq) return
def test(self, rq): choices = [(3, 3), (12, 18), (2, 33), (32, 64)] times = [] for c in choices: arr1 = np.random.uniform(-1, 1, c).astype(np.float32) x1 = sail.Tensor(arr1, requires_grad=rq) lin = sail.modules.ReLU() y = lin(x1) y2 = np.clip(arr1, 0, 100) self.assert_eq_np_sail(y2, y, eps=1e-8) self.assert_eq(y.requires_grad, rq) return
def test(self, rq): choices = [(3, 3), (12, 18), (2, 33), (32, 64)] times = [] for c in choices: arr1 = np.random.uniform(0, 1, c).astype(np.float32) x1 = sail.Tensor(arr1, requires_grad=rq) lin = sail.modules.Softmax() y = lin(x1) y2 = np.exp(arr1) / np.sum(np.exp(arr1), 1, keepdims=True) self.assert_eq_np_sail(y2, y, eps=5e-6) self.assert_eq(y.requires_grad, rq) return
def test_bias(self): choices = [(3, 3), (12, 18), (2, 33), (32, 64)] choices_2 = [[(3, 3), (3, 1), (3, 10)], [(18, 12), (18, 2)], [(33, 1), (33, 33)], [(64, 12)]] times = [] for ca, cbs in zip(choices, choices_2): for cb in cbs: verif_shape = (ca[0], cb[1]) arr1 = np.random.uniform(0, 1, (ca)).astype(np.float32) x1 = sail.Tensor(arr1, requires_grad=False) lin = sail.modules.Linear(cb[0], cb[1], use_bias=True) y = lin(x1) y2 = np.matmul(arr1, lin.weights.numpy()) + lin.biases.numpy() self.assert_eq_np_sail(y2, y, eps=5e-6) self.assert_eq(y.requires_grad, True) return
def test_base_reshape(self): choices = [(12, 13, 3), (2, 5), (18, 15), (1, 3, 2, 3), (2,)] times = [] for c in choices: for i in range(len(c)): c_ = list(c) random.shuffle(c_) arr1 = np.random.uniform(0, 1, (c_)) x1 = sail.Tensor(arr1, requires_grad=False) t = time.time() x3 = sail.reshape(x1, c) times.append(time.time() - t) arr3 = np.reshape(arr1, c) self.assert_eq_np_sail(arr3, x3) self.assert_eq(x3.shape, c) return
def test_catch_error(self): choices = [(12, 13, 3), (2, 5), (18, 15), (1, 3, 2, 3), (2,)] times = [] for c in choices: arr1 = np.random.uniform(0, 1, (c)) x1 = sail.Tensor(arr1, requires_grad=False) try: x3 = sail.expand_dims(x1, 10) except sail.DimensionError as e: try: x3 = sail.expand_dims(x1, -10) except sail.DimensionError as e: self.assert_eq(True, True) return self.assert_eq(False, True) return
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_catch_error(self): choices = [(12, 13, 3), (2, 5), (18, 15), (1, 3, 2, 3), (2,)] times = [] for c in choices: c_ = list(c) random.shuffle(c_) c_[-1] = c_[-1] * 2 arr1 = np.random.uniform(0, 1, (c)) x1 = sail.Tensor(arr1, requires_grad=False) try: x3 = sail.reshape(x1, c_) except sail.DimensionError as e: self.assert_eq(True, True) return self.assert_eq(False, True) 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
def test_base(self): choices = [(12, 13, 3), (2, 5), (18, 15), (1, 3, 2, 3)] times = [] for c in choices: for i in range(len(c)): cr = list(range(len(c))) random.shuffle(cr) fixed_shape = [c[r] for r in cr] arr1 = np.random.uniform(0, 1, (c)) x1 = sail.Tensor(arr1, requires_grad=False) x3 = sail.transpose(x1, cr) arr3 = np.transpose(arr1, cr) self.assert_eq_np_sail(arr3, x3) self.assert_eq(list(x3.shape), fixed_shape) return
def test_base(self): choices = [(12, 13, 3), (2, 5), (18, 15), (1, 3, 2, 3), (2,)] arrs = [] for c in choices: temp = [] for i in range(-len(c)-1, len(c)): x = np.expand_dims(np.random.uniform(0, 1, (c)), i) temp.append(x) arrs.append(temp) times = [] for a, c in zip(arrs, choices): z = 0 for i in range(-a[0].ndim, a[0].ndim-1): arr1 = a[z] x1 = sail.Tensor(arr1, requires_grad=False) arr3 = np.squeeze(arr1, i) x3 = sail.squeeze(x1, i) self.assert_eq_np_sail(arr3, x3) self.assert_eq(arr3.shape, x3.shape) z += 1 return
def test_negative_print(self): x = np.ones((32, 32)) x *= -0.001 x[16:] *= 1.3 comparison = ''' tensor([[-0.00100000 -0.00100000 -0.00100000 ... -0.00100000 -0.00100000 -0.00100000] [-0.00100000 -0.00100000 -0.00100000 ... -0.00100000 -0.00100000 -0.00100000] [-0.00100000 -0.00100000 -0.00100000 ... -0.00100000 -0.00100000 -0.00100000] ... [-0.0013 -0.0013 -0.0013 ... -0.0013 -0.0013 -0.0013 ] [-0.0013 -0.0013 -0.0013 ... -0.0013 -0.0013 -0.0013 ] [-0.0013 -0.0013 -0.0013 ... -0.0013 -0.0013 -0.0013 ]], 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 *= -0.00000002 x[16:] *= 1.3 comparison = ''' tensor([-0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000002 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003 -0.00000003], shape=(32))''' comparison = comparison[1:] x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.ones((32)).astype(np.float64) x *= -100000000000 x[16:] *= 1.3 comparison = ''' tensor([-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11 -1.30000000e+11], shape=(32))''' comparison = comparison[1:] x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.ones((32, 12, 4)).astype(np.float64) x *= -100000000000 x[16:] *= 0.00000002 comparison = ''' tensor([[[-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] ... [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11]] [[-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] ... [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11]] [[-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] ... [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11] [-1.00000000e+11 -1.00000000e+11 -1.00000000e+11 -1.00000000e+11]] ... [[-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] ... [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03]] [[-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] ... [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03]] [[-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] ... [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03] [-2.00000000e+03 -2.00000000e+03 -2.00000000e+03 -2.00000000e+03]]], shape=(32, 12, 4))''' comparison = comparison[1:] x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", ""))
def test_base(self): choices = [ { 'a_shape': (4, 3, 2), 'b_shape': (3, 2, 5), 'axes': 2, 'gc_shape': (4, 5) }, # NOQA { 'a_shape': (4, 3, 2), 'b_shape': (3, 2, 5), 'axes': ([1, 2], [0, 1]), 'gc_shape': (4, 5) }, # NOQA { 'a_shape': (4, 2, 3), 'b_shape': (3, 5, 2), 'axes': ([2, 1], [0, 2]), 'gc_shape': (4, 5) }, # NOQA { 'a_shape': (2, 4, 3), 'b_shape': (5, 3, 2), 'axes': ([2, 0], [1, 2]), 'gc_shape': (4, 5) }, # NOQA { 'a_shape': (2, 3, 4), 'b_shape': (5, 2, 3), 'axes': ([1, 0], [2, 1]), 'gc_shape': (4, 5) }, # NOQA { 'a_shape': (3, 2, 4), 'b_shape': (2, 5, 3), 'axes': ([0, 1], [2, 0]), 'gc_shape': (4, 5) }, # NOQA { 'a_shape': (3, 4, 2), 'b_shape': (2, 3, 5), 'axes': ([0, 2], [1, 0]), 'gc_shape': (4, 5) }, # NOQA { 'a_shape': (3, 4, 2), 'b_shape': (2, 5, 6), 'axes': 1, 'gc_shape': (3, 4, 5, 6) }, # NOQA { 'a_shape': (3, 4, 2), 'b_shape': (2, 5, 6), 'axes': ([2], [0]), 'gc_shape': (3, 4, 5, 6) }, # NOQA { 'a_shape': (3, 2, 4), 'b_shape': (5, 2, 6), 'axes': ([1], [1]), 'gc_shape': (3, 4, 5, 6) }, # NOQA { 'a_shape': (2, 3, 4), 'b_shape': (5, 6, 2), 'axes': ([0], [2]), 'gc_shape': (3, 4, 5, 6) }, # NOQA { 'a_shape': (4, 5, 3, 2), 'b_shape': (3, 2, 6), 'axes': 2, 'gc_shape': (4, 5, 6) }, # NOQA { 'a_shape': (4, 5, 3, 2), 'b_shape': (3, 2, 6), 'axes': ([2, 3], [0, 1]), 'gc_shape': (4, 5, 6) }, # NOQA { 'a_shape': (4, 5, 2, 3), 'b_shape': (3, 6, 2), 'axes': ([3, 2], [0, 2]), 'gc_shape': (4, 5, 6) }, # NOQA { 'a_shape': (4, 2, 5, 3), 'b_shape': (6, 3, 2), 'axes': ([3, 1], [1, 2]), 'gc_shape': (4, 5, 6) }, # NOQA { 'a_shape': (2, 4, 5, 3), 'b_shape': (6, 2, 3), 'axes': ([3, 0], [2, 1]), 'gc_shape': (4, 5, 6) }, # NOQA { 'a_shape': (2, 4, 3, 5), 'b_shape': (2, 6, 3), 'axes': ([2, 0], [2, 0]), 'gc_shape': (4, 5, 6) }, # NOQA { 'a_shape': (2, 3, 4, 5), 'b_shape': (2, 3, 6), 'axes': ([1, 0], [1, 0]), 'gc_shape': (4, 5, 6) }, # NOQA { 'a_shape': (3, 2, 4, 5), 'b_shape': (3, 2, 6), 'axes': ([0, 1], [0, 1]), 'gc_shape': (4, 5, 6) }, # NOQA { 'a_shape': (3, 2, 5, 4), 'b_shape': (3, 6, 2), 'axes': ([0, 1], [0, 2]), 'gc_shape': (5, 4, 6) }, # NOQA { 'a_shape': (3, 5, 2, 4), 'b_shape': (6, 3, 2), 'axes': ([0, 2], [1, 2]), 'gc_shape': (5, 4, 6) }, # NOQA { 'a_shape': (5, 3, 2, 4), 'b_shape': (6, 2, 3), 'axes': ([1, 2], [2, 1]), 'gc_shape': (5, 4, 6) }, # NOQA { 'a_shape': (5, 4, 3, 2), 'b_shape': (4, 3, 2, 6), 'axes': 3, 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (5, 4, 3, 2), 'b_shape': (4, 3, 2, 6), 'axes': ([1, 2, 3], [0, 1, 2]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (5, 4, 2, 3), 'b_shape': (4, 3, 6, 2), 'axes': ([1, 3, 2], [0, 1, 3]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (5, 2, 4, 3), 'b_shape': (4, 6, 3, 2), 'axes': ([2, 3, 1], [0, 2, 3]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (2, 5, 4, 3), 'b_shape': (4, 6, 2, 3), 'axes': ([2, 3, 0], [0, 3, 2]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (2, 5, 3, 4), 'b_shape': (6, 4, 2, 3), 'axes': ([3, 2, 0], [1, 3, 2]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (2, 3, 5, 4), 'b_shape': (6, 2, 4, 3), 'axes': ([3, 1, 0], [2, 3, 1]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (3, 2, 5, 4), 'b_shape': (6, 2, 3, 4), 'axes': ([3, 0, 1], [3, 2, 1]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (3, 2, 4, 5), 'b_shape': (2, 6, 3, 4), 'axes': ([2, 0, 1], [3, 2, 0]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (3, 4, 2, 5), 'b_shape': (2, 3, 6, 4), 'axes': ([1, 0, 2], [3, 1, 0]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (4, 3, 2, 5), 'b_shape': (2, 3, 4, 6), 'axes': ([0, 1, 2], [2, 1, 0]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (4, 3, 5, 2), 'b_shape': (3, 2, 4, 6), 'axes': ([0, 1, 3], [2, 0, 1]), 'gc_shape': (5, 6) }, # NOQA { 'a_shape': (4, 5, 3, 2), 'b_shape': (3, 4, 2, 6), 'axes': ([0, 2, 3], [1, 0, 2]), 'gc_shape': (5, 6) }, # NOQA ] for c in choices: arr1 = np.random.uniform(0, 1, c["a_shape"]) arr2 = np.random.uniform(0, 1, c["b_shape"]) x1 = sail.Tensor(arr1, requires_grad=False) x2 = sail.Tensor(arr2, requires_grad=False) arr3 = np.tensordot(arr1, arr2, axes=c["axes"]) x3 = sail.tensordot(x1, x2, axes=c["axes"]) self.assert_eq_np_sail(arr3, x3, eps=1e-7)
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(" ", ""))
end = batch_size step = batch_size total_loss = 0 steps = 0 ar = np.arange(len(x_train)) np.random.shuffle(ar) x_train = x_train[ar] y_train = y_train[ar] u_time = 0 while end < len(x_train): x_batch = x_train[start:end] y_batch = y_train[start:end] x_batch = sail.Tensor(x_batch) y_batch = sail.Tensor(y_batch) preds = mnist(x_batch) loss = loss_fcn(preds, y_batch) total_loss += loss.numpy()[0] loss.backward() opt.update() start += batch_size end += batch_size steps += 1 pred_test = mnist(sail.Tensor(x_test))
def test_print(self): x = np.ones((32, 32)) x *= 0.001 x[16:] *= 1.3 comparison = ''' tensor([[0.00100000 0.00100000 0.00100000 ... 0.00100000 0.00100000 0.00100000] [0.00100000 0.00100000 0.00100000 ... 0.00100000 0.00100000 0.00100000] [0.00100000 0.00100000 0.00100000 ... 0.00100000 0.00100000 0.00100000] ... [0.0013 0.0013 0.0013 ... 0.0013 0.0013 0.0013 ] [0.0013 0.0013 0.0013 ... 0.0013 0.0013 0.0013 ] [0.0013 0.0013 0.0013 ... 0.0013 0.0013 0.0013 ]], 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 *= 0.00000002 x[16:] *= 1.3 comparison = ''' tensor([0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000002 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003 0.00000003], shape=(32))''' comparison = comparison[1:] x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.ones((32)).astype(np.float64) x *= 100000000000 x[16:] *= 1.3 comparison = ''' tensor([1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11 1.30000000e+11], shape=(32))''' comparison = comparison[1:] x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", "")) x = np.ones((32, 12, 4)).astype(np.float64) x *= 100000000000 x[16:] *= 0.00000002 comparison = ''' tensor([[[1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] ... [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11]] [[1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] ... [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11]] [[1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] ... [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11] [1.00000000e+11 1.00000000e+11 1.00000000e+11 1.00000000e+11]] ... [[2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] ... [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03]] [[2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] ... [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03]] [[2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] ... [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03] [2.00000000e+03 2.00000000e+03 2.00000000e+03 2.00000000e+03]]], shape=(32, 12, 4))''' comparison = comparison[1:] x = sail.Tensor(x) self.assert_eq(x.__repr__().replace(" ", ""), comparison.replace(" ", ""))