12æããJavaèªæ¸ä¼BOF*1ã§èªã¿å§ãããDeep Learning Javaããã°ã©ãã³ã°ãã§ã2.5.1ç¯ã®ãã¼ã»ãããã³ï¼å層ãã¥ã¼ã©ã«ãããã¯ã¼ã¯ï¼ã®ãµã³ãã«ã³ã¼ããJavaFXã§GUIã追å ãã¦ä½æãã¦ã¿ã¾ããã
Deep Learning Javaããã°ã©ãã³ã° 深層å¦ç¿ã®çè«ã¨å®è£ (impress top gear)
æ¬æ¸ç±ã®ãµã³ãã«ã³ã¼ãã¯åºç社ï¼ã¤ã³ãã¬ã¹ï¼ã®æ¸ç±ãã¼ã¸ãããã¦ã³ãã¼ãã§ãã¾ãã
http://book.impress.co.jp/books/1115101146
ãã ãã³ã³ã½ã¼ã«ã§å®è¡ãããµã³ãã«ã¨ãªã£ã¦ããã®ã§ããã¼ã¿ã®åå¸ãåããããJavaFXã§GUIãä»ãã¦ã¿ã¾ããã
å®è¡ããç»é¢ã¯æ¬¡ã«ãªãã¾ãã
ããã°ã©ãã³ã°ã¡ã¢
æ£å¸å³ï¼ScatterChart)
2次å
ã®ãã¼ã¿åå¸ãªã®ã§ãJavaFXã®æ£å¸å³ï¼ScatterChartï¼ã使ãã¾ããã
id:torutk:20170113 ã«Scene Builderä¸ã§ScatterChartãè²¼ã£ã¦ãã®è»¸ã®ããããã£ãè¨å®ããã¡ã¢ãæ¸ãã¦ãã¾ãã
ããã©ã«ãã®æ£å¸å³ã§ã¯ãã·ã³ãã«ï¼ãªã¬ã³ã¸è²ã®ä¸¸ããã®ï¼ã大ãããã¼ã¿æ°ãå¤ããªãã¨éãªã£ã¦åå¸ãè¦ãã«ãããªã£ã¦ãã¾ãã¾ãããããã§ãCSSãã¡ã¤ã«ãè¨ãã¦ã·ã³ãã«ã®å¤§ããã5pxãã2pxã«å¤æ´ãã¦ãã¾ãã
- perceptron.css
.chart-symbol { -fx-background-radius: 2px; -fx-padding: 2px; }
NetBeansä¸ã§CSSãã¡ã¤ã«ãä½æããScene Builderä¸ã§ç»é²ããæ¹æ³ã¯ãã¡ããã©ä»æå
¬éãããITProã®JavaFXé£è¼è¨äºï¼èè
ã¯æ«»åºããï¼ã«æ¸ããã¦ãã¾ãã
JavaFXで見た目を設定する、JavaFXにおけるCSSとは | 日経 xTECH(クロステック)
æ£å¸å³ã«è¡¨ç¤ºããããã¼ã¿ã¯ãåã ã®ãã¼ã¿ãXYChart.Dataã§è¡¨ç¾ãããã¼ã¿ã®éã¾ãï¼ç³»åï¼ãXYChart.Seriesã§è¡¨ç¾ãã¾ãããã¼ã¿ç³»åãã¨ã«ã·ã³ãã«å½¢ç¶ã¨è²ãå¤ããã¾ãã
ããã©ã«ãã§ã¯ãå¡ä¾ã表示ãããã®ã§ããã¼ã¿ç³»åã1ã¤ã®ã¨ãã¯éªéãªã®ã§Scene Builderä¸ããScatterChartã®ããããã£Legend Visibleãå¤ãã¾ããã
ããã©ã«ãã§ã¯ãNumberAxisã®ãããã㣠Auto Ranging ãæå¹ã¨ãªã£ã¦ãã¾ãããã¼ã¿ã®ç¯å²ã«åããã¦è¡¨ç¤ºããã¹ã±ã¼ã«ã調æ´ãã¦ãããã®ã§ãä»åã®ç¨éã§ã¯ä¾¿å©ã§ããéã«ãLower BoundãUpper Boundã«è¨å®ãã軸ã®è¡¨ç¤ºã®æå°ã»æ大å¤ã¯ãã¼ã¿ãã»ããããåï¼Auto Rangingãä½ç¨ããåï¼ã®è¡¨ç¤ºã«åæ ããã¾ãã
ViewModelï¼Presentation Modelï¼ã®è¨ç½®
NetBeans ã®JavaFX FXMLã¢ããªã±ã¼ã·ã§ã³ã§çæããããã¸ã§ã¯ãã§ã¯ãApplicationæ´¾çã¯ã©ã¹ï¼mainã¡ã½ãããæã¤ã¯ã©ã¹ï¼ãFXMLãã¡ã¤ã«ãFXMLããåç §ãããã³ã³ããã¼ã©ã¯ã©ã¹ã®3ã¤ã®ãã¡ã¤ã«ãçæããã¾ãã
å°ããªGUIã¢ããªã±ã¼ã·ã§ã³ã®å ´åãã¤ãã³ã³ããã¼ã©ã¯ã©ã¹ã«è¡¨ç¤ºãããã¼ã¿ãæããã¦ãã¾ãã¾ããããããã³ã³ããã¼ã©ã¯ã©ã¹ãã©ãã©ãè¥å¤§åãã¦ãã¾ãã¾ãããã¨ã§ãªãã¡ã¯ã¿ãªã³ã°ãããã¨ãã¦ããã³ã£ããççãã¦ãã¾ã£ããã®ãå¼ãå¥ããã®ã¯å¤§å¤ã§ãã
ããã§ãæåããã³ã³ããã¼ã©ã¨ã¯å¥ã«è¡¨ç¤ºãããã¼ã¿ãæããã¦ããã¯ã©ã¹ï¼ViewModelãããã¯PresentationModelï¼ãä½æãã¦ãããã³ã³ããã¼ã©ã¯ããã¾ã§ViewModelã¨FXMLã¨ã®æ¥åï¼ãã¤ã³ãã£ã³ã°ã¨ããªã¹ãã¼ç»é²ã¨ãã®ä½æ¥ï¼ã«å°å¿µãã¦ããã¾ãã
ä»åä½æããã³ã³ããã¼ã©ã¯ã©ã¹ã¯ããã¼ã¿ã®ä¿æãViewModelã«åé¢ããã®ã§æ¬¡ã®ããã«ã·ã³ãã«ãªã³ã¼ãã¨ãªã£ã¦ãã¾ãã
package perceptron; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.chart.ScatterChart; import javafx.scene.control.Label; public class PerceptronViewController implements Initializable { @FXML private ScatterChart chart; @FXML private Label accuracyLabel; @FXML private Label precisionLabel; @FXML private Label recallLabel; private PerceptronViewModel model = new PerceptronViewModel(); @FXML private void doLearning(ActionEvent event) { model.getTrainData().stream() .forEach(chart.getData()::add); model.train(); model.test(); model.evaluate(); } @Override public void initialize(URL url, ResourceBundle rb) { chart.setTitle("å¦ç¿ãã¼ã¿ã®æ£å¸å³"); accuracyLabel.textProperty().bind(model.accuracyProperty()); precisionLabel.textProperty().bind(model.precisionProperty()); recallLabel.textProperty().bind(model.recallProperty()); } }
æ£è¦åå¸ä¹±æ°ã®çæ
æ¸ç±ã®ãµã³ãã«ã³ã¼ãã§ã¯ãæ£è¦åå¸ã®ä¹±æ°ãçæãããã¸ãã¯ãç¬èªã«ã³ã¼ãã£ã³ã°ãã¦ãã¾ãããJava SEã®æ¨æºAPI java.util.Randomã¯ã©ã¹ã«ã¯nextGaussian ã¡ã½ããããããå¹³å0.0ãæ¨æºåå·®1.0ã®æ£è¦åå¸ä¹±æ°ãåå¾ã§ãã¾ãã
ä»åçæãããã¼ã¿ã¯ãå¹³åã-2.0ããã³2.0ã§æ¨æºåå·®1.0ãªã®ã§ãnextGaussianã®æ»ãå¤ã«å¹³åã®å¤§ããï¼-2.0ããã³2.0ï¼ãå ãããã®ã«ãªãã¾ãã
æ¸ç±ã®ãµã³ãã«ã³ã¼ã
GaussianDistribution g1 = new GaussianDistribution(-2.0, 1.0, rng); GaussianDistribution g2 = new GaussianDistribution(2.0, 1.0, rng); // data set in class 1 for (int i = 0; i < train_N/2 - 1; i++) { train_X[i][0] = g1.random(); train_X[i][1] = g2.random(); train_T[i] = 1; }
nextGaussianã使ã£ãçæï¼foræã®æ¿ããã«Stream使ç¨ï¼
IntStream.range(0, NUM_TRAIN / 2) .forEach(i -> { trainData[i][0] = random.nextGaussian() - 2.0; trainData[i][1] = random.nextGaussian() + 2.0; trainLabels[i] = 1; });
æ¬æ¥ã¯ãStreamã®çµç«¯ã«forEachã§ã¯ãªããtoArrayãç¨ãã¦é åã«ãããã£ãã®ã§ãããåç´ãª1次å é åã§ã¯ãªãã®ã§æãè¦ããããããã¾ããã
*1:次åã¯1æ21æ¥ï¼åï¼ã«éå¬ãã¾ããhttp://www.javareading.com/bof/