minitorch / module-1 Goto Github PK
View Code? Open in Web Editor NEWModule 1 - Autodifferentiation
Module 1 - Autodifferentiation
class LT(ScalarFunction):
"Less-than function"
@staticmethod
def forward(ctx, a, b):
return 1.0 if a < b else 0.0
@staticmethod
def backward(ctx, d_output):
return 0.0. # bug, it should be: return 0.0, 0.0
cp ../Module-0/operators.py ../Module-0/module.py minitorch/
should be replaced with:
cp ../Module-0/minitorch/operators.py ../Module-0/minitorch/module.py minitorch/
For Task 1.2, I believe the intended code for scalar.Scalar.__sub__
is return Add.apply(self, -b)
, which passes the tests. Interestingly, if return Add.apply(self, Neg.apply(b))
is used, the tests that involve subtracting constants fail. Excerpt of the failed test cases below:
===============================short test summary info==============================
FAILED tests/test_scalar.py::test_one_args[fn4] - AssertionError: Expected return typ <class 'float'> got <class 'int'>: -200
FAILED tests/test_scalar.py::test_one_args[fn5] - AssertionError: Expected return typ <class 'float'> got <class 'int'>: -200
FAILED tests/test_scalar.py::test_one_args[fn13] - AssertionError: Expected return typ <class 'float'> got <class 'int'>: -5
I've slightly modified the error message in autodiff.FunctionBase
to print the offending value in question, and they are all ints. The root cause is due to scalar.ScalarFunction.data_type
being hard-coded as float
. I assume data_type
is fixed for a reason, and there probably isn't an easy fix to allow the alternate solution to pass the tests. Just thought y'all should know.
when I push my answer, It will raise error:
๐ Setup
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Collecting flake[8](https://github.com/minitorch/minitorch-module-1-gamdwk/actions/runs/7295821833/job/19882743013#step:3:9)==5.0.4
Downloading flake8-5.0.4-py2.py3-none-any.whl (61 kB)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 61.[9](https://github.com/minitorch/minitorch-module-1-gamdwk/actions/runs/7295821833/job/19882743013#step:3:10)/61.9 KB 3.4 MB/s eta 0:00:00
Collecting pyflakes<2.6.0,>=2.5.0
Downloading pyflakes-2.5.0-py2.py3-none-any.whl (66 kB)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 66.1/66.1 KB 8.0 MB/s eta 0:00:00
Collecting pycodestyle<2.[10](https://github.com/minitorch/minitorch-module-1-gamdwk/actions/runs/7295821833/job/19882743013#step:3:11).0,>=2.9.0
Downloading pycodestyle-2.9.1-py2.py3-none-any.whl (41 kB)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 41.5/41.5 KB 11.0 MB/s eta 0:00:00
Collecting mccabe<0.8.0,>=0.7.0
Downloading mccabe-0.7.0-py2.py3-none-any.whl (7.3 kB)
Installing collected packages: pyflakes, pycodestyle, mccabe, flake8
Successfully installed flake8-5.0.4 mccabe-0.7.0 pycodestyle-2.9.1 pyflakes-2.5.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
/usr/local/lib/python3.10/dist-packages/numpy/__init__.pyi:642: error: Positional-only parameters are only supported in Python 3.8 and greater
Found 1 error in 1 file (errors prevented further checking)
I have never had such a problem on my local computer.
Why does Python 3.10 not support the 3.8 feature? How should I locate this error?
By the way, the isort for pre-commit should update to version 5.12.0, otherwise, an error will occur during submission. Please make the necessary changes.
Initialize a queue with the final variable+derivative.
While the queue is not empty, pull a variable+derivative from the queue.
If the variable is a leaf then add its final derivative (_add_deriv) and loop to (1).
If the variables is not a leaf call .chain_rule on the last function that created it with derivative as ๐๐๐ข๐ก.
Loop through all the variables+derivative produced by the chain rule.
If the variable is in the queue (check .name), add to its current derivativ
Otherwise, add to the queue.
Actually, it's not necessary to Loop through all the variables+derivative produced by the chain rule.
Even though we don't check for repetition, the chain rule and sum rule will automatically accumulate the deriv to the leaf node.
So sorry to bother you.
Thanks for your nice code and tutorial, I really benefited a lot after finish module-1. But some thing seems to be wrong with the Class LT. It does not define a backward function, but the method will be tested in task1_4. Am i correct? Or am I misunderstanding something? Thank you so much.
class LT(ScalarFunction):
"Less-than function :math:`f(x) =` 1.0 if x is less than y else 0.0"
@staticmethod
def forward(ctx, a, b):
# TODO: Implement for Task 1.2.
raise NotImplementedError('Need to implement for Task 1.2')
# TODO: Implement for Task 1.2.
raise NotImplementedError('Need to implement for Task 1.2')
# TODO: Implement for Task 1.4.
raise NotImplementedError('Need to implement for Task 1.4')
In https://github.com/minitorch/Module-1/blob/master/tests/test_autodiff.py line 53, are we sure that this assert is correct:
assert variable_with_deriv.deriv == 5
?
Shouldn't it be assert variable_with_deriv.deriv == 5 * 10
?
As it will only return a single variable with derivative (as the constants are dropped from the chain_rule) and multiplication is commutative.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.