Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rmethods to ArithmeticData #191

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions examples/mlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ def run_helper(epochs, n, batch_size, layer_sizes):
for inp, target in data:
cost, dmodel = step(model, inp, target)
costs.append(cost)
# For various reasons including a numpy quirk, when multiplying
# by a scalar the scalar needs to be the right side operand.
model = model - (dmodel * numpy.float32(0.01))
model = model - (numpy.float32(0.01) * dmodel)
print(f'Cost: {cost / n}')


Expand Down
30 changes: 30 additions & 0 deletions myia/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ class ArithmeticData:
all matching fields from the added instances.
"""

__array_priority__ = 1_000_000

@core
def __add__(self, x):
return hyper_map(add, self, x)
Expand Down Expand Up @@ -870,3 +872,31 @@ def __pos__(self):
@core
def __neg__(self):
return hyper_map(usub, self)

@core
def __radd__(self, x):
return hyper_map(add, x, self)

@core
def __rsub__(self, x):
return hyper_map(sub, x, self)

@core
def __rmul__(self, x):
return hyper_map(mul, x, self)

@core
def __rtruediv__(self, x):
return hyper_map(truediv, x, self)

@core
def __rfloordiv__(self, x):
return hyper_map(floordiv, x, self)

@core
def __rmod__(self, x):
return hyper_map(mod, x, self)

@core
def __rpow__(self, x):
return hyper_map(pow, x, self)