diff options
Diffstat (limited to 'tensorflow/java')
11 files changed, 138 insertions, 170 deletions
diff --git a/tensorflow/java/maven/README.md b/tensorflow/java/maven/README.md index 3e030dcd09..cbc64a284f 100644 --- a/tensorflow/java/maven/README.md +++ b/tensorflow/java/maven/README.md @@ -151,16 +151,6 @@ conducted in a [Docker](https://www.docker.com) container. 7. Upon successful release, commit changes to all the `pom.xml` files (which should have the updated version number). -### Snapshots - -If the `TF_VERSION` provided to the `release.sh` script ends in `-SNAPSHOT`, -then instead of using official release files, the nightly build artifacts from -https://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow/, -https://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow-windows/ and -https://ci.tensorflow.org/view/Nightly/job/nightly-android -will be used to upload to the Maven Central snapshots repository. (Note that -snapshots are only uploaded to Maven Central, not Bintray.) - ### Skip deploying to a repository Should you need, setting environment variables `DEPLOY_OSSRH=0` or @@ -173,12 +163,12 @@ cannot skip deploying to OSSRH for a `-SNAPSHOT` version. This section provides some pointers around how artifacts are currently assembled. -All native and java code is first built and tested on -a [Tensorflow Jenkins server](https://ci.tensorflow.org/) which run various -scripts under the [`tools/ci_build`](../../tools/ci_build/) directory. Of -particular interest may be `tools/ci_build/builds/libtensorflow.sh` which -bundles Java-related build sources and outputs into archives, and -`tools/ci_build/builds/android_full.sh` which produces an Android AAR package. +All native and java code is first built and tested by the release process +which run various scripts under the [`tools/ci_build`](../../tools/ci_build/) +directory. Of particular interest may be +`tools/ci_build/builds/libtensorflow.sh` which bundles Java-related build +sources and outputs into archives, and `tools/ci_build/builds/android_full.sh` +which produces an Android AAR package. Maven artifacts however are not created in Jenkins. Instead, artifacts are created and deployed externally on-demand, when a maintainer runs the diff --git a/tensorflow/java/maven/run_inside_container.sh b/tensorflow/java/maven/run_inside_container.sh index 2240d6b7b9..f4794d68a9 100644 --- a/tensorflow/java/maven/run_inside_container.sh +++ b/tensorflow/java/maven/run_inside_container.sh @@ -26,12 +26,6 @@ TF_ECOSYSTEM_URL="https://github.com/tensorflow/ecosystem.git" DEPLOY_BINTRAY="${DEPLOY_BINTRAY:-true}" DEPLOY_OSSRH="${DEPLOY_OSSRH:-true}" -IS_SNAPSHOT="false" -if [[ "${TF_VERSION}" == *"-SNAPSHOT" ]]; then - IS_SNAPSHOT="true" - # Bintray does not allow snapshots. - DEPLOY_BINTRAY="false" -fi PROTOC_RELEASE_URL="https://github.com/google/protobuf/releases/download/v3.5.1/protoc-3.5.1-linux-x86_64.zip" if [[ "${DEPLOY_BINTRAY}" != "true" && "${DEPLOY_OSSRH}" != "true" ]]; then echo "Must deploy to at least one of Bintray or OSSRH" >&2 @@ -69,11 +63,7 @@ mvn_property() { } download_libtensorflow() { - if [[ "${IS_SNAPSHOT}" == "true" ]]; then - URL="http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow/TYPE=cpu-slave/lastSuccessfulBuild/artifact/lib_package/libtensorflow-src.jar" - else - URL="${RELEASE_URL_PREFIX}/libtensorflow-src-${TF_VERSION}.jar" - fi + URL="${RELEASE_URL_PREFIX}/libtensorflow-src-${TF_VERSION}.jar" curl -L "${URL}" -o /tmp/src.jar cd "${DIR}/libtensorflow" jar -xvf /tmp/src.jar @@ -101,17 +91,9 @@ download_libtensorflow_jni() { mkdir windows-x86_64 mkdir darwin-x86_64 - if [[ "${IS_SNAPSHOT}" == "true" ]]; then - # Nightly builds from http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow/ - # and http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow-windows/ - curl -L "http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow/TYPE=cpu-slave/lastSuccessfulBuild/artifact/lib_package/libtensorflow_jni-cpu-linux-x86_64.tar.gz" | tar -xvz -C linux-x86_64 - curl -L "http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow/TYPE=mac-slave/lastSuccessfulBuild/artifact/lib_package/libtensorflow_jni-cpu-darwin-x86_64.tar.gz" | tar -xvz -C darwin-x86_64 - curl -L "http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow-windows/lastSuccessfulBuild/artifact/lib_package/libtensorflow_jni-cpu-windows-x86_64.zip" -o /tmp/windows.zip - else - curl -L "${RELEASE_URL_PREFIX}/libtensorflow_jni-cpu-linux-x86_64-${TF_VERSION}.tar.gz" | tar -xvz -C linux-x86_64 - curl -L "${RELEASE_URL_PREFIX}/libtensorflow_jni-cpu-darwin-x86_64-${TF_VERSION}.tar.gz" | tar -xvz -C darwin-x86_64 - curl -L "${RELEASE_URL_PREFIX}/libtensorflow_jni-cpu-windows-x86_64-${TF_VERSION}.zip" -o /tmp/windows.zip - fi + curl -L "${RELEASE_URL_PREFIX}/libtensorflow_jni-cpu-linux-x86_64-${TF_VERSION}.tar.gz" | tar -xvz -C linux-x86_64 + curl -L "${RELEASE_URL_PREFIX}/libtensorflow_jni-cpu-darwin-x86_64-${TF_VERSION}.tar.gz" | tar -xvz -C darwin-x86_64 + curl -L "${RELEASE_URL_PREFIX}/libtensorflow_jni-cpu-windows-x86_64-${TF_VERSION}.zip" -o /tmp/windows.zip unzip /tmp/windows.zip -d windows-x86_64 rm -f /tmp/windows.zip @@ -129,13 +111,7 @@ download_libtensorflow_jni_gpu() { mkdir linux-x86_64 - if [[ "${IS_SNAPSHOT}" == "true" ]]; then - # Nightly builds from http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow/ - # and http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow-windows/ - curl -L "http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow/TYPE=gpu-linux/lastSuccessfulBuild/artifact/lib_package/libtensorflow_jni-gpu-linux-x86_64.tar.gz" | tar -xvz -C linux-x86_64 - else - curl -L "${RELEASE_URL_PREFIX}/libtensorflow_jni-gpu-linux-x86_64-${TF_VERSION}.tar.gz" | tar -xvz -C linux-x86_64 - fi + curl -L "${RELEASE_URL_PREFIX}/libtensorflow_jni-gpu-linux-x86_64-${TF_VERSION}.tar.gz" | tar -xvz -C linux-x86_64 # Updated timestamps seem to be required to get Maven to pick up the file. touch linux-x86_64/* @@ -165,11 +141,7 @@ generate_java_protos() { rm -f "/tmp/protoc.zip" # Download the release archive of TensorFlow protos. - if [[ "${IS_SNAPSHOT}" == "true" ]]; then - URL="http://ci.tensorflow.org/view/Nightly/job/nightly-libtensorflow/TYPE=cpu-slave/lastSuccessfulBuild/artifact/lib_package/libtensorflow_proto.zip" - else - URL="${RELEASE_URL_PREFIX}/libtensorflow_proto-${TF_VERSION}.zip" - fi + URL="${RELEASE_URL_PREFIX}/libtensorflow_proto-${TF_VERSION}.zip" curl -L "${URL}" -o /tmp/libtensorflow_proto.zip mkdir -p "${DIR}/proto/tmp/src" unzip -d "${DIR}/proto/tmp/src" "/tmp/libtensorflow_proto.zip" @@ -238,11 +210,7 @@ deploy_profile() { # Determine the correct pom file property to use # for the repository url. local rtype - if [[ "${IS_SNAPSHOT}" == "true" ]]; then - rtype='snapshotRepository' - else - rtype='repository' - fi + rtype='repository' local url=$(mvn_property "${profile}" "project.distributionManagement.${rtype}.url") local repositoryId=$(mvn_property "${profile}" "project.distributionManagement.${rtype}.id") mvn gpg:sign-and-deploy-file \ @@ -300,17 +268,13 @@ mvn verify deploy_artifacts set +ex -if [[ "${IS_SNAPSHOT}" == "false" ]]; then - echo "Uploaded to the staging repository" - echo "After validating the release: " - if [[ "${DEPLOY_OSSRH}" == "true" ]]; then - echo "* Login to https://oss.sonatype.org/#stagingRepositories" - echo "* Find the 'org.tensorflow' staging release and click either 'Release' to release or 'Drop' to abort" - fi - if [[ "${DEPLOY_BINTRAY}" == "true" ]]; then - echo "* Login to https://bintray.com/google/tensorflow/tensorflow" - echo "* Either 'Publish' unpublished items to release, or 'Discard' to abort" - fi -else - echo "Uploaded to the snapshot repository" +echo "Uploaded to the staging repository" +echo "After validating the release: " +if [[ "${DEPLOY_OSSRH}" == "true" ]]; then + echo "* Login to https://oss.sonatype.org/#stagingRepositories" + echo "* Find the 'org.tensorflow' staging release and click either 'Release' to release or 'Drop' to abort" +fi +if [[ "${DEPLOY_BINTRAY}" == "true" ]]; then + echo "* Login to https://bintray.com/google/tensorflow/tensorflow" + echo "* Either 'Publish' unpublished items to release, or 'Discard' to abort" fi diff --git a/tensorflow/java/maven/tensorflow-android/update.py b/tensorflow/java/maven/tensorflow-android/update.py index 2206d800ca..c620564072 100644 --- a/tensorflow/java/maven/tensorflow-android/update.py +++ b/tensorflow/java/maven/tensorflow-android/update.py @@ -86,19 +86,10 @@ def read_template(path): def main(): args = get_args() - # Artifacts are downloaded from the ci build. A SNAPSHOT release is - # associated with artifacts from the last successful nightly build. Otherwise, - # it comes from the officially blessed release artifacts. - if args.version.endswith('SNAPSHOT'): - info_url = ('https://ci.tensorflow.org/view/Nightly/job/nightly-android' - '/lastSuccessfulBuild/api/json') - aar_url = None - build_type = 'nightly-android' - else: - release_prefix = 'https://storage.googleapis.com/tensorflow/libtensorflow' - info_url = '%s/android_buildinfo-%s.json' % (release_prefix, args.version) - aar_url = '%s/tensorflow-%s.aar' % (release_prefix, args.version) - build_type = 'release-android' + release_prefix = 'https://storage.googleapis.com/tensorflow/libtensorflow' + info_url = '%s/android_buildinfo-%s.json' % (release_prefix, args.version) + aar_url = '%s/tensorflow-%s.aar' % (release_prefix, args.version) + build_type = 'release-android' # Retrieve build information build_info = get_json(info_url) diff --git a/tensorflow/java/src/main/java/org/tensorflow/Graph.java b/tensorflow/java/src/main/java/org/tensorflow/Graph.java index abca956b97..752b49af04 100644 --- a/tensorflow/java/src/main/java/org/tensorflow/Graph.java +++ b/tensorflow/java/src/main/java/org/tensorflow/Graph.java @@ -144,22 +144,23 @@ public final class Graph implements AutoCloseable { } /** - * Adds operations to compute the partial derivatives of sum of {@code y}s w.r.t {@code x}s, - * i.e., {@code d(y_1 + y_2 + ...)/dx_1, d(y_1 + y_2 + ...)/dx_2...} - * <p> - * {@code dx} are used as initial gradients (which represent the symbolic partial derivatives of some loss function - * {@code L} w.r.t. {@code y}). {@code dx} must be null or have size of {@code y}. - * <p> - * If {@code dx} is null, the implementation will use dx of {@link org.tensorflow.op.core.OnesLike OnesLike} for all - * shapes in {@code y}. - * <p> - * {@code prefix} is used as the name prefix applied to all nodes added to the graph to compute gradients. It must - * be unique within the provided graph or the operation will fail. - * <p> - * If {@code prefix} is null, then one will be chosen automatically. - * - * @param prefix unique string prefix applied before the names of nodes added to the graph to compute gradients. - * If null, a default one will be chosen. + * Adds operations to compute the partial derivatives of sum of {@code y}s w.r.t {@code x}s, i.e., + * {@code d(y_1 + y_2 + ...)/dx_1, d(y_1 + y_2 + ...)/dx_2...} + * + * <p>{@code dx} are used as initial gradients (which represent the symbolic partial derivatives + * of some loss function {@code L} w.r.t. {@code y}). {@code dx} must be null or have size of + * {@code y}. + * + * <p>If {@code dx} is null, the implementation will use dx of {@link + * org.tensorflow.op.core.OnesLike OnesLike} for all shapes in {@code y}. + * + * <p>{@code prefix} is used as the name prefix applied to all nodes added to the graph to compute + * gradients. It must be unique within the provided graph or the operation will fail. + * + * <p>If {@code prefix} is null, then one will be chosen automatically. + * + * @param prefix unique string prefix applied before the names of nodes added to the graph to + * compute gradients. If null, a default one will be chosen. * @param y output of the function to derive * @param x inputs of the function for which partial derivatives are computed * @param dx if not null, the partial derivatives of some loss function {@code L} w.r.t. {@code y} @@ -192,12 +193,21 @@ public final class Graph implements AutoCloseable { dxIndices[i] = dx[i].index(); } } - // Gradient outputs are returned in two continuous arrays concatenated into one. The first holds the native - // handles of the gradient operations while the second holds the index of their output e.g. given - // xHandles = [x0Handle, x1Handle, ...] and xIndices = [x0Index, x1Index, ..], we obtain + // Gradient outputs are returned in two continuous arrays concatenated into one. The first + // holds the native handles of the gradient operations while the second holds the index of + // their output e.g. given + // xHandles = [x0Handle, x1Handle, ...] and xIndices = [x0Index, x1Index, ..], we obtain // dy = [dy0Handle, dy1Handle, ..., dy0Index, dy1Index, ...] long[] dyHandlesAndIndices = - addGradients(ref.nativeHandle(), prefix, yHandles, yIndices, xHandles, xIndices, dxHandles, dxIndices); + addGradients( + ref.nativeHandle(), + prefix, + yHandles, + yIndices, + xHandles, + xIndices, + dxHandles, + dxIndices); int ndy = dyHandlesAndIndices.length >> 1; if (ndy != dy.length) { throw new IllegalStateException(String.valueOf(ndy) + " gradients were added to the graph when " + dy.length @@ -214,16 +224,16 @@ public final class Graph implements AutoCloseable { /** * Adds operations to compute the partial derivatives of sum of {@code y}s w.r.t {@code x}s, * i.e., {@code dy/dx_1, dy/dx_2...} - * <p> + * <p> * This is a simplified version of {@link #addGradients(Output[], Output[], Output[]) where {@code y} is * a single output, {@code dx} is null and {@code prefix} is null. - * + * * @param y output of the function to derive * @param x inputs of the function for which partial derivatives are computed * @return the partial derivatives {@code dy} with the size of {@code x} */ public Output<?>[] addGradients(Output<?> y, Output<?>[] x) { - return addGradients(null, new Output<?>[]{y}, x, null); + return addGradients(null, new Output<?>[] {y}, x, null); } private final Object nativeHandleLock = new Object(); @@ -337,8 +347,15 @@ public final class Graph implements AutoCloseable { private static native byte[] toGraphDef(long handle); - private static native long[] addGradients(long handle, String prefix, long[] inputHandles, int[] inputIndices, - long[] outputHandles, int[] outputIndices, long[] gradInputHandles, int[] gradInputIndices); + private static native long[] addGradients( + long handle, + String prefix, + long[] inputHandles, + int[] inputIndices, + long[] outputHandles, + int[] outputIndices, + long[] gradInputHandles, + int[] gradInputIndices); static { TensorFlow.init(); diff --git a/tensorflow/java/src/main/java/org/tensorflow/op/Scope.java b/tensorflow/java/src/main/java/org/tensorflow/op/Scope.java index 563ea66ef1..5a233bcc98 100644 --- a/tensorflow/java/src/main/java/org/tensorflow/op/Scope.java +++ b/tensorflow/java/src/main/java/org/tensorflow/op/Scope.java @@ -135,8 +135,8 @@ public final class Scope { * }</pre> * * <p><b>Note:</b> if you provide a composite operator building class (i.e, a class that adds a - * set of related operations to the graph by calling other operator building code), the provided name - * will act as a subscope to all underlying operators. + * set of related operations to the graph by calling other operator building code), the provided + * name will act as a subscope to all underlying operators. * * @param defaultName name for the underlying operator. * @return unique name for the operator. diff --git a/tensorflow/java/src/main/java/org/tensorflow/op/core/Gradients.java b/tensorflow/java/src/main/java/org/tensorflow/op/core/Gradients.java index 5432ff244e..eea9dc1c47 100644 --- a/tensorflow/java/src/main/java/org/tensorflow/op/core/Gradients.java +++ b/tensorflow/java/src/main/java/org/tensorflow/op/core/Gradients.java @@ -18,7 +18,6 @@ package org.tensorflow.op.core; import java.util.Arrays; import java.util.Iterator; import java.util.List; - import org.tensorflow.Operand; import org.tensorflow.Output; import org.tensorflow.op.Op; @@ -54,7 +53,7 @@ public class Gradients implements Op, Iterable<Operand<?>> { * Optional attributes for {@link Gradients} */ public static class Options { - + /** * @param dx partial derivatives of some loss function {@code L} w.r.t. {@code y} * @return this option builder @@ -63,23 +62,27 @@ public class Gradients implements Op, Iterable<Operand<?>> { this.dx = dx; return this; } - + private Iterable<? extends Operand<?>> dx; - + private Options() { } } /** * Adds gradients computation ops to the graph according to scope. - * + * * @param scope current graph scope * @param y outputs of the function to derive * @param x inputs of the function for which partial derivatives are computed * @param options carries optional attributes values * @return a new instance of {@code Gradients} */ - public static Gradients create(Scope scope, Iterable<? extends Operand<?>> y, Iterable<? extends Operand<?>> x, Options... options) { + public static Gradients create( + Scope scope, + Iterable<? extends Operand<?>> y, + Iterable<? extends Operand<?>> x, + Options... options) { Output<?>[] dx = null; if (options != null) { for (Options opts : options) { @@ -88,20 +91,20 @@ public class Gradients implements Op, Iterable<Operand<?>> { } } } - Output<?>[] dy = scope.graph().addGradients( - scope.makeOpName("Gradients"), - Operands.asOutputs(y), - Operands.asOutputs(x), - dx); + Output<?>[] dy = + scope + .graph() + .addGradients( + scope.makeOpName("Gradients"), Operands.asOutputs(y), Operands.asOutputs(x), dx); return new Gradients(Arrays.asList(dy)); } /** * Adds gradients computation ops to the graph according to scope. - * - * This is a simplified version of {@link #create(Scope, Iterable, Iterable, Options...)} where {@code y} is - * a single output. - * + * + * <p>This is a simplified version of {@link #create(Scope, Iterable, Iterable, Options...)} where + * {@code y} is a single output. + * * @param scope current graph scope * @param y output of the function to derive * @param x inputs of the function for which partial derivatives are computed @@ -109,7 +112,8 @@ public class Gradients implements Op, Iterable<Operand<?>> { * @return a new instance of {@code Gradients} */ @SuppressWarnings({"unchecked", "rawtypes"}) - public static Gradients create(Scope scope, Operand<?> y, Iterable<? extends Operand<?>> x, Options... options) { + public static Gradients create( + Scope scope, Operand<?> y, Iterable<? extends Operand<?>> x, Options... options) { return create(scope, (Iterable) Arrays.asList(y), x, options); } @@ -133,11 +137,11 @@ public class Gradients implements Op, Iterable<Operand<?>> { public List<Output<?>> dy() { return dy; } - + /** * Returns a symbolic handle to one of the gradient operation output - * <p> - * Warning: Does not check that the type of the tensor matches T. It is recommended to call + * + * <p>Warning: Does not check that the type of the tensor matches T. It is recommended to call * this method with an explicit type parameter rather than letting it be inferred, e.g. {@code * gradients.<Float>dy(0)} * diff --git a/tensorflow/java/src/main/native/graph_jni.cc b/tensorflow/java/src/main/native/graph_jni.cc index 1bbda52641..f1744d8769 100644 --- a/tensorflow/java/src/main/native/graph_jni.cc +++ b/tensorflow/java/src/main/native/graph_jni.cc @@ -133,12 +133,10 @@ Java_org_tensorflow_Graph_toGraphDef(JNIEnv* env, jclass clazz, jlong handle) { return ret; } -JNIEXPORT jlongArray JNICALL -Java_org_tensorflow_Graph_addGradients(JNIEnv* env, jclass clazz, jlong handle, - jstring prefix, jlongArray y_handles, jintArray y_indices, - jlongArray x_handles, jintArray x_indices, - jlongArray dx_handles, jintArray dx_indices) { - +JNIEXPORT jlongArray JNICALL Java_org_tensorflow_Graph_addGradients( + JNIEnv* env, jclass clazz, jlong handle, jstring prefix, + jlongArray y_handles, jintArray y_indices, jlongArray x_handles, + jintArray x_indices, jlongArray dx_handles, jintArray dx_indices) { TF_Graph* g = requireHandle(env, handle); if (g == nullptr) return nullptr; diff --git a/tensorflow/java/src/main/native/graph_jni.h b/tensorflow/java/src/main/native/graph_jni.h index e483bf953b..215695cdfd 100644 --- a/tensorflow/java/src/main/native/graph_jni.h +++ b/tensorflow/java/src/main/native/graph_jni.h @@ -78,9 +78,9 @@ JNIEXPORT jbyteArray JNICALL Java_org_tensorflow_Graph_toGraphDef(JNIEnv *, * Method: name * Signature: (JLjava/lang/String;[J[I[J[I[J[I)[J */ -JNIEXPORT jlongArray JNICALL Java_org_tensorflow_Graph_addGradients(JNIEnv *, - jclass, jlong, jstring, jlongArray, jintArray, jlongArray, jintArray, - jlongArray, jintArray); +JNIEXPORT jlongArray JNICALL Java_org_tensorflow_Graph_addGradients( + JNIEnv *, jclass, jlong, jstring, jlongArray, jintArray, jlongArray, + jintArray, jlongArray, jintArray); #ifdef __cplusplus } // extern "C" diff --git a/tensorflow/java/src/test/java/org/tensorflow/GraphTest.java b/tensorflow/java/src/test/java/org/tensorflow/GraphTest.java index 56c8f22daa..7c05c1deaf 100644 --- a/tensorflow/java/src/test/java/org/tensorflow/GraphTest.java +++ b/tensorflow/java/src/test/java/org/tensorflow/GraphTest.java @@ -22,7 +22,6 @@ import static org.junit.Assert.assertTrue; import java.util.HashSet; import java.util.Iterator; - import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -180,7 +179,7 @@ public class GraphTest { Output<Float> x = TestUtil.placeholder(g, "x", Float.class); Output<Float> y0 = TestUtil.square(g, "y0", x); Output<Float> y1 = TestUtil.square(g, "y1", y0); - + Output<?>[] grad = g.addGradients(null, toArray(y0, y1), toArray(x), null); assertNotNull(grad); assertEquals(1, grad.length); @@ -228,14 +227,14 @@ public class GraphTest { } } } - + @Test public void validateGradientsNames() { try (Graph g = new Graph()) { Output<Float> x = TestUtil.placeholder(g, "x", Float.class); Output<Float> y0 = TestUtil.square(g, "y0", x); - + Output<?>[] grad0 = g.addGradients(null, toArray(y0), toArray(x), null); assertTrue(grad0[0].op().name().startsWith("gradients/")); diff --git a/tensorflow/java/src/test/java/org/tensorflow/op/ScopeTest.java b/tensorflow/java/src/test/java/org/tensorflow/op/ScopeTest.java index 0e9c7df697..125de73554 100644 --- a/tensorflow/java/src/test/java/org/tensorflow/op/ScopeTest.java +++ b/tensorflow/java/src/test/java/org/tensorflow/op/ScopeTest.java @@ -21,7 +21,6 @@ import static org.junit.Assert.fail; import java.util.HashMap; import java.util.Map; - import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; diff --git a/tensorflow/java/src/test/java/org/tensorflow/op/core/GradientsTest.java b/tensorflow/java/src/test/java/org/tensorflow/op/core/GradientsTest.java index b75f79a421..3f49790b29 100644 --- a/tensorflow/java/src/test/java/org/tensorflow/op/core/GradientsTest.java +++ b/tensorflow/java/src/test/java/org/tensorflow/op/core/GradientsTest.java @@ -1,3 +1,18 @@ +/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + package org.tensorflow.op.core; import static org.junit.Assert.assertEquals; @@ -5,7 +20,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Arrays; - import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -22,28 +36,25 @@ public class GradientsTest { @Test public void createGradients() { - try (Graph g = new Graph(); + try (Graph g = new Graph(); Session sess = new Session(g)) { Scope scope = new Scope(g); Output<Float> x = TestUtil.placeholder(g, "x1", Float.class); Output<Float> y0 = TestUtil.square(g, "y0", x); Output<Float> y1 = TestUtil.square(g, "y1", y0); - + Gradients grads = Gradients.create(scope, y1, Arrays.asList(x, y0)); - + assertNotNull(grads); assertNotNull(grads.dy()); assertEquals(2, grads.dy().size()); try (Tensor<Float> c = Tensors.create(3.0f); - TestUtil.AutoCloseableList<Tensor<?>> outputs = new TestUtil.AutoCloseableList<>( - sess.runner() - .feed(x, c) - .fetch(grads.dy(0)) - .fetch(grads.dy(1)) - .run())) { - + TestUtil.AutoCloseableList<Tensor<?>> outputs = + new TestUtil.AutoCloseableList<>( + sess.runner().feed(x, c).fetch(grads.dy(0)).fetch(grads.dy(1)).run())) { + assertEquals(108.0f, outputs.get(0).floatValue(), 0.0f); assertEquals(18.0f, outputs.get(1).floatValue(), 0.0f); } @@ -59,20 +70,17 @@ public class GradientsTest { Output<Float> x = TestUtil.placeholder(g, "x1", Float.class); Output<Float> y0 = TestUtil.square(g, "y0", x); Output<Float> y1 = TestUtil.square(g, "y1", y0); - + Gradients grads = Gradients.create(scope, Arrays.asList(y0, y1), Arrays.asList(x)); - + assertNotNull(grads); assertNotNull(grads.dy()); assertEquals(1, grads.dy().size()); try (Tensor<Float> c = Tensors.create(3.0f); - TestUtil.AutoCloseableList<Tensor<?>> outputs = new TestUtil.AutoCloseableList<>( - sess.runner() - .feed(x, c) - .fetch(grads.dy(0)) - .run())) { - + TestUtil.AutoCloseableList<Tensor<?>> outputs = + new TestUtil.AutoCloseableList<>(sess.runner().feed(x, c).fetch(grads.dy(0)).run())) { + assertEquals(114.0f, outputs.get(0).floatValue(), 0.0f); } } @@ -87,21 +95,19 @@ public class GradientsTest { Output<Float> x = TestUtil.placeholder(g, "x1", Float.class); Output<Float> y0 = TestUtil.square(g, "y0", x); Output<Float> y1 = TestUtil.square(g, "y1", y0); - + Gradients grads0 = Gradients.create(scope, y1, Arrays.asList(y0)); Gradients grads1 = Gradients.create(scope, y0, Arrays.asList(x), Gradients.dx(grads0.dy())); - + assertNotNull(grads1); assertNotNull(grads1.dy()); assertEquals(1, grads1.dy().size()); try (Tensor<Float> c = Tensors.create(3.0f); - TestUtil.AutoCloseableList<Tensor<?>> outputs = new TestUtil.AutoCloseableList<>( - sess.runner() - .feed(x, c) - .fetch(grads1.dy(0)) - .run())) { - + TestUtil.AutoCloseableList<Tensor<?>> outputs = + new TestUtil.AutoCloseableList<>( + sess.runner().feed(x, c).fetch(grads1.dy(0)).run())) { + assertEquals(108.0f, outputs.get(0).floatValue(), 0.0f); } } @@ -114,10 +120,10 @@ public class GradientsTest { Output<Float> x = TestUtil.placeholder(g, "x1", Float.class); Output<Float> y = TestUtil.square(g, "y", x); - + Gradients grad0 = Gradients.create(scope, y, Arrays.asList(x)); assertTrue(grad0.dy(0).op().name().startsWith("sub/Gradients/")); - + Gradients grad1 = Gradients.create(scope.withName("MyGradients"), y, Arrays.asList(x)); assertTrue(grad1.dy(0).op().name().startsWith("sub/MyGradients/")); } |