Skip to content

Commit 2843138

Browse files
authored
Initial checkin of Keras Optimzers and helper classes. (tensorflow#91)
* Initial checkin of Keras Optimzers and helper classes. Fixed dependencies in pom.xml * Added static final NAME to replace hardcoded String in the create method. This allows the NAME to be used elsewhere instead of hardcoding the string. * Changed of method to use the DataType NAME attribute rather than hardcoding the string. added methods isFloating(), isInteger(), isNUmeric(), isBoolean() and isString() * Added method WriteFieldWithInitializer to output a "final static String OP_NAME" to each generated operation. * Added tf.nn.softmaxCrossEntropyWitLogits() and tf.nn.raw.softmaxCrossEntropyWitLogits() Added tf.nn.sparesSoftmaxCrossEntropyWithLogits() and tf.nn.raw.sparesSoftmaxCrossEntropyWithLogits() Added tf.nn.sigmoidCrossEntropyWithLogits() * Moved SoftmaxCrossEntropyWithLogits and SparseSoftmaxCrossEntropyWithLogits to org.tensorflow.op.nn.raw * Generated classes now have public static final String OP_NAME = "XXXXXXXX"; * Generated classes now have public static final String OP_NAME = "XXXXXXXX"; * fix dependencies for other Tensorflow Java modules * formatting fix * Fix ctors with name to properly pass the name to the the super ctor. * change asserts to IllegalArgumentException fix javadoc, fix casts * change asserts to IllegalArgumentException * Moved back to tests * Moved SoftmaxCrossEntropyWithLogits.java and SparseSoftmaxCrossEntropyWithLogits.java to nn.raw, added new versions of these to NnOps * Deleted files that are not necessary yet * Added nn.raw group for softmaxCrossEntropyWithLogits() and sparseSoftmaxCrossEntropyWithLogits() * Added nn.raw group for softmaxCrossEntropyWithLogits() and sparseSoftmaxCrossEntropyWithLogits() * Refactor NN into individual operations under org.tensorflow.op.nn. Fix JavaDoc. Change from snake case to camel case. * Refactor NN into individual operations under org.tensorflow.op.nn. Fix JavaDoc. Change from snake case to camel case. * Reformatted code * Added sub scope * Miscellaneous fixes based on review comments. * Fixed op_generator.cc to remove a spurious new line in the generated Java files for some Ops. This also resulted in new generated source that are also committed. * Changed back to non-generic Operand until we resolve how to handle generics. * Regenerated due to creation of SoftmaxCrossEntropyWithLogits.java, SigmoidCrossEntropyWithLogits.java, and SparseSoftmaxCrossEntropyWithLogits.java under package org.tensorflow.op.nn in * change snake case to camel case. format code * clean upd warning, format code * Added Adamax, Ftrl, and Nadam Optimizers. Added Optimizers enum for easy inclusion of a default optimizer. Cleaned up JavaDoc * Removed optimize classes from tensorflow-keras, moved optimizer test cases to framework. Created Tests for GradientDescent and Momentum * Fixed generics * Fixed from Unit test results * added @SuppressWarnings("unchecked") on Variable array
1 parent 8c67525 commit 2843138

File tree

1,201 files changed

+10940
-378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,201 files changed

+10940
-378
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
op {
22
graph_op_name: "SoftmaxCrossEntropyWithLogits"
33
endpoint {
4-
name: "nn.SoftmaxCrossEntropyWithLogits"
4+
name: "nn.raw.SoftmaxCrossEntropyWithLogits"
55
}
66
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
op {
22
graph_op_name: "SparseSoftmaxCrossEntropyWithLogits"
33
endpoint {
4-
name: "nn.SparseSoftmaxCrossEntropyWithLogits"
4+
name: "nn.raw.SparseSoftmaxCrossEntropyWithLogits"
55
}
66
}

tensorflow-core/tensorflow-core-api/src/bazel/op_generator/op_generator.cc

+13-2
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,20 @@ void GenerateOp(const OpSpec& op, const EndpointSpec& endpoint,
509509
RenderInterfaceImpl(op, mode, &writer);
510510
}
511511
writer.EndLine();
512-
for (const ArgumentSpec& output : op.outputs()) {
513-
writer.WriteField(output.var(), PRIVATE);
512+
513+
Variable nameVariable = Variable::Create("OP_NAME", Type::Class("String"));
514+
Javadoc name_javadoc = Javadoc::Create("The name of this op, as known by TensorFlow core engine");
515+
string quoted_string = "\"" + op.graph_op_name() + "\"";
516+
writer.WriteFieldWithInitializer(nameVariable, PUBLIC|STATIC|FINAL, &name_javadoc, quoted_string );
517+
518+
519+
if(!op.outputs().empty()) {
520+
writer.EndLine();
521+
for (const ArgumentSpec& output : op.outputs()) {
522+
writer.WriteField(output.var(), PRIVATE);
523+
}
514524
}
525+
515526
RenderConstructor(op, op_class, &writer);
516527
writer.EndType();
517528
}

tensorflow-core/tensorflow-core-api/src/bazel/op_generator/source_writer.cc

+16
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ SourceWriter& SourceWriter::WriteField(const Variable& field, int modifiers,
238238
return *this;
239239
}
240240

241+
SourceWriter& SourceWriter::WriteFieldWithInitializer(const Variable& field,
242+
int modifiers, const Javadoc* javadoc, const string& initializer) {
243+
// If present, write field javadoc only as one brief line
244+
if (javadoc != nullptr && !javadoc->brief().empty()) {
245+
Append("/** ").Append(javadoc->brief()).Append(" */").EndLine();
246+
}
247+
WriteModifiers(modifiers);
248+
if (!initializer.empty())
249+
AppendType(field.type()).Append(" ").Append(field.name()).
250+
Append(" = ").Append(initializer).Append(";");
251+
else
252+
AppendType(field.type()).Append(" ").Append(field.name()).Append(";");
253+
EndLine();
254+
return *this;
255+
}
256+
241257
SourceWriter& SourceWriter::WriteModifiers(int modifiers) {
242258
if (modifiers & PUBLIC) {
243259
Append("public ");

tensorflow-core/tensorflow-core-api/src/bazel/op_generator/source_writer.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ class SourceWriter {
153153
SourceWriter& WriteField(const Variable& field, int modifiers,
154154
const Javadoc* javadoc = nullptr);
155155

156+
SourceWriter& WriteFieldWithInitializer(const Variable& field,
157+
int modifiers, const Javadoc* javadoc = nullptr, const string& initializer = nullptr);
158+
156159
protected:
157160
virtual void DoAppend(const StringPiece& str) = 0;
158161

@@ -161,7 +164,7 @@ class SourceWriter {
161164
class TypeVisitor {
162165
public:
163166
virtual ~TypeVisitor() = default;
164-
void Visit(const Type& type);
167+
void Visit(const Type& type);
165168

166169
protected:
167170
virtual void DoVisit(const Type& type) = 0;

tensorflow-core/tensorflow-core-api/src/gen/annotations/org/tensorflow/op/NnOps.java

+145-30
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import org.tensorflow.op.nn.Relu;
8484
import org.tensorflow.op.nn.Relu6;
8585
import org.tensorflow.op.nn.Selu;
86+
import org.tensorflow.op.nn.SigmoidCrossEntropyWithLogits;
8687
import org.tensorflow.op.nn.Softmax;
8788
import org.tensorflow.op.nn.SoftmaxCrossEntropyWithLogits;
8889
import org.tensorflow.op.nn.Softsign;
@@ -102,10 +103,13 @@
102103
* @see {@link Ops}
103104
*/
104105
public final class NnOps {
106+
public final NnRawOps raw;
107+
105108
private final Scope scope;
106109

107110
NnOps(Scope scope) {
108111
this.scope = scope;
112+
raw = new NnRawOps(scope);
109113
}
110114

111115
/**
@@ -1753,6 +1757,56 @@ public <T extends TNumber> Selu<T> selu(Operand<T> features) {
17531757
return Selu.create(scope, features);
17541758
}
17551759

1760+
/**
1761+
* Computes sigmoid cross entropy given <code>logits</code>.
1762+
*
1763+
* <p>Measures the probability error in discrete classification tasks in which each class is
1764+
* independent and not mutually exclusive. For instance, one could perform multilabel
1765+
* classification where a picture can contain both an elephant and a dog at the same time.
1766+
*
1767+
* <p>For brevity, let <code>x = logits</code>, <code>z = labels</code>. The logistic loss in
1768+
* pseudo-code is
1769+
*
1770+
* <pre>
1771+
* z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
1772+
* = z * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))
1773+
* = z * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))
1774+
* = z * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))
1775+
* = (1 - z) * x + log(1 + exp(-x))
1776+
* = x - x * z + log(1 + exp(-x))
1777+
* </pre>
1778+
*
1779+
* <p>For <code>x < 0</code>, to avoid overflow in <code>exp(-x)</code>, we reformulate the above
1780+
*
1781+
* <pre>
1782+
* x - x * z + log(1 + exp(-x))
1783+
* = log(exp(x)) - x * z + log(1 + exp(-x))
1784+
* = - x * z + log(1 + exp(x))
1785+
* </pre>
1786+
*
1787+
* <p>Hence, to ensure stability and avoid overflow, the implementation uses this equivalent
1788+
* formulation
1789+
*
1790+
* <pre>
1791+
* max(x, 0) - x * z + log(1 + exp(-abs(x)))
1792+
* </pre>
1793+
*
1794+
* <p></ode>logits</code> and <code>labels</code> must have the same type and shape.
1795+
*
1796+
* <p>
1797+
*
1798+
* @param scope The TensorFlow scope
1799+
* @param labels the labels
1800+
* @param logits the logits of type float32 or float64
1801+
* @param <T> the type of labels and logits
1802+
* @return the component-wise logistic losses.
1803+
* @throws IllegalArgumentException if logits' and labels' do not have the same shape
1804+
*/
1805+
public <T extends TNumber> Operand<T> sigmoidCrossEntropyWithLogits(Operand<T> labels,
1806+
Operand<T> logits) {
1807+
return SigmoidCrossEntropyWithLogits.sigmoidCrossEntropyWithLogits(scope, labels, logits);
1808+
}
1809+
17561810
/**
17571811
* Computes softmax activations.
17581812
* <p>
@@ -1769,20 +1823,54 @@ public <T extends TNumber> Softmax<T> softmax(Operand<T> logits) {
17691823
}
17701824

17711825
/**
1772-
* Computes softmax cross entropy cost and gradients to backpropagate.
1773-
* <p>
1774-
* Inputs are the logits, not probabilities.
1826+
* Computes softmax cross entropy between <code>logits</code> and <code>labels</code>.
17751827
*
1776-
* @param <T> data type for {@code loss()} output
1777-
* @param features batch_size x num_classes matrix
1778-
* @param labels batch_size x num_classes matrix
1779-
* The caller must ensure that each batch of labels represents a valid
1780-
* probability distribution.
1781-
* @return a new instance of SoftmaxCrossEntropyWithLogits
1828+
* <p>Measures the probability error in discrete classification tasks in which the classes are
1829+
* mutually exclusive (each entry is in exactly one class). For example, each CIFAR-10 image is
1830+
* labeled with one and only one label: an image can be a dog or a truck, but not both.
1831+
*
1832+
* <p><b>NOTE:</b>
1833+
*
1834+
* <p>While the classes are mutually exclusive, their probabilities need not be. All that is
1835+
* required is that each row of <code>labels</code> is a valid probability distribution. If they
1836+
* are not, the computation of the gradient will be incorrect.
1837+
*
1838+
* <p>If using exclusive <code>labels</code> (wherein one and only one class is true at a time),
1839+
* see {@link org.tensorflow.op.NnOps#sparseSoftmaxCrossEntropyWithLogits}
1840+
*
1841+
* <p>Usage:
1842+
*
1843+
* <pre>
1844+
* Operand&lt;TFloat32&gt; logits =
1845+
* tf.constant(new float[][] {{4.0F, 2.0F, 1.0F}, {0.0F, 5.0F, 1.0F}} );
1846+
* Operand&lt;TFloat32&gt; labels =
1847+
* tf.constant(new float[][] {{1.0F, 0.0F, 0.0F}, {0.0F, 0.8F, 0.2F}} );
1848+
* Operand&lt;TFloat32&gt; output =
1849+
* tf.nn.softmaxCrossEntropyWithLogits(labels, logits, -1);
1850+
* // output Shape = [2]
1851+
* // dataType = FLOAT (1)
1852+
* // values { 0.169846, 0.824745 }
1853+
* </pre>
1854+
*
1855+
* <p>Backpropagation will happen into both <code>logits</code> and <code>labels</code>. To
1856+
* disallow backpropagation into <code>labels</code>, pass label tensors through <code>
1857+
* tf.stopGradient</code> before feeding it to this function.
1858+
*
1859+
* @param scope current scope
1860+
* @param labels Each vector along the class dimension should hold a valid probability
1861+
* distribution e.g. for the case in which labels are of shape <code>[batch_size, num_classes]
1862+
* </code>, each row of <code>labels[i]</code> must be a valid probability distribution.
1863+
* @param logits Per-label activations, typically a linear output. These activation energies are
1864+
* interpreted as unnormalized log probabilities.
1865+
* @param axis The class dimension. -1 is the last dimension.
1866+
* @param <T> the number type of the operands
1867+
* @return the softmax cross entropy loss. Its type is the same as <code>logits</code> and its
1868+
* shape is the same as <code>labels</code> except that it does not have the last dimension of
1869+
* <code>labels</code>.
17821870
*/
1783-
public <T extends TNumber> SoftmaxCrossEntropyWithLogits<T> softmaxCrossEntropyWithLogits(
1784-
Operand<T> features, Operand<T> labels) {
1785-
return SoftmaxCrossEntropyWithLogits.create(scope, features, labels);
1871+
public <T extends TNumber, U extends TNumber> Operand<T> softmaxCrossEntropyWithLogits(
1872+
Operand<U> labels, Operand<T> logits, int axis) {
1873+
return SoftmaxCrossEntropyWithLogits.softmaxCrossEntropyWithLogits(scope, labels, logits, axis);
17861874
}
17871875

17881876
/**
@@ -1974,24 +2062,51 @@ public <T extends TType> SpaceToDepth<T> spaceToDepth(Operand<T> input, Long blo
19742062
}
19752063

19762064
/**
1977-
* Computes softmax cross entropy cost and gradients to backpropagate.
1978-
* <p>
1979-
* Unlike `SoftmaxCrossEntropyWithLogits`, this operation does not accept
1980-
* a matrix of label probabilities, but rather a single label per row
1981-
* of features. This label is considered to have probability 1.0 for the
1982-
* given row.
1983-
* <p>
1984-
* Inputs are the logits, not probabilities.
1985-
*
1986-
* @param <T> data type for {@code loss()} output
1987-
* @param features batch_size x num_classes matrix
1988-
* @param labels batch_size vector with values in [0, num_classes).
1989-
* This is the label for the given minibatch entry.
1990-
* @return a new instance of SparseSoftmaxCrossEntropyWithLogits
1991-
*/
1992-
public <T extends TNumber, U extends TNumber> SparseSoftmaxCrossEntropyWithLogits<T> sparseSoftmaxCrossEntropyWithLogits(
1993-
Operand<T> features, Operand<U> labels) {
1994-
return SparseSoftmaxCrossEntropyWithLogits.create(scope, features, labels);
2065+
* Computes sparse softmax cross entropy between <code>logits</code> and <code>labels</code>.
2066+
*
2067+
* <p>Measures the probability error in discrete classification tasks in which the classes are
2068+
* mutually exclusive (each entry is in exactly one class). For example, each CIFAR-10 image is
2069+
* labeled with one and only one label: an image can be a dog or a truck, but not both.
2070+
*
2071+
* <p><b>NOTE:</b>
2072+
*
2073+
* <p>For this operation, the probability of a given label is considered exclusive. That is, soft
2074+
* classes are not allowed, and the <code>labels</code> vector must provide a single specific
2075+
* index for the true class for each row of <code>logits</code> (each minibatch entry). For soft
2076+
* softmax classification with a probability distribution for each entry, {@link
2077+
* org.tensorflow.op.NnOps#softmaxCrossEntropyWithLogits}.
2078+
*
2079+
* <p><b>WARNING:</b>
2080+
*
2081+
* <p>This op expects unscaled logits, since it performs a <code>softmax</code> on <code>logits
2082+
* </code> internally for efficiency. Do not call this op with the output of <code>softmax</code>,
2083+
* as it will produce incorrect results.
2084+
*
2085+
* <p>A common use case is to have logits of shape <code>[batchSize, numClasses]</code> and have
2086+
* labels of shape <code>[batchSize]</code>, but higher dimensions are supported, in which case
2087+
* the <code>dim</code>-th dimension is assumed to be of size <code>numClasses</code>. <code>
2088+
* logits</code> must have the <cod>dataType</cod> of <code>TFloat16</code>, <code>TFloat32</code>
2089+
* , or <code>TFloat64</code>, and <code>labels</code> must have the dtype of <code>TInt32</code>
2090+
* or <code>TInt64</code>.
2091+
*
2092+
* @param scope current scope
2093+
* @param labels <code>Tensor</code> of shape <code>[d_0, d_1, ..., d_{r-1}]</code> (where <code>r
2094+
* </code> is rank of <code>labels</code> and result) and the dataType is <code>TInt32</code>
2095+
* or <code>TInt64</code>. Each entry in <code>labels</code> must be an index in <code>[0,
2096+
* numClasses)</code>. Other values will raise an exception when this op is run on CPU, and
2097+
* return <code>NaN</code> for corresponding loss and gradient rows on GPU.
2098+
* @param logits Per-label activations (typically a linear output) of shape <code>[d_0, d_1, ...,
2099+
* d_{r-1}, numClasses]</code> and dataType of <code>TFloat16</code>, <code>TFloat32</code>,
2100+
* or <code>TFloat64</code>. These activation energies are interpreted as unnormalized log
2101+
* probabilities.
2102+
* @return A <code>Tensor</code> of the same shape as <code>labels</code> and of the same type as
2103+
* <code>logits</code> with the softmax cross entropy loss.
2104+
* @throws IllegalArgumentException If logits are scalars (need to have rank >= 1) or if the rank
2105+
* of the labels is not equal to the rank of the logits minus one.
2106+
*/
2107+
public <T extends TNumber, U extends TNumber> Operand sparseSoftmaxCrossEntropyWithLogits(
2108+
Operand<T> labels, Operand<U> logits) {
2109+
return SparseSoftmaxCrossEntropyWithLogits.sparseSoftmaxCrossEntropyWithLogits(scope, labels, logits);
19952110
}
19962111

19972112
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
// ==============================================================================
15+
//
16+
// This class has been generated, DO NOT EDIT!
17+
//
18+
package org.tensorflow.op;
19+
20+
import org.tensorflow.Operand;
21+
import org.tensorflow.op.nn.raw.SoftmaxCrossEntropyWithLogits;
22+
import org.tensorflow.op.nn.raw.SparseSoftmaxCrossEntropyWithLogits;
23+
import org.tensorflow.types.family.TNumber;
24+
25+
/**
26+
* An API for building {@code nn.raw} operations as {@link Op Op}s
27+
*
28+
* @see {@link Ops}
29+
*/
30+
public final class NnRawOps {
31+
private final Scope scope;
32+
33+
NnRawOps(Scope scope) {
34+
this.scope = scope;
35+
}
36+
37+
/**
38+
* Computes softmax cross entropy cost and gradients to backpropagate.
39+
* <p>
40+
* Inputs are the logits, not probabilities.
41+
*
42+
* @param <T> data type for {@code loss()} output
43+
* @param features batch_size x num_classes matrix
44+
* @param labels batch_size x num_classes matrix
45+
* The caller must ensure that each batch of labels represents a valid
46+
* probability distribution.
47+
* @return a new instance of SoftmaxCrossEntropyWithLogits
48+
*/
49+
public <T extends TNumber> SoftmaxCrossEntropyWithLogits<T> softmaxCrossEntropyWithLogits(
50+
Operand<T> features, Operand<T> labels) {
51+
return SoftmaxCrossEntropyWithLogits.create(scope, features, labels);
52+
}
53+
54+
/**
55+
* Computes softmax cross entropy cost and gradients to backpropagate.
56+
* <p>
57+
* Unlike `SoftmaxCrossEntropyWithLogits`, this operation does not accept
58+
* a matrix of label probabilities, but rather a single label per row
59+
* of features. This label is considered to have probability 1.0 for the
60+
* given row.
61+
* <p>
62+
* Inputs are the logits, not probabilities.
63+
*
64+
* @param <T> data type for {@code loss()} output
65+
* @param features batch_size x num_classes matrix
66+
* @param labels batch_size vector with values in [0, num_classes).
67+
* This is the label for the given minibatch entry.
68+
* @return a new instance of SparseSoftmaxCrossEntropyWithLogits
69+
*/
70+
public <T extends TNumber, U extends TNumber> SparseSoftmaxCrossEntropyWithLogits<T> sparseSoftmaxCrossEntropyWithLogits(
71+
Operand<T> features, Operand<U> labels) {
72+
return SparseSoftmaxCrossEntropyWithLogits.create(scope, features, labels);
73+
}
74+
}

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/audio/AudioSpectrogram.java

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ public Output<TFloat32> asOutput() {
127127
return spectrogram;
128128
}
129129

130+
/** The name of this op, as known by TensorFlow core engine */
131+
public static final String OP_NAME = "AudioSpectrogram";
132+
130133
private Output<TFloat32> spectrogram;
131134

132135
private AudioSpectrogram(Operation operation) {

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/audio/DecodeWav.java

+3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public Output<TInt32> sampleRate() {
132132
return sampleRate;
133133
}
134134

135+
/** The name of this op, as known by TensorFlow core engine */
136+
public static final String OP_NAME = "DecodeWav";
137+
135138
private Output<TFloat32> audio;
136139
private Output<TInt32> sampleRate;
137140

0 commit comments

Comments
 (0)