org.tensorflow.Graph类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(209)

本文整理了Java中org.tensorflow.Graph类的一些代码示例,展示了Graph类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Graph类的具体详情如下:
包路径:org.tensorflow.Graph
类名称:Graph

Graph介绍

[英]A data flow graph representing a TensorFlow computation.

Instances of a Graph are thread-safe.

WARNING: Resources consumed by the Graph object must be explicitly freed by invoking the #close() method then the Graph object is no longer needed.
[中]表示TensorFlow计算的数据流图。
图的实例是线程安全的。
警告:必须通过调用#close()方法显式释放Graph对象消耗的资源,然后不再需要Graph对象。

代码示例

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
  @Override public Session parseModel(byte[] mdl) {
    Graph graph = new Graph();
    graph.importGraphDef(mdl);

    return new Session(graph);
  }
}

代码示例来源:origin: org.tensorflow/libtensorflow

/**
 * Factory method to create a class to wrap a new NoOp operation to the graph.
 * 
 * @param scope current graph scope
 * @return a new instance of NoOp
 */
public static NoOp create(Scope scope) {
 OperationBuilder opBuilder = scope.graph().opBuilder("NoOp", scope.makeOpName("NoOp"));
 return new NoOp(opBuilder.build());
}

代码示例来源:origin: org.tensorflow/libtensorflow

/**
 * Create a SavedModelBundle object from a handle to the C TF_Graph object and to the C TF_Session
 * object, plus the serialized MetaGraphDef.
 *
 * <p>Invoked from the native load method. Takes ownership of the handles.
 */
private static SavedModelBundle fromHandle(
  long graphHandle, long sessionHandle, byte[] metaGraphDef) {
 Graph graph = new Graph(graphHandle);
 Session session = new Session(graph, sessionHandle);
 return new SavedModelBundle(graph, session, metaGraphDef);
}

代码示例来源:origin: biezhi/java-library-examples

public static void main(String[] args) throws Exception {
    try (Graph g = new Graph()) {
      final String value = "Hello from " + TensorFlow.version();

      // Construct the computation graph with a single operation, a constant
      // named "MyConst" with a value "value".
      try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
        // The Java API doesn't yet include convenience functions for adding operations.
        g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
      }

      // Execute the "MyConst" operation in a Session.
      try (Session s = new Session(g);
         Tensor output = s.runner().fetch("MyConst").run().get(0)) {
        System.out.println(new String(output.bytesValue(), "UTF-8"));
      }
    }
  }
}

代码示例来源:origin: org.tensorflow/libtensorflow

/**
 * Import a serialized representation of a TensorFlow graph.
 *
 * <p>The serialized representation of the graph, often referred to as a <i>GraphDef</i>, can be
 * generated by {@link #toGraphDef()} and equivalents in other language APIs.
 *
 * @throws IllegalArgumentException if graphDef is not a recognized serialization of a graph.
 * @see #importGraphDef(byte[], String)
 */
public void importGraphDef(byte[] graphDef) throws IllegalArgumentException {
 importGraphDef(graphDef, "");
}

代码示例来源:origin: org.springframework.cloud.stream.app/spring-cloud-starter-stream-common-tensorflow

@Override
public void close() throws Exception {
  logger.info("Close TensorFlow Graph!");
  if (graph != null) {
    graph.close();
  }
}

代码示例来源:origin: org.tensorflow/libtensorflow

int[] dxIndices = null;
try (Reference ref = ref()) {
 for (int i = 0; i < y.length; ++i) {
  yHandles[i] = y[i].op().getUnsafeNativeHandle();
   addGradients(
     ref.nativeHandle(),
     prefix,

代码示例来源:origin: org.tensorflow/libtensorflow

/**
 * 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>
 * 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);
}

代码示例来源:origin: org.bytedeco.javacpp-presets/tensorflow

/** Create an empty Graph. */
public Graph() {
 nativeHandle = allocate();
}

代码示例来源:origin: org.tensorflow/libtensorflow

/**
 * Release resources associated with the Graph.
 *
 * <p>Blocks until there are no active {@link Session} instances referring to this Graph. A Graph
 * is not usable after close returns.
 */
@Override
public void close() {
 synchronized (nativeHandleLock) {
  if (nativeHandle == 0) {
   return;
  }
  while (refcount > 0) {
   try {
    nativeHandleLock.wait();
   } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    // Possible leak of the graph in this case?
    return;
   }
  }
  delete(nativeHandle);
  nativeHandle = 0;
 }
}

代码示例来源:origin: org.bytedeco.javacpp-presets/tensorflow

/**
 * Factory method to create a class to wrap a new Timestamp operation to the graph.
 * 
 * @param scope current graph scope
 * @return a new instance of Timestamp
 */
public static Timestamp create(Scope scope) {
 OperationBuilder opBuilder = scope.graph().opBuilder("Timestamp", scope.makeOpName("Timestamp"));
 return new Timestamp(opBuilder.build());
}

代码示例来源:origin: org.bytedeco.javacpp-presets/tensorflow

/**
 * Create a SavedModelBundle object from a handle to the C TF_Graph object and to the C TF_Session
 * object, plus the serialized MetaGraphDef.
 *
 * <p>Invoked from the native load method. Takes ownership of the handles.
 */
private static SavedModelBundle fromHandle(
  long graphHandle, long sessionHandle, byte[] metaGraphDef) {
 Graph graph = new Graph(graphHandle);
 Session session = new Session(graph, sessionHandle);
 return new SavedModelBundle(graph, session, metaGraphDef);
}

代码示例来源:origin: biezhi/java-library-examples

public static void main(String[] args) {
    /**
     * 定义一个 graph 类,并在这张图上定义了 foo 与 bar 的两个变量
     */
    try (Graph g = new Graph()) {

      try (Tensor<Integer> t = Tensor.create(30, Integer.class)) {
        g.opBuilder("Const", "foo").setAttr("dtype", t.dataType()).setAttr("value", t).build();
      }

      try (Tensor<Integer> t = Tensor.create(20, Integer.class)) {
        g.opBuilder("Const", "bar").setAttr("dtype", t.dataType()).setAttr("value", t).build();
      }

      try (Session s = new Session(g);
         Tensor output1 = s.runner().fetch("foo").run().get(0);
         Tensor output2 = s.runner().fetch("bar").run().get(0)) {

        System.out.println(output1.intValue());
        System.out.println(output2.intValue());
      }
    }
  }
}

代码示例来源:origin: org.bytedeco.javacpp-presets/tensorflow

/**
 * Import a serialized representation of a TensorFlow graph.
 *
 * <p>The serialized representation of the graph, often referred to as a <i>GraphDef</i>, can be
 * generated by {@link #toGraphDef()} and equivalents in other language APIs.
 *
 * @throws IllegalArgumentException if graphDef is not a recognized serialization of a graph.
 * @see #importGraphDef(byte[], String)
 */
public void importGraphDef(byte[] graphDef) throws IllegalArgumentException {
 importGraphDef(graphDef, "");
}

代码示例来源:origin: org.springframework.cloud.stream.app/spring-cloud-starter-stream-processor-image-recognition

@Override
public void close() throws Exception {
  logger.info("Input Graph Destroyed");
  if (graph != null) {
    graph.close();
  }
}

代码示例来源:origin: org.bytedeco.javacpp-presets/tensorflow

int[] dxIndices = null;
try (Reference ref = ref()) {
 for (int i = 0; i < y.length; ++i) {
  yHandles[i] = y[i].op().getUnsafeNativeHandle();
   addGradients(
     ref.nativeHandle(),
     prefix,

代码示例来源:origin: org.bytedeco.javacpp-presets/tensorflow

/**
 * 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>
 * 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);
}

代码示例来源:origin: org.tensorflow/libtensorflow

/** Create an empty Graph. */
public Graph() {
 nativeHandle = allocate();
}

代码示例来源:origin: org.bytedeco.javacpp-presets/tensorflow

/**
 * Release resources associated with the Graph.
 *
 * <p>Blocks until there are no active {@link Session} instances referring to this Graph. A Graph
 * is not usable after close returns.
 */
@Override
public void close() {
 synchronized (nativeHandleLock) {
  if (nativeHandle == 0) {
   return;
  }
  while (refcount > 0) {
   try {
    nativeHandleLock.wait();
   } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    // Possible leak of the graph in this case?
    return;
   }
  }
  delete(nativeHandle);
  nativeHandle = 0;
 }
}

代码示例来源:origin: org.tensorflow/libtensorflow

/**
 * Factory method to create a class to wrap a new IsInf operation to the graph.
 * 
 * @param scope current graph scope
 * @param x 
 * @return a new instance of IsInf
 */
public static <T extends Number> IsInf create(Scope scope, Operand<T> x) {
 OperationBuilder opBuilder = scope.graph().opBuilder("IsInf", scope.makeOpName("IsInf"));
 opBuilder.addInput(x.asOutput());
 return new IsInf(opBuilder.build());
}

相关文章