In order to use StructED to a specific task one should implement three interfaces which contains four functions:
\begin{equation} \label{eq:loss} \ell ((t_{s}, t_{e}),(\hat{t_{s}}, \hat{t_{e}})) = max{\{|(\hat{t_{s}}-\hat{t_{e}}) - (t_{s} - t_{e})| - \epsilon ,0\}} \end{equation}
In words, the loss will be the max between zero to the difference between the predicted signals length to its actual length and we minus epsilon. This means that we allow the classifier to be mistaken by at most epsilon.\begin{equation} \label{eq:decoding} \hat{y}_{w}(x) = argmax_{\mathcal{y} \in \mathcal{Y}} ~ w^\top \phi(x, y) \end{equation}
and,\begin{equation} \label{eq:augmented-loss} \hat{y}_{w}(x) = argmax_{\mathcal{y} \in \mathcal{Y}} ~ w^\top \phi(x, y) + \ell (y,\hat{y}) \end{equation}
For this we create new Java class inside the com.structed.models.inference package, this class should implement the IInference interface. The code for the implementation of this class is attached to this tutorial or can be found at the tutorial folder.Logger.info("Dummy data example."); int readerType = 0; int epochNum = 3; int isAvg = 1; int numExamples2Display = 3; // <the path to the train dummy data> String trainPath = "data/train.txt"; // <the path to the test dummy data> String testPath = "data/test.txt"; // load the data Reader reader = getReader(readerType); InstancesContainer dummyTrainInstances = reader.readData(trainPath, Consts.SPACE, Consts.COLON_SPLITTER); InstancesContainer dummyTestInstances = reader.readData(testPath, Consts.SPACE, Consts.COLON_SPLITTER); if ( dummyTrainInstances.getSize() == 0 ) return; // ======= PASSIVE AGGRESSIVE ====== // // init the first weight vector Vector W = new Vector() {{put(0, 0.0);}}; // model parameters ArrayList<Double> arguments = new ArrayList<Double>(){{add(3.0);}}; // task loss parameters ArrayList<Double> task_loss_params = new ArrayList<Double>(){{add(1.0);}}; // create the model StructEDModel dummy_model = new StructEDModel(W, new PassiveAggressive(), new TaskLossDummyData(), new InferenceDummyData(), null, new FeatureFunctionsDummy(), arguments); // train dummy_model.train(dummyTrainInstances, task_loss_params, null, epochNum, isAvg); // predict ArrayList<PredictedLabels> labels = dummy_model.predict(dummyTestInstances, task_loss_params, numExamples2Display); // print the prediction for(int i=0 ; i<dummyTestInstances.getSize() ; i++) Logger.info("Y = "+dummyTestInstances.getInstance(i).getLabel()+", Y_HAT = "+ labels.get(i).firstKey()); Logger.info("");
// ======= CRF ====== // // init the first weight vector W = new Vector() {{put(0, 0.0);}}; // model parameters arguments = new ArrayList<Double>(){{add(0.1);add(0.1);}}; // create the model StructEDModel dummy_model_crf = new StructEDModel(W, new CRF(), new TaskLossDummyData(), new InferenceDummyData(), null, new FeatureFunctionsDummy(), arguments); // train dummy_model_crf.train(dummyTrainInstances, task_loss_params, null, epochNum, isAvg); // predict labels = dummy_model_crf.predict(dummyTestInstances, task_loss_params, numExamples2Display); // print the prediction for(int i=0 ; i<dummyTestInstances.getSize() ; i++) Logger.info("Y = "+dummyTestInstances.getInstance(i).getLabel()+", Y_HAT = "+ labels.get(i).firstKey()); Logger.info("");