📄 cnnclassificationmodel.java
字号:
/* * Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project * * Contact: cdk-devel@lists.sourceforge.net * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */package org.openscience.cdk.qsar.model.R;import org.openscience.cdk.qsar.model.QSARModelException;import java.util.HashMap;/** * A modeling class that provides a computational neural network classification model. * * When instantiated this class ensures that the R/Java interface has been * initialized. The response and independent variables can be specified at construction * time or via the <code>setParameters</code> method. * The actual fitting procedure is carried out by <code>build</code> after which * the model may be used to make predictions, via <code>predict</code>. An example of the use * of this class is shown below: * <pre> * double[][] x; * String[] y; * Double[] wts; * Double[][] newx; * ... * try { * CNNClassificationModel cnnrm = new CNNClassificationModel(x,y,3); * cnnrm.setParameters("Wts",wts); * cnnrm.build(); * * double fitValue = cnnrm.getFitValue(); * * cnnrm.setParameters("newdata", newx); * cnnrm.setParameters("type", "raw"); * cnnrm.predict(); * * double[][] preds = cnnrm.getPredictPredicted(); * } catch (QSARModelException qme) { * System.out.println(qme.toString()); * } * </pre> * The above code snippet builds a 3-3-1 CNN model. * Multiple output neurons are easily * specified by supplying a matrix for y (i.e., double[][]) with the output variables * in the columns. * <p> * Nearly all the arguments to * <a href="http://www.maths.lth.se/help/R/.R/library/nnet/html/nnet.html" target="_top">nnet()</a> are * supported via the <code>setParameters</code> method. The table below lists the names of the arguments, * the expected type of the argument and the default setting for the arguments supported by this wrapper class. * <center> * <table border=1 cellpadding=5> * <THEAD> * <tr> * <th>Name</th><th>Java Type</th><th>Default</th><th>Notes</th> * </tr> * </thead> * <tbody> * <tr><td>x</td><td>Double[][]</td><td>None</td><td>This must be set by the caller via the constructors or via <code>setParameters</code></td></tr> * <tr><td>y</td><td>String[][]</td><td>None</td><td>This must be set by the caller via the constructors or via <code>setParameters</code></td></tr> * <tr><td>weights</td><td>Double[]</td><td>rep(1,nobs)</td><td>The default case weights is a vector of 1's equal in length to the number of observations, nobs</td></tr> * <tr><td>size</td><td>Integer</td><td>None</td><td>This must be set by the caller via the constructors or via <code>setParameters</code></td></tr> * <tr><td>subset</td><td>Integer[]</td><td>1:nobs</td><td>This is supposed to be an index vector specifying which observations are to be used in building the model. The default indicates that all should be used</td></tr> * <tr><td>Wts</td><td>Double[]</td><td>runif(1,nwt)</td><td>The initial weight vector is set to a random vector of length equal to the number of weights if not set by the user</td></tr> * <tr><td>mask</td><td>Boolean[]</td><td>rep(TRUE,nwt)</td><td>All weights are to be optimized unless otherwise specified by the user</td></tr> * <tr><td>linout</td><td>Boolean</td><td>FALSE</td><td>Since this class performs classification this need not be changed</td></tr> * <tr><td>entropy</td><td>Boolean</td><td>TRUE</td><td></td></tr> * <tr><td>softmax</td><td>Boolean</td><td>FALSE</td><td></td></tr> * <tr><td>censored</td><td>Boolean</td><td>FALSE</td><td></td></tr> * <tr><td>skip</td><td>Boolean</td><td>FALSE</td><td></td></tr> * <tr><td>rang</td><td>Double</td><td>0.7</td><td></td></tr> * <tr><td>decay</td><td>Double</td><td>0.0</td><td></td></tr> * <tr><td>maxit</td><td>Integer</td><td>100</td><td></td></tr> * <tr><td>Hess</td><td>Boolean</td><td>FALSE</td><td></td></tr> * <tr><td>trace</td><td>Boolean</td><td>TRUE</td><td></td></tr> * <tr><td>MaxNWts</td><td>Integer</td><td>1000</td><td></td></tr> * <tr><td>abstol</td><td>Double</td><td>1.0e-4</td><td></td></tr> * <tr><td>reltol</td><td>Double</td><td>1.0e-8</td><td></td></tr> * </tbody> * </table> * </center> * <p> * In general the <code>getFit*</code> methods provide access to results from the fit * and <code>getPredict*</code> methods provide access to results from the prediction (i.e., * prediction using the model on new data). The values returned correspond to the various * values returned by the <a href="http://www.maths.lth.se/help/R/.R/library/nnet/html/nnet.html" target="_top">nnet</a> and * <a href="http://www.maths.lth.se/help/R/.R/library/nnet/html/predict.nnet.html" target="_top">predict.nnet</a> functions * in R * <p> * See {@link RModel} for details regarding the R and SJava environment. * * @author Rajarshi Guha * @cdk.require r-project * @cdk.module qsar * * @cdk.keyword neural network * @cdk.keyword classification * @deprecated */public class CNNClassificationModel extends RModel { static int globalID = 0; private int currentID; private CNNClassificationModelFit modelfit = null; private CNNClassificationModelPredict modelpredict = null; private HashMap params = null; private int noutput = 0; private int nvar = 0; private void setDefaults() { // lets set the default values of the arguments that are specified // to have default values in ?nnet // these params are vectors that depend on user defined stuff // so as a default we set them to FALSE so R can check if these // were not set this.params.put("subset", new Boolean(false)); this.params.put("mask", new Boolean(false) ); this.params.put("Wts", new Boolean(false)); this.params.put("weights", new Boolean(false)); this.params.put("linout", new Boolean(false)); // we want only classification this.params.put("entropy", new Boolean(true)); this.params.put("softmax",new Boolean(false)); this.params.put("censored", new Boolean(false)); this.params.put("skip", new Boolean(false)); this.params.put("rang", new Double(0.7)); this.params.put("decay", new Double(0.0)); this.params.put("maxit", new Integer(100)); this.params.put("Hess", new Boolean(false)); this.params.put("trace", new Boolean(false)); // no need to see output this.params.put("MaxNWts", new Integer(1000)); this.params.put("abstol", new Double(1.0e-4)); this.params.put("reltol", new Double(1.0e-8)); } /** * Constructs a CNNClassificationModel object. * * This constructor allows the user to simply set up the modeling class. It is * expected that parameters such as training data, architecture will be set at a * later point. The result of this constructor is to simply create a name for the * current instance of the modeling object. * <p> * Other parameters that are required to be set should be done via * calls to <code>setParameters</code>. A number of parameters are set to the * defaults as specified in the manpage for * <a href="http://www.maths.lth.se/help/R/.R/library/nnet/html/nnet.html" target="_top">nnet</a>. * */ public CNNClassificationModel() { super(); this.params = new HashMap(); this.currentID = CNNClassificationModel.globalID; CNNClassificationModel.globalID++; this.setModelName("cdkCNNCModel"+this.currentID); this.setDefaults(); } /** * Constructs a CNNClassificationModel object. * * This constructor allows the user to specify the dependent and * independent variables along with the number of hidden layer neurons. * This constructor is suitable for cases when there is a single output * neuron. If the number of rows of the design matrix is not equal to * the number of observations in y an exception will be thrown. * <p> * Other parameters that are required to be set should be done via * calls to <code>setParameters</code>. A number of parameters are set to the * defaults as specified in the manpage for * <a href="http://www.maths.lth.se/help/R/.R/library/nnet/html/nnet.html" target="_top">nnet</a>. * * @param x An array of independent variables. Observations should be in * the rows and variables in the columns. * @param y An array (single column) of observed class assignments * @param size The number of hidden layer neurons * @throws QSARModelException if the number of observations in x and y are not the same */ public CNNClassificationModel(double[][] x, String[] y, int size) throws QSARModelException { super(); this.params = new HashMap(); this.currentID = CNNClassificationModel.globalID; CNNClassificationModel.globalID++; this.setModelName("cdkCNNCModel"+this.currentID); int nrow = y.length; int ncol = x[0].length; if (nrow != x.length) { throw new QSARModelException("The number of values for the dependent variable does not match the number of rows of the design matrix"); } this.nvar = ncol; this.noutput = 1; Double[][] xx = new Double[nrow][ncol]; String[][] yy = new String[nrow][1]; for (int i = 0; i < nrow; i++) { yy[i][0] = new String(y[i]); for (int j = 0; j < ncol; j++) { xx[i][j] = new Double(x[i][j]); } } this.params.put("x", xx); this.params.put("y", yy); this.params.put("size", new Integer(size)); this.setDefaults(); } /** * Constructs a CNNClassificationModel object. * * This constructor allows the user to specify the dependent and * independent variables along with the number of hidden layer neurons. * This constructor is suitable for cases when there are multiple output * neuron. If the number of rows of the design matrix is not equal to * the number of observations in y an exception will be thrown. * <p> * Other parameters that are required to be set should be done via * calls to <code>setParameters</code>. A number of parameters are set to the * defaults as specified in the manpage for * <a href="http://www.maths.lth.se/help/R/.R/library/nnet/html/nnet.html" target="_top">nnet</a>. * * @param x An array of independent variables. Observations should be in * the rows and variables in the columns. * @param y An array (multiple columns) of observed values * @param size The number of hidden layer neurons * @throws QSARModelException if the number of observations in x and y are not the same */ public CNNClassificationModel(double[][] x, String[][] y, int size) throws QSARModelException{ super(); this.params = new HashMap(); this.currentID = CNNClassificationModel.globalID; CNNClassificationModel.globalID++; this.setModelName("cdkCNNCModel"+this.currentID); int nrow = y.length; int ncol = x[0].length; if (nrow != x.length) { throw new QSARModelException("The number of values for the dependent variable does not match the number of rows of the design matrix"); } this.nvar = ncol; this.noutput = y[0].length; Double[][] xx = new Double[nrow][ncol]; String[][] yy = new String[nrow][this.noutput]; for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { xx[i][j] = new Double(x[i][j]); } } for (int i = 0; i < nrow; i++) { for (int j = 0; j < this.noutput; j++) { yy[i][j] = new String(y[i][j]); } } this.params.put("x", xx); this.params.put("y", yy); this.params.put("size", new Integer(size)); this.setDefaults(); } /** * Sets parameters required for building a linear model or using one for prediction.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -