Skip to content

Merge main branch to local branch #4

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 5 commits into from
Oct 27, 2020
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
25 changes: 4 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
# TensorFlow for Java

***!!! IMPORTANT NOTICE !!! This repository is UNDER CONSTRUCTION and does not yet host the code of the
offical TensorFlow Java artifacts!***

***Please refer to the [TensorFlow Java module](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java)
of the main repository for the actual code.***

## Welcome to the Java world of TensorFlow!

TensorFlow can run on any JVM for building, training and running machine learning models. It comes with
Expand All @@ -30,19 +24,10 @@ The following describes the layout of the repository and its different artifacts
TensorFlow and just want a thin layer to access the TensorFlow runtime from the JVM

* `tensorflow-framework`
* Complete but fairly primitive API for building and training neural networks with TensorFlow
* Intended audience: expert neural network developers who prefer to make explicit, detailed decisions
about their models and training algorithms

* `tensorflow-keras` (early WIP; only defined in `dev` profile)
* Partially covers the framework API to allow simpler definition of models and training algorithms
* Intended to be familiar if you know the Python Keras API, but prioritizes clean, idiomatic Java
over fidelity to Python
* Provides defaults based on common best practices
* Allows developers to selectively be more explicit by overriding defaults or dipping into the framework API
* Intended audience: neural network developers across the spectrum from beginner to expert who prefer to
rely mostly on best-practice defaults and then selectively fine-tune

* Primary API for building and training neural networks with TensorFlow
* Intended audience: neural network developers
* For more information: [tensorflow-framework/README.md](tensorflow-framework/README.md)

* `ndarray`
* Generic utility library for n-dimensional data I/O operations
* Used by TensorFlow but does not depend on TensorFlow
Expand Down Expand Up @@ -172,8 +157,6 @@ This table shows the mapping between different version of TensorFlow for Java an

| TensorFlow Java Version | TensorFlow Version |
| ------------- | ------------- |
| 0.1.0-SNAPSHOT | 2.2.0 |
| 0.2.0-SNAPSHOT | 2.3.1 |
| 0.2.0 | 2.3.1 |
| 0.3.0-SNAPSHOT | 2.3.1 |

Expand Down
13 changes: 0 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,6 @@
</dependencyManagement>

<profiles>
<!--
Developing profile
The 'dev' profile is used for local development or PR compilation check only.
Here, we enable the `tensorflow-keras` module only under this profile, until
it is mature enough for being deployed and distributed for the end users.
-->
<profile>
<id>dev</id>
<modules>
<!-- Disabled while the library is still empty -->
<!--module>tensorflow-keras</module-->
</modules>
</profile>
<!--
Deploying profile
Build the Javadoc when deploying
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
op {
graph_op_name: "LeakyRelu"
visibility: VISIBLE
endpoint {
name: "nn.LeakyRelu"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.tensorflow.op.nn.FusedResizeAndPadConv2d;
import org.tensorflow.op.nn.InTopK;
import org.tensorflow.op.nn.L2Loss;
import org.tensorflow.op.nn.LeakyRelu;
import org.tensorflow.op.nn.LearnedUnigramCandidateSampler;
import org.tensorflow.op.nn.LocalResponseNormalization;
import org.tensorflow.op.nn.LogSoftmax;
Expand Down Expand Up @@ -1226,6 +1227,19 @@ public <T extends TNumber> L2Loss<T> l2Loss(Operand<T> t) {
return L2Loss.create(scope, t);
}

/**
* Computes rectified linear: `max(features, features * alpha)`.
*
* @param <T> data type for {@code activations()} output
* @param features
* @param options carries optional attributes values
* @return a new instance of LeakyRelu
*/
public <T extends TNumber> LeakyRelu<T> leakyRelu(Operand<T> features,
LeakyRelu.Options... options) {
return LeakyRelu.create(scope, features, options);
}

/**
* Generates labels for candidate sampling with a learned unigram distribution.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @param <T> data type for {@code activations()} output
*/
@Operator(group = "nn")
public final class LeakyRelu<T extends TNumber> extends RawOp implements Operand<T> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ opts, runOpts, new BytePointer(exportDir), new PointerPointer(tags),
}

private static void validateTags(String[] tags) {
if (tags == null || tags.length == 0 || Arrays.stream(tags).anyMatch(t -> t == null || t.isEmpty())) {
if (tags == null || Arrays.stream(tags).anyMatch(t -> t == null || t.isEmpty())) {
throw new IllegalArgumentException("Invalid tags: " + Arrays.toString(tags));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.tensorflow.ndarray.NdArray;
import org.tensorflow.ndarray.StdArrays;
import org.tensorflow.ndarray.impl.dense.FloatDenseNdArray;
import org.tensorflow.types.family.TNumber;
import org.tensorflow.types.family.TFloating;

/**
* Brain 16-bit float tensor type.
Expand All @@ -48,7 +48,7 @@
* <p>Note that some CPUs support the bfloat16 format natively, which can result in faster
* computation compared to {@link TFloat16} when GPUs are not used.
*/
public interface TBfloat16 extends FloatNdArray, TNumber {
public interface TBfloat16 extends FloatNdArray, TFloating {
/** readable-name for the data type */
static final String NAME = "BFLOAT16";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.tensorflow.ndarray.NdArray;
import org.tensorflow.ndarray.StdArrays;
import org.tensorflow.ndarray.impl.dense.FloatDenseNdArray;
import org.tensorflow.types.family.TNumber;
import org.tensorflow.types.family.TFloating;

/**
* IEEE-754 half-precision 16-bit float tensor type.
Expand All @@ -45,7 +45,7 @@
* most CPUs do not support this format natively. For CPU computation on 16-bit floats, the {@link
* TBfloat16} tensor type might be a better option.
*/
public interface TFloat16 extends FloatNdArray, TNumber {
public interface TFloat16 extends FloatNdArray, TFloating {

/** readable-name for the data type */
static final String NAME = "FLOAT16";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import org.tensorflow.ndarray.NdArray;
import org.tensorflow.ndarray.StdArrays;
import org.tensorflow.ndarray.impl.dense.FloatDenseNdArray;
import org.tensorflow.types.family.TNumber;
import org.tensorflow.types.family.TFloating;

/** IEEE-754 single-precision 32-bit float tensor type. */
public interface TFloat32 extends FloatNdArray, TNumber {
public interface TFloat32 extends FloatNdArray, TFloating {

/** readable-name for the data type */
static final String NAME = "FLOAT";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
import org.tensorflow.ndarray.NdArray;
import org.tensorflow.ndarray.StdArrays;
import org.tensorflow.ndarray.impl.dense.DoubleDenseNdArray;
import org.tensorflow.types.family.TNumber;
import org.tensorflow.types.family.TFloating;


/** IEEE-754 double-precision 64-bit float tensor type. */
public interface TFloat64 extends DoubleNdArray, TNumber {
public interface TFloat64 extends DoubleNdArray, TFloating {

/** readable-name for the data type */
static final String NAME = "DOUBLE";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.tensorflow.types.family;

/**
* Marker interface for floating point tensor types.
*
* <p>Operations that only accepts floating point values as some of their operands enforce that the tensor
* types for these operands to be bound to this interface. For example:
*
* <pre>{@code
* TFloat32 tensor1 = TFloat32.vectorOf(1, 2, 3);
* TBool tensor2 = TBool.vectorOf(true, false, true);
*
* Ops tf = Ops.create();
* Exponential<TFloat32> exp = new Exponential<>(tf);
* exp.call(tf.constant(tensor1)); // OK
* exp.call(tf.constant(tensor2)); // Compilation failure
* }</pre>
*/
public interface TFloating extends TNumber {}
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ public void cannotExportMultipleFunctionsWithSameSignatureKey() throws IOExcepti
@Test
public void cannotExportOrImportInvalidTags() {
assertThrows(IllegalArgumentException.class, () ->
SavedModelBundle.loader("/").withTags()
);
assertThrows(IllegalArgumentException.class, () ->
SavedModelBundle.loader("/").withTags(new String[]{})
SavedModelBundle.loader("/").withTags(null)
);
assertThrows(IllegalArgumentException.class, () ->
SavedModelBundle.loader("/").withTags(new String[]{"tag", null})
Expand All @@ -274,10 +271,7 @@ public void cannotExportOrImportInvalidTags() {
SavedModelBundle.loader("/").withTags(new String[]{"tag", ""})
);
assertThrows(IllegalArgumentException.class, () ->
SavedModelBundle.exporter("/").withTags()
);
assertThrows(IllegalArgumentException.class, () ->
SavedModelBundle.exporter("/").withTags(new String[]{})
SavedModelBundle.exporter("/").withTags(null)
);
assertThrows(IllegalArgumentException.class, () ->
SavedModelBundle.exporter("/").withTags(new String[]{"tag", null})
Expand Down
28 changes: 28 additions & 0 deletions tensorflow-framework/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Framework API

This is the primary Java API for building and training neural networks with TensorFlow.
This API deliberately mirrors the overall structure of Python Keras. However, it
is intended as a comfortable, idiomatic Java API for developers who may or may not
be familiar with Keras.

This API is intended to provide convenient, sensible defaults, while still allowing you to
exercise fine control over the details of your model, training, and inference when necessary.

More specifically, the following goals drive API evolution:

* If either you know how to implement a model in the Python Keras API, or you are reimplementing an
existing Python Keras model in Java, you should be able to cleanly and naturally follow the same
high-level structure in the framework API.

* Also, given some familiarity with patterns followed throughout the framework API, you should be
able to easily translate every detail of a Python Keras implementation into the framework API.

* However, the framework API is not intended to literally mimic the Python Keras API. Rather, it
should expose the same capabilities in an API that feels natural and idiomatic to a Java
programmer who does not know Keras. If we ever find ourselves unable to reconcile this goal with
easy translation from Python Keras, we may split out a Keras layer.

* Also, the framework API should support fine control over all aspects of modeling, training, and
inference. Unlike with Python Keras, we want this to feel like staying in the same API rather
than diving into a separate layer. But here again, if we are ever unable to reconcile this goal
with easy translation from Python Keras, we may split the framework API into two layers.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright 2020 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.framework.activations;

import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TNumber;

/**
* Abstract base class for Activations
*
* <p><b>Note:</b> The {@link #tf} attribute must be set prior to invoking the call method. See
* {@link #setTF(Ops)} and the constructor {@link #Activation(Ops)}.
*
* @param <T> the data type of the activation
*/
public abstract class Activation<T extends TNumber> {

/** The TensorFlow Ops */
protected Ops tf;

/**
* Creates the abstract class for an Activation
*
* @param tf the TensorFlow Ops
*/
protected Activation(Ops tf) {
this.tf = tf;
}

/**
* Sets the TensorFlow Ops
*
* @param tf the TensorFlow Ops
*/
protected void setTF(Ops tf) {
this.tf = tf;
}

/**
* Gets the TensorFlow Ops
*
* @return the TensorFlow Ops
*/
protected Ops getTF() {
return this.tf;
}

/**
* Gets the calculation operation for the activation.
*
* @param input the input tensor
* @return The operand for the activation
*/
public abstract Operand<T> call(Operand<T> input);
}
Loading