def test_cross_entropy(self, constructor): # cross_entropy in pytorch combines softmax and the cross-entropy function sl_function = lambda x, labels: sl_F.cross_entropy( sl_F.softmax(x), labels.detach()) torch_function = lambda x, labels: torch_F.cross_entropy( x, labels.long()) evaluate_function_with_pytorch(sl_function, torch_function, constructor)
def test_exp(self, constructor): evaluate_function_with_pytorch(lambda x: x.exp(), lambda x: x.exp(), constructor)
def test_sum_self_keepdims(self, constructor): evaluate_function_with_pytorch(lambda x: x.sum(axis=0, keepdims=False), lambda x: x.sum(axis=0, keepdims=False), constructor)
def test_transpose(self, constructor): evaluate_function_with_pytorch(lambda x: x.T, lambda x: x.T, constructor)
def test_reshape(self, constructor): evaluate_function_with_pytorch(lambda x: x.reshape( (-1, 1)), lambda x: x.reshape((-1, 1)), constructor)
def test_mean(self, constructor): evaluate_function_with_pytorch(lambda x: x.mean(), lambda x: x.mean(), constructor)
def test_matmul(self, constructor): constructor[1][-1] = constructor[1][ -1][::-1] # change the size of last argument to match matmul evaluate_function_with_pytorch(operator.matmul, operator.matmul, constructor)
def test_pow(self, constructor): constructor[ 0] = np.random.rand # overriding function to keep the tests real valued evaluate_function_with_pytorch(operator.pow, operator.pow, constructor)
def test_div(self, constructor): evaluate_function_with_pytorch(operator.truediv, operator.truediv, constructor)
def test_mul(self, constructor): evaluate_function_with_pytorch(operator.mul, operator.mul, constructor)
def test_sub(self, constructor): evaluate_function_with_pytorch(operator.sub, operator.sub, constructor)
def test_sum(self, constructor): evaluate_function_with_pytorch(operator.add, operator.add, constructor)
def test_log(self, constructor): constructor[ 0] = np.random.rand # overriding function to keep the tests real valued evaluate_function_with_pytorch(lambda x: x.log(), lambda x: x.log(), constructor)
def test_softmax(self, constructor): # adding extra pow function to make the softmax derivatives bigger sl_function = lambda x: (sl_F.softmax(x)**4.2).sum() torch_function = lambda x: (torch_F.softmax(x, dim=-1)**4.2).sum() evaluate_function_with_pytorch(sl_function, torch_function, constructor)
def test_relu(self, constructor): evaluate_function_with_pytorch(sl_F.relu, torch_F.relu, constructor)