⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 milr.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *    This program is free software; you can redistribute it and/or modify *    it under the terms of the GNU General Public License as published by *    the Free Software Foundation; either version 2 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 General Public License for more details. * *    You should have received a copy of the GNU General Public License *    along with this program; if not, write to the Free Software *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * MILR.java * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand * */package weka.classifiers.mi;import weka.classifiers.Classifier;import weka.core.Capabilities;import weka.core.Instance;import weka.core.Instances;import weka.core.MultiInstanceCapabilitiesHandler;import weka.core.Optimization;import weka.core.Option;import weka.core.OptionHandler;import weka.core.SelectedTag;import weka.core.Tag;import weka.core.Utils;import weka.core.Capabilities.Capability;import java.util.Enumeration;import java.util.Vector;/** <!-- globalinfo-start --> * Uses either standard or collective multi-instance assumption, but within linear regression. For the collective assumption, it offers arithmetic or geometric mean for the posteriors. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> *  * <pre> -D *  Turn on debugging output.</pre> *  * <pre> -R &lt;ridge&gt; *  Set the ridge in the log-likelihood.</pre> *  * <pre> -A [0|1|2] *  Defines the type of algorithm: *   0. standard MI assumption *   1. collective MI assumption, arithmetic mean for posteriors *   2. collective MI assumption, geometric mean for posteriors</pre> *  <!-- options-end --> * * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @author Xin Xu (xx5@cs.waikato.ac.nz) * @version $Revision: 1.3 $  */public class MILR  extends Classifier   implements OptionHandler, MultiInstanceCapabilitiesHandler {  /** for serialization */  static final long serialVersionUID = 1996101190172373826L;    protected double[] m_Par;  /** The number of the class labels */  protected int m_NumClasses;  /** The ridge parameter. */  protected double m_Ridge = 1e-6;  /** Class labels for each bag */  protected int[] m_Classes;  /** MI data */   protected double[][][] m_Data;  /** All attribute names */  protected Instances m_Attributes;  protected double[] xMean = null, xSD = null;  /** the type of processing */  protected int m_AlgorithmType = ALGORITHMTYPE_DEFAULT;  /** standard MI assumption */  public static final int ALGORITHMTYPE_DEFAULT = 0;  /** collective MI assumption, arithmetic mean for posteriors */  public static final int ALGORITHMTYPE_ARITHMETIC = 1;  /** collective MI assumption, geometric mean for posteriors */  public static final int ALGORITHMTYPE_GEOMETRIC = 2;  /** the types of algorithms */  public static final Tag [] TAGS_ALGORITHMTYPE = {    new Tag(ALGORITHMTYPE_DEFAULT, "standard MI assumption"),    new Tag(ALGORITHMTYPE_ARITHMETIC, "collective MI assumption, arithmetic mean for posteriors"),    new Tag(ALGORITHMTYPE_GEOMETRIC, "collective MI assumption, geometric mean for posteriors"),  };  /**   * Returns the tip text for this property   *   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String globalInfo() {    return         "Uses either standard or collective multi-instance assumption, but "      + "within linear regression. For the collective assumption, it offers "      + "arithmetic or geometric mean for the posteriors.";  }  /**   * Returns an enumeration describing the available options   *   * @return an enumeration of all the available options   */  public Enumeration listOptions() {    Vector result = new Vector();        result.addElement(new Option(          "\tTurn on debugging output.",          "D", 0, "-D"));        result.addElement(new Option(        "\tSet the ridge in the log-likelihood.",        "R", 1, "-R <ridge>"));    result.addElement(new Option(        "\tDefines the type of algorithm:\n"        + "\t 0. standard MI assumption\n"        + "\t 1. collective MI assumption, arithmetic mean for posteriors\n"        + "\t 2. collective MI assumption, geometric mean for posteriors",        "A", 1, "-A [0|1|2]"));    return result.elements();  }  /**   * Parses a given list of options.    *   * @param options the list of options as an array of strings   * @throws Exception if an option is not supported   */  public void setOptions(String[] options) throws Exception {    String      tmpStr;    setDebug(Utils.getFlag('D', options));    tmpStr = Utils.getOption('R', options);    if (tmpStr.length() != 0)       setRidge(Double.parseDouble(tmpStr));    else       setRidge(1.0e-6);    tmpStr = Utils.getOption('A', options);    if (tmpStr.length() != 0) {      setAlgorithmType(new SelectedTag(Integer.parseInt(tmpStr), TAGS_ALGORITHMTYPE));    } else {      setAlgorithmType(new SelectedTag(ALGORITHMTYPE_DEFAULT, TAGS_ALGORITHMTYPE));    }       }  /**   * Gets the current settings of the classifier.   *   * @return an array of strings suitable for passing to setOptions   */  public String[] getOptions() {    Vector        result;        result = new Vector();    if (getDebug())      result.add("-D");        result.add("-R");    result.add("" + getRidge());        result.add("-A");    result.add("" + m_AlgorithmType);    return (String[]) result.toArray(new String[result.size()]);  }  /**   * Returns the tip text for this property   *   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String ridgeTipText() {    return "The ridge in the log-likelihood.";  }  /**   * Sets the ridge in the log-likelihood.   *   * @param ridge the ridge   */  public void setRidge(double ridge) {    m_Ridge = ridge;  }  /**   * Gets the ridge in the log-likelihood.   *   * @return the ridge   */  public double getRidge() {    return m_Ridge;  }  /**   * Returns the tip text for this property   *   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String algorithmTypeTipText() {    return "The mean type for the posteriors.";  }  /**   * Gets the type of algorithm.   *   * @return the algorithm type   */  public SelectedTag getAlgorithmType() {    return new SelectedTag(m_AlgorithmType, TAGS_ALGORITHMTYPE);  }  /**   * Sets the algorithm type.   *   * @param newType the new algorithm type   */  public void setAlgorithmType(SelectedTag newType) {    if (newType.getTags() == TAGS_ALGORITHMTYPE) {      m_AlgorithmType = newType.getSelectedTag().getID();    }  }  private class OptEng     extends Optimization {        /** the type to use      * @see MILR#TAGS_ALGORITHMTYPE */    private int m_Type;        /**     * initializes the object     *      * @param type      the type top use     * @see MILR#TAGS_ALGORITHMTYPE     */    public OptEng(int type) {      super();            m_Type = type;    }        /**      * Evaluate objective function     * @param x the current values of variables     * @return the value of the objective function      */    protected double objectiveFunction(double[] x){      double nll = 0; // -LogLikelihood            switch (m_Type) {        case ALGORITHMTYPE_DEFAULT:          for(int i=0; i<m_Classes.length; i++){ // ith bag            int nI = m_Data[i][0].length; // numInstances in ith bag            double bag = 0.0, // NLL of each bag                    prod = 0.0;   // Log-prob.             for(int j=0; j<nI; j++){              double exp=0.0;              for(int k=m_Data[i].length-1; k>=0; k--)                exp += m_Data[i][k][j]*x[k+1];              exp += x[0];              exp = Math.exp(exp);              if(m_Classes[i]==1)                prod -= Math.log(1.0+exp);              else                bag += Math.log(1.0+exp);            }            if(m_Classes[i]==1)              bag = -Math.log(1.0-Math.exp(prod));            nll += bag;          }             break;                case ALGORITHMTYPE_ARITHMETIC:          for(int i=0; i<m_Classes.length; i++){ // ith bag            int nI = m_Data[i][0].length; // numInstances in ith bag            double bag = 0;  // NLL of each bag            for(int j=0; j<nI; j++){              double exp=0.0;              for(int k=m_Data[i].length-1; k>=0; k--)                exp += m_Data[i][k][j]*x[k+1];              exp += x[0];              exp = Math.exp(exp);              if(m_Classes[i] == 1)                bag += 1.0-1.0/(1.0+exp); // To avoid exp infinite              else                bag += 1.0/(1.0+exp);                              }               bag /= (double)nI;            nll -= Math.log(bag);          }             break;                  case ALGORITHMTYPE_GEOMETRIC:          for(int i=0; i<m_Classes.length; i++){ // ith bag            int nI = m_Data[i][0].length; // numInstances in ith bag            double bag = 0;   // Log-prob.             for(int j=0; j<nI; j++){              double exp=0.0;              for(int k=m_Data[i].length-1; k>=0; k--)                exp += m_Data[i][k][j]*x[k+1];              exp += x[0];              if(m_Classes[i]==1)                bag -= exp/(double)nI;              else                bag += exp/(double)nI;            }            nll += Math.log(1.0+Math.exp(bag));          }             break;      }      // ridge: note that intercepts NOT included      for(int r=1; r<x.length; r++)        nll += m_Ridge*x[r]*x[r];      return nll;    }    /**      * Evaluate Jacobian vector     * @param x the current values of variables     * @return the gradient vector      */    protected double[] evaluateGradient(double[] x){      double[] grad = new double[x.length];            switch (m_Type) {        case ALGORITHMTYPE_DEFAULT:          for(int i=0; i<m_Classes.length; i++){ // ith bag            int nI = m_Data[i][0].length; // numInstances in ith bag            double denom = 0.0; // denominator, in log-scale                   double[] bag = new double[grad.length]; //gradient update with ith bag            for(int j=0; j<nI; j++){              // Compute exp(b0+b1*Xi1j+...)/[1+exp(b0+b1*Xi1j+...)]              double exp=0.0;              for(int k=m_Data[i].length-1; k>=0; k--)                exp += m_Data[i][k][j]*x[k+1];              exp += x[0];              exp = Math.exp(exp)/(1.0+Math.exp(exp));              if(m_Classes[i]==1)                // Bug fix: it used to be denom += Math.log(1.0+exp);                // Fixed 21 Jan 2005 (Eibe)                denom -= Math.log(1.0-exp);              // Instance-wise update of dNLL/dBk              for(int p=0; p<x.length; p++){  // pth variable                double m = 1.0;                if(p>0) m=m_Data[i][p-1][j];                bag[p] += m*exp;              }                 }            denom = Math.exp(denom);            // Bag-wise update of dNLL/dBk            for(int q=0; q<grad.length; q++){              if(m_Classes[i]==1)                grad[q] -= bag[q]/(denom-1.0);              else                grad[q] += bag[q];            }             }          break;                case ALGORITHMTYPE_ARITHMETIC:          for(int i=0; i<m_Classes.length; i++){ // ith bag

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -