Skip to content

Commit

Permalink
Merge pull request #7 from dantp-ai/solution/task-0-2
Browse files Browse the repository at this point in the history
Solution/task 0 2
  • Loading branch information
dantp-ai authored Mar 3, 2024
2 parents fa0650e + 223e1c5 commit 852fc98
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import pytest
from hypothesis import given
from hypothesis.strategies import lists
from hypothesis.strategies import lists, floats

from minitorch import MathTest
from minitorch.operators import sigmoid # noqa: F401
from minitorch.operators import (
add,
addLists,
Expand All @@ -22,6 +21,7 @@
prod,
relu,
relu_back,
sigmoid,
sum,
)

Expand Down Expand Up @@ -99,53 +99,61 @@ def test_eq(a: float) -> None:


@pytest.mark.task0_2
@given(small_floats)
@given(floats(min_value=-20, max_value=20, allow_nan=False))
def test_sigmoid(a: float) -> None:
"""Check properties of the sigmoid function, specifically
* It is always between 0.0 and 1.0.
* one minus sigmoid is the same as sigmoid of the negative
* It crosses 0 at 0.5
* It is strictly increasing.
"""
# TODO: Implement for Task 0.2.
raise NotImplementedError("Need to implement for Task 0.2")
assert sigmoid(a) >= 0.0
assert sigmoid(a) <= 1.0
assert_close(1 - sigmoid(a), sigmoid(-a))
assert sigmoid(0) == 0.5
assert sigmoid(a + 0.001) - sigmoid(a) > 0.0


@pytest.mark.task0_2
@given(small_floats, small_floats, small_floats)
def test_transitive(a: float, b: float, c: float) -> None:
"Test the transitive property of less-than (a < b and b < c implies a < c)"
# TODO: Implement for Task 0.2.
raise NotImplementedError("Need to implement for Task 0.2")
if lt(a, b) and lt(b, c):
assert lt(a, c)


@pytest.mark.task0_2
def test_symmetric() -> None:
@given(small_floats, small_floats)
def test_symmetric(a: float, b: float) -> None:
"""
Write a test that ensures that :func:`minitorch.operators.mul` is symmetric, i.e.
gives the same value regardless of the order of its input.
"""
# TODO: Implement for Task 0.2.
raise NotImplementedError("Need to implement for Task 0.2")
assert mul(a, b) == mul(b, a)


@pytest.mark.task0_2
def test_distribute() -> None:
@given(small_floats, small_floats, small_floats)
def test_distribute(x: float, y: float, z: float) -> None:
r"""
Write a test that ensures that your operators distribute, i.e.
:math:`z \times (x + y) = z \times x + z \times y`
"""
# TODO: Implement for Task 0.2.
raise NotImplementedError("Need to implement for Task 0.2")
assert_close(mul(z, add(x, y)), add(mul(z, x), mul(z, y)))


@pytest.mark.task0_2
def test_other() -> None:
@given(small_floats, small_floats, small_floats)
def test_other(a: float, b: float, c: float) -> None:
"""
Write a test that ensures some other property holds for your functions.
"""
# TODO: Implement for Task 0.2.
raise NotImplementedError("Need to implement for Task 0.2")
# Identity property multiplication
assert mul(1, a) == a
assert mul(a, 1) == a

# Associative property multiplication
assert_close(mul(a, mul(b, c)), mul(mul(a, b), c))


# ## Task 0.3 - Higher-order functions
Expand Down

0 comments on commit 852fc98

Please # to comment.