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 NDArray stopGradient and scaleGradient #548

Merged
merged 1 commit into from
Jan 21, 2021
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
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