Skip to content

Commit

Permalink
Add NDArray stopGradient and scaleGradient (#548)
Browse files Browse the repository at this point in the history
Change-Id: I9df47b3f1a7f374bebe45492af3aa45f53aed1c4
  • Loading branch information
zachgk authored Jan 21, 2021
1 parent 2994b95 commit e71f178
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/src/main/java/ai/djl/ndarray/NDArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ default byte[] encode() {
*/
boolean hasGradient();

/**
* Returns an NDArray equal to this that stop gradient propagation through it.
*
* @return an NDArray equal to this that stops gradient propagation through it
*/
NDArray stopGradient();

/**
* Returns an NDArray equal to this that magnifies the gradient propagated to this by a
* constant.
*
* @param scale how to much to magnify the gradient propagated to this
* @return an NDArray equal to this that magnifies the gradient propagated to this by a constant
*/
default NDArray scaleGradient(double scale) {
return this.mul(scale).add(this.stopGradient().mul(1 - scale));
}

/**
* Returns the size of this {@code NDArray} along a given axis.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public boolean hasGradient() {
return false;
}

/** {@inheritDoc} */
@Override
public NDArray stopGradient() {
throw new UnsupportedOperationException("Not supported for DLR");
}

/** {@inheritDoc} */
@Override
public ByteBuffer toByteBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ public boolean hasGradient() {
return hasGradient;
}

@Override
public NDArray stopGradient() {
return manager.invoke("stop_gradient", this, null);
}

/** {@inheritDoc} */
@Override
public ByteBuffer toByteBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public boolean hasGradient() {
return false;
}

/** {@inheritDoc} */
@Override
public NDArray stopGradient() {
throw new UnsupportedOperationException("Not supported for ONNX Runtime");
}

/** {@inheritDoc} */
@Override
public ByteBuffer toByteBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ public boolean hasGradient() {
return false;
}

/** {@inheritDoc} */
@Override
public NDArray stopGradient() {
throw new UnsupportedOperationException("Not supported");
}

/** {@inheritDoc} */
@Override
public ByteBuffer toByteBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ public boolean hasGradient() {
return hasGradient;
}

/** {@inheritDoc} */
@Override
public NDArray stopGradient() {
throw new UnsupportedOperationException("Not supported");
}

/** {@inheritDoc} */
@Override
public ByteBuffer toByteBuffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ public boolean hasGradient() {
return false;
}

/** {@inheritDoc} */
@Override
public NDArray stopGradient() {
throw new UnsupportedOperationException("Not implemented");
}

/** {@inheritDoc} */
@Override
public double[] toDoubleArray() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ public boolean hasGradient() {
return false;
}

/** {@inheritDoc} */
@Override
public NDArray stopGradient() {
throw new UnsupportedOperationException("Not supported for TFLite");
}

/** {@inheritDoc} */
@Override
public ByteBuffer toByteBuffer() {
Expand Down

0 comments on commit e71f178

Please sign in to comment.