Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* Copyright 2019 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;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* An environment for executing TensorFlow operations eagerly.
*
*
Eager execution is an imperative programming environment that evaluates operations
* immediately, without building graphs. Operations return concrete values instead of constructing a
* computational graph to run later, as with {@link Graph}s and {@link Session}s.
*
*
This makes it easy to develop with TensorFlow and debug models, as it behaves more like a
* standard programming library.
*
*
Instances of a {@code EagerSession} are thread-safe.
*/
public final class EagerSession implements ExecutionEnvironment, AutoCloseable {
/**
* Controls how to act when we try to run an operation on a given device but some input tensors
* are not on that device.
*/
public static enum DevicePlacementPolicy {
/** Running operations with input tensors on the wrong device will fail. */
EXPLICIT(0),
/** Copy the tensor to the right device but log a warning. */
WARN(1),
/**
* Silently copy the tensor, which has a performance cost since the operation will be blocked
* till the copy completes. This is the default placement policy.
*/
SILENT(2),
/** Placement policy which silently copies int32 tensors but not other dtypes. */
SILENT_FOR_INT32(3);
private DevicePlacementPolicy(int code) {
this.code = code;
}
private final int code;
}
/**
* Controls how TensorFlow resources are cleaned up when they are no longer needed.
*
*
All resources allocated during an {@code EagerSession} are deleted when the session is
* closed. To prevent out-of-memory errors, it is also strongly suggest to cleanup those resources
* during the session. For example, executing n operations in a loop of m iterations will allocate
* a minimum of n*m resources while in most cases, only resources of the last iteration are still
* being used.
*
*
{@code EagerSession} instances can be notified in different ways when TensorFlow objects are
* no longer being referred, so they can proceed to the cleanup of any resources they owned.
*/
public static enum ResourceCleanupStrategy {
/**
* Monitor and delete unused resources from a new thread running in background.
*
*
This is the most reliable approach to cleanup TensorFlow resources, at the cost of
* starting and running an additional thread dedicated to this task. Each {@code EagerSession}
* instance has its own thread, which is stopped only when the session is closed.
*
*
This strategy is used by default.
*/
IN_BACKGROUND,
/**
* Monitor and delete unused resources from existing threads, before or after they complete
* another task.
*
*
Unused resources are released when a call to the TensorFlow library reaches a safe point
* for cleanup. This is done synchronously and might block for a short period of time the thread
* who triggered that call.
*
*
This strategy should be used only if, for some reasons, no additional thread should be
* allocated for cleanup. Otherwise, {@link #IN_BACKGROUND} should be preferred.
*/
ON_SAFE_POINTS,
/**
* Only delete resources when the session is closed.
*
*
All resources allocated during the session will remained in memory until the session is
* explicitly closed (or via the traditional `try-with-resource` technique). No extra task for
* resource cleanup will be attempted.
*
*
This strategy can lead up to out-of-memory errors and its usage is not recommended, unless
* the scope of the session is limited to execute only a small amount of operations.
*/
ON_SESSION_CLOSE,
}
public static class Options {
/**
* Controls how operations dispatched are actually executed.
*
*
When set to true, each operation are executed asynchronously (in which case some
* operations might return "non-ready" outputs). When set to false, all operations are executed
* synchronously.
*
*
Synchronous execution is used by default.
*
* @param value true for asynchronous execution, false for synchronous.
*/
public Options async(boolean value) {
async = value;
return this;
}
/**
* Controls how to act when we try to run an operation on a given device but some input tensors
* are not on that device.
*
*
{@link DevicePlacementPolicy#SILENT} is used by default.
*
* @param value policy to apply
* @see DevicePlacementPolicy
*/
public Options devicePlacementPolicy(DevicePlacementPolicy value) {
devicePlacementPolicy = value;
return this;
}
/**
* Controls how TensorFlow resources are cleaned up when no longer needed.
*
*
{@link ResourceCleanupStrategy#IN_BACKGROUND} is used by default.
*
* @param value strategy to use
* @see ResourceCleanupStrategy
*/
public Options resourceCleanupStrategy(ResourceCleanupStrategy value) {
resourceCleanupStrategy = value;
return this;
}
/**
* Configures the session based on the data found in the provided buffer, which is serialized
* TensorFlow config proto.
*
*
Warning: the support of this feature is subject to changes since TensorFlow protos might
* not be supported on public endpoints in the future.
*
* @param value a serialized config proto
* @see
*/
public Options config(byte[] value) {
config = value;
return this;
}
/** Builds an eager session with the selected options. */
public EagerSession build() {
return new EagerSession(this, new ReferenceQueue