8000 dynet-1320. Add / support for cdiv. by xunzhang · Pull Request #1321 · clab/dynet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

dynet-1320. Add / support for cdiv. #1321

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

Merged
merged 2 commits into from
Mar 28, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dynet/expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Expression operator-(real x, const Expression& y) { return Expression(y.pg, y.pg
Expression operator-(const Expression& x, real y) { return -(y - x); }
Expression operator*(const Expression& x, const Expression& y) { return Expression(x.pg, x.pg->add_function<MatrixMultiply>({x.i, y.i})); }
Expression operator*(const Expression& x, float y) { return Expression(x.pg, x.pg->add_function<ConstScalarMultiply>({x.i}, y)); }
Expression operator/(const Expression& x, const Expression& y) { return Expression(x.pg, x.pg->add_function<CwiseQuotient>({x.i, y.i})); }
Expression cmult(const Expression& x, const Expression& y) { return Expression(x.pg, x.pg->add_function<CwiseMultiply>({x.i, y.i})); }
Expression cdiv(const Expression& x, const Expression& y) { return Expression(x.pg, x.pg->add_function<CwiseQuotient>({x.i, y.i})); }
Expression colwise_add(const Expression& x, const Expression& bias) { return Expression(x.pg, x.pg->add_function<AddVectorToAllColumns>({x.i, bias.i})); }
Expand Down
12 changes: 12 additions & 0 deletions dynet/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,18 @@ inline Expression operator*(float y, const Expression& x) { return x * y; }
*/
inline Expression operator/(const Expression& x, float y) { return x * (1.f / y); }

/**
* \ingroup arithmeticoperations
* \brief Matrix division
* \details Divide an expression by another.
*
* \param x The left-hand matrix
* \param y The right-hand matrix
*
* \return An expression where the ith element is x_i divided by y_i
*/
Expression operator/(const Expression& x, const Expression& y);

/**
* \ingroup arithmeticoperations
* \brief Affine transform
Expand Down
1 change: 1 addition & 0 deletions python/_dynet.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ cdef extern from "dynet/expr.h" namespace "dynet":
CExpression c_op_add "dynet::operator+" (CExpression& x, CExpression& y) except + #
CExpression c_op_scalar_add "dynet::operator+" (CExpression& x, float y) except + #
CExpression c_op_mul "dynet::operator*" (CExpression& x, CExpression& y) except + #
CExpression c_op_div "dynet::operator/" (CExpression& x, CExpression& y) except + #
CExpression c_op_scalar_mul "dynet::operator*" (CExpression& x, float y) except + #
CExpression c_op_scalar_div "dynet::operator/" (CExpression& x, float y) except + #
CExpression c_op_scalar_sub "dynet::operator-" (float y, CExpression& x) except + #
Expand Down
7 changes: 6 additions & 1 deletion python/_dynet.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,11 @@ cdef class Expression: #{{{
return _cmul(self, other)
else: raise NotImplementedError()
def __div__(self, other):
if isinstance(self, Expression) and isinstance(other, (int,float)):
if isinstance(self, Expression) and isinstance(other, Expression):
return _div(self, other)
elif isinstance(self, (int,float)):
return _cdiv(self, other)
elif isinstance(other, (int,float)):
return _cdiv(self, other)
else: raise NotImplementedError()
def __truediv__(self, other):
Expand Down Expand Up @@ -2089,6 +2093,7 @@ cdef _add(Expression a, Expression b): ensure_freshness(b); return Expression.fr
cdef _mul(Expression a, Expression b): ensure_freshness(b); return Expression.from_cexpr(a.cg_version, c_op_mul(a.c(), b.c()))
cdef _neg(Expression a): return Expression.from_cexpr(a.cg_version, c_op_neg(a.c()))
cdef _scalarsub(float a, Expression b): ensure_freshness(b); return Expression.from_cexpr(b.cg_version, c_op_scalar_sub(a, b.c()))
cdef _div(Expression a, Expression b): return Expression.from_cexpr(a.cg_version, c_op_div(a.c(), b.c()))
cdef _cadd(Expression a, float b): return Expression.from_cexpr(a.cg_version, c_op_scalar_add(a.c(), b))
cdef _cmul(Expression a, float b): return Expression.from_cexpr(a.cg_version, c_op_scalar_mul(a.c(), b))
cdef _cdiv(Expression a, float b): return Expression.from_cexpr(a.cg_version, c_op_scalar_div(a.c(), b))
Expand Down
0