Welcome to my GitHub

Here is a classification and summary of all original (including supporting source code): 161789d922e14f https://github.com/zq2599/blog_demos

Overview of this article

  • This article is the sixth article in the "DL4J Actual Combat" series. Let's continue to consolidate our basic skills. This time we learned how to show the training process more vividly and completely: the graphical page, the effect is as shown in the figure below:

在这里插入图片描述

Dependent library configuration

  • The first is to add a dependent library, which requires a total of two steps:
  • Open the pom.xml of the parent project <font color="blue">dlfj-tutorials</font>. This is the place to manage the version number of the dependent library. In the <font color="blue">dependencies</font> node, ensure the following The child node exists, where the value of dl4j-master.version is <font color="blue">1.0.0-beta7</font>:
<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-ui</artifactId>
    <version>${dl4j-master.version}</version>
</dependency>
  • Open the pom.xml of the subproject <font color="blue">simple-convolution</font> and add the dependency of the deeplearning4j-ui library:
<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-ui</artifactId>
</dependency>
  • If you are using IDEA, it is recommended to perform the operations in the following figure to ensure that the configuration takes effect immediately:

在这里插入图片描述

Add graphical related code

  • There are very few graphics-related codes, only a few sentences, written in the position after the MultiLayerNetwork instance is initialized, as shown in the red box in the following figure:

在这里插入图片描述

  • The code in the red box above is here for your convenience:
// 初始化用户界面后端
UIServer uiServer = UIServer.getInstance();

// 配置要存储网络信息(梯度、分数、时间等)的位置。这里:存储在内存中。
StatsStorage statsStorage = new InMemoryStatsStorage();

// 将StatsStorage实例附加到UI:这允许StatsStorage的内容可视化
uiServer.attach(statsStorage);

//然后添加StatsListener来收集网络上的信息
net.setListeners(new StatsListener(statsStorage));
  • Finally, in order to avoid the premature end of the process and the inability to continue to view the graphical information, move a little at the end of the main method, such as waiting for input or delay, I added a delay here:
Thread.sleep(Integer.MAX_VALUE);
  • At this point, the encoding is complete, start running, enter <font color="blue"> http://localhost:9000 </font> in the browser, and the effect is as follows:

在这里插入图片描述

  • You can also change the page language to Chinese, as shown below:

在这里插入图片描述

  • Click the menu in the red box in the figure below, and you can also see the details of each layer of the neural network:

在这里插入图片描述

  • The basic functions have been completed, let's look at the persistence problem

Persistent storage of the content displayed on the page

  • The previous graphical function has an obvious problem: once the process is over, the web service is over, and the next time it is started, the latest training data is displayed on the page. If you want to save the content displayed on the page for later playback , How should it be done? Next, let’s work together and change it to something that can be stored persistently.
  • Find this line of code:
StatsStorage statsStorage = new InMemoryStatsStorage();
  • Delete the above line of code and replace it with the following line. The input parameter of the File object is the data storage location. Please modify it according to the actual situation of your computer:
StatsStorage statsStorage = new FileStatsStorage(new File("E:\\temp\\202107\\11", "ui-stats.dl4j"));
  • Delete the following line of code, so that the process will end immediately after the training is completed:
Thread.sleep(Integer.MAX_VALUE);
  • Now run the code to perform a training. After completion, in the <font color="blue">E:\temp\202107\11</font> directory, it is found that the name <font color="red">ui-stats.dl4j is generated </font>'s files:

在这里插入图片描述

  • The picture above looks like the data has been saved to the hard disk. Next, try to see if it can be read again. Add a <font color="red">Test to the <font color="blue">simple-convolution</font> project. .java</font>, the code is as follows, a few lines, all of the previous things:
package com.bolingcavalry.convolution;

import org.deeplearning4j.api.storage.StatsStorage;
import org.deeplearning4j.ui.api.UIServer;
import org.deeplearning4j.ui.storage.FileStatsStorage;
import java.io.File;

public class Test {
    public static void main(String[] args) throws Exception {
        UIServer uiServer = UIServer.getInstance();
        StatsStorage statsStorage = new FileStatsStorage(new File("E:\\temp\\202107\\11", "ui-stats.dl4j"));
        // 将StatsStorage实例附加到UI:这允许StatsStorage的内容可视化
        uiServer.attach(statsStorage);
        Thread.sleep(99999);
    }
}

在这里插入图片描述

  • Smart, you read the above Test.java code, you will definitely cast a contemptuous look at Xin Chen, at this level? Please allow me to explain. This is just a demonstration of how to load and display data, so it is too simple to write. You can put this code snippet in a common web application, such as SpringBoot, so that it can be loaded and viewed as needed
  • The storage problem is solved, let’s take a look at how to adjust the web port. Because of port conflicts, or some port management, there is a need to adjust the port, and it cannot be fixed at <font color="blue">9000</font>

About the web port of the graph refinement service

在这里插入图片描述

  • Listen to the official, configure the startup parameters, the operation is as follows:

在这里插入图片描述

  • Make sure that the menu in the red box 2 below is selected:

在这里插入图片描述

  • Fill in the official recommendation in the red box position (VM options position) in the figure below, and specify the port as 9001:

在这里插入图片描述

  • Fearing that there is a problem with the parameters I set, I added the two lines of code in the red box in the figure below to print the parameters:

在这里插入图片描述

  • Run Test.java again, as shown in the figure below. The red box 2 proves that the parameters I entered according to the official recommendation is no problem, but the red box 1 shows that the service is still listening on port 9000:

在这里插入图片描述

  • Go to the browser to try and find that it is true: the service of port 9000 is normal, port 9001 cannot be accessed
  • This is depressing, is there a problem with my operation? If there is a problem, the output of <font color="blue">System.getProperty("org.deeplearning4j.ui.port")</font> is <font color="red"> 9001 </font> explain?
  • Let's look at the source code. It's easy to find the code for setting the port value. In the <font color="blue">VertxUIServer</font> class, as shown in the red box in the figure below, it's really white! The code to get the port is <font color="red"> System.getenv </font>, this is the method to get the environment variable, it cannot be set in the previous way:

在这里插入图片描述

  • Since the source code uses the <font color="blue">System.getenv</font> method to get the parameters, let’s enter it according to the environment variable method, as shown in the figure below, delete the previous configuration, and then Enter the parameter at the 2 position of the red box. Note that the first part of the parameter does not need to be <font color="red"> -D </font>:

在这里插入图片描述

  • After the correction, start to run Test.java, as shown in the figure below, the red box 1 shows that the web service monitoring port has become 9001, and the value in the red box 2 is null, which is also normal, because the initial system property parameter has been deleted
  • Browser access <font color="red"> http://localhost:9001 </font> Try it, you can access normally:

在这里插入图片描述

  • So, finally by modifying the environment variables, we successfully modified the web port. At this moment, think back to the official system property and <font color="blue">-Dorg.deeplearning4j.ui.port=9001</font> What is going on, it turns out that this change is useless...
  • I think it should be because Xin Chen is talented and ignorant of learning and does not understand the truth. If you are smart, if you find the cause of the problem, please give pointers to the commenter of the article. Thank you.
  • At this point, the actual combat of the graphical display of the training process is complete. I hope this article can provide you with some references to help you successfully build a graphical environment to better observe and adjust training parameters and optimize the network.

You are not lonely, Xinchen original is with you all the way

  1. Java series
  2. Spring series
  3. Docker series
  4. kubernetes series
  5. database + middleware series
  6. DevOps series

Welcome to pay attention to the public account: programmer Xin Chen

Search "Programmer Xin Chen" on WeChat, I am Xin Chen, and I look forward to traveling the Java world with you...
https://github.com/zq2599/blog_demos

程序员欣宸
147 声望24 粉丝

热爱Java和Docker