Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is TensorFlow?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is TensorFlow". In daily operation, I believe many people have doubts about what is TensorFlow. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is TensorFlow?" Next, please follow the editor to study!

Brief introduction

Machine learning is becoming more and more popular and used around the world. It has revolutionized the way some applications are built and is likely to continue to be a huge (and growing) part of our daily lives. There is no packaging and machine learning is not easy. It seems very complex and often daunting to many people. Companies like Google linked their machine learning concepts to developers, and with the help of Google, they gradually took the first step, so the TensorFlow framework was born.

What is TensorFlow?

TensorFlow is an open source machine learning framework developed by Google using Python and C++. It can help developers easily access data, prepare and train models, predict future states, and perform large-scale machine learning. With it, we can train and run the contents of deep neural networks, such as optical character recognition, image recognition / classification, natural language processing and so on.

Tensor and operation

TensorFlow is based on a computational graph, which you can think of as a classic graph with nodes and edges. Each node is called an operation, and they input zero or more tensors and produce zero or more tensor outputs. Operations can be very simple, such as basic additions, but they can also be very complex. The tensor is depicted as the edge of the graph and is the core data unit. When we provide them to the operation, we perform different functions on these tensors. They can have single or multiple dimensions, sometimes called their levels (scalar: level 0, vector: level 1, matrix: level 2). Affected by the operation, these data are transferred to the calculation graph by tensor, so they are called TensorFlow. Tensors can store data in any number of dimensions and there are three main types of tensors: placeholders, variables, and constants.

Install TensorFlow

With Maven, installing TensorFlow is as simple as including dependencies:

Org.tensorflow tensorflow 1.13.1

If your device supports GPU, you can add the following dependencies:

Org.tensorflow libtensorflow 1.13.1 org.tensorflow libtensorflow_jni_gpu 1.13.1

You can use the TensorFlow object to check the version of the TensorFlow you are currently operating on.

System.out.println (TensorFlow.version ()); JavaAPI of TensorFlow

The Java API TensorFlow offer is included in the org.tensorflow package. It is currently experimental, so its stability cannot be guaranteed. It is important to note that the only language fully supported by TensorFlow is that Python,Java API has very little functionality. API introduced us to new classes, interfaces, enumerations and exceptions.

Class

The new classes introduced through API are:

Graph: data flow diagram representing TensorFlow calculations

Operation: the Graph node that performs the calculation on the Tensors

The builder class of OperationBuilder:Operations

Output: symbolic handle to the tensor generated by the operation

SavedModelBundle: represents a model loaded from storage

SavedModelBundle.Loader: provides the option to load SavedModel

Server: in-process TensorFlow server for distributed training

Session: the driver for graphics execution

Session.Run: outputs the tensor and metadata obtained during the execution of the session

Session.Runner: run operations and evaluate tensors

Shape: the shape of the tensor that may be partially known by the operation

Tensor: a multidimensional array of static types whose elements are types described by T

TensorFlow: a static utility method that describes the TensorFlow runtime

Tensors: a type-safe factory method for creating tensor objects

Enumerate

DataType: represents element types in tensors as enumerations

Interface

Operand: interface implemented by operands operated by TensorFlow

Abnormal

TensorFlowException: an unchecked exception thrown when executing a TensorFlow diagram

If we compare all of this with the tf module in Python, we will find that there is a significant difference. Java API doesn't have almost the same functionality, at least for now.

Figure (Graphs)

As mentioned earlier, TensorFlow is based on computational diagrams-where org.tensorflow.Graph is the implementation of Java. Note: its instance is thread-safe, although we need to explicitly release the resources used by Graph after completing it.

Let's start with an empty picture:

Graph graph = new Graph ()

The object is empty, so the diagram doesn't make much sense. To do anything with it, we first need to load it using Operations. We use the opBuilder () method to load it, which returns an OperationBuilder object, and once we call the .build () method, it adds the operation to our graph.

Constant

Let's add a constant to the chart:

Operation x = graph.opBuilder ("Const", "x") .setAttr ("dtype", DataType.FLOAT) .setAttr ("value", Tensor.create (3.0f)) .build (); placeholder

Placeholders are the "types" of variables that are not assigned when declared, and their values are assigned later. This allows us to build graphics using operations that do not have any actual data:

Operation y = graph.opBuilder ("Placeholder", "y") .setAttr ("dtype", DataType.FLOAT) .build (); function

Finally, to solve this problem, we need to add some functions. These can be as simple as multiplication, division or addition, or as complex as matrix multiplication. As before, we use the .opBuilder () method to define the function:

Operation xy = graph.opBuilder ("Mul", "xy") .addInput (x.output (0)) .addInput (y.output (0)) .build ()

Note: we can have multiple outputs using input (0) as the tensor.

Graphic visualization

Unfortunately, Java API doesn't include any tools that allow you to visualize graphics like you did in Python.

Session (Sessions)

As mentioned earlier, Session is the driver for Graph. It encapsulates the environment that performs Operation and Graph computing tensors (tensors). This means that the tensors in the graph (graph) we built doesn't actually have any value, because we don't run the graph (graph) in the session. We first add the diagram to the session (session):

Session session = new Session (graph)

Our operational knowledge simply multiplies x by y. In order to run our graph (graph) and get the results, we need to use fetch () to get the operation of xy and provide it with the values of x and y:

Tensor tensor = session.runner (). Fetch ("xy"). Feed ("x", Tensor.create (5.0f)). Feed ("y", Tensor.create (2.0f)). Run (). Get (0); System.out.println (tensor.floatValue ())

The result of running this code is as follows:

Load Saving module in Python in 10.0fJava

This may sound strange, but because Python is the only language that is well supported, Java API still doesn't have the ability to save the model. This means that Java API is used only for service use cases, at least until TensorFlow is fully supported. For now, at least we can use the SavedModelBundle class to train and save models in Python, and then use Java to load them to serve them:

SavedModelBundle model = SavedModelBundle.load (". / model", "serve"); Tensor tensor = model.session (). Runner (). Fetch ("xy"). Feed ("x", Tensor.create (5.0f)). Feed ("y", Tensor.create (2.0f)). Run (). Get (0); System.out.println (tensor.floatValue ()); at this point, the study of "what is TensorFlow" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report