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

📄 miemdd.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. *//* * MIEMDD.java * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand * */package weka.classifiers.mi;import weka.classifiers.RandomizableClassifier;import weka.core.Capabilities;import weka.core.FastVector;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.TechnicalInformation;import weka.core.TechnicalInformationHandler;import weka.core.Utils;import weka.core.Capabilities.Capability;import weka.core.TechnicalInformation.Field;import weka.core.TechnicalInformation.Type;import weka.filters.Filter;import weka.filters.unsupervised.attribute.Normalize;import weka.filters.unsupervised.attribute.ReplaceMissingValues;import weka.filters.unsupervised.attribute.Standardize;import java.util.Enumeration;import java.util.Random;import java.util.Vector;/** <!-- globalinfo-start --> * EMDD model builds heavily upon Dietterich's Diverse Density (DD) algorithm.<br/> * It is a general framework for MI learning of converting the MI problem to a single-instance setting using EM. In this implementation, we use most-likely cause DD model and only use 3 random selected postive bags as initial starting points of EM.<br/> * <br/> * For more information see:<br/> * <br/> * Qi Zhang, Sally A. Goldman: EM-DD: An Improved Multiple-Instance Learning Technique. In: Advances in Neural Information Processing Systems 14, 1073-108, 2001. * <p/> <!-- globalinfo-end --> *  <!-- technical-bibtex-start --> * BibTeX: * <pre> * &#64;inproceedings{Zhang2001, *    author = {Qi Zhang and Sally A. Goldman}, *    booktitle = {Advances in Neural Information Processing Systems 14}, *    pages = {1073-108}, *    publisher = {MIT Press}, *    title = {EM-DD: An Improved Multiple-Instance Learning Technique}, *    year = {2001} * } * </pre> * <p/> <!-- technical-bibtex-end --> * <!-- options-start --> * Valid options are: <p/> *  * <pre> -N &lt;num&gt; *  Whether to 0=normalize/1=standardize/2=neither. *  (default 1=standardize)</pre> *  * <pre> -S &lt;num&gt; *  Random number seed. *  (default 1)</pre> *  * <pre> -D *  If set, classifier is run in debug mode and *  may output additional info to the console</pre> *  <!-- options-end --> *      * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @author Lin Dong (ld21@cs.waikato.ac.nz) * @version $Revision: 1.5 $  */public class MIEMDD   extends RandomizableClassifier   implements OptionHandler, MultiInstanceCapabilitiesHandler,             TechnicalInformationHandler {  /** for serialization */  static final long serialVersionUID = 3899547154866223734L;    /** The index of the class attribute */  protected int m_ClassIndex;  protected double[] m_Par;  /** The number of the class labels */  protected int m_NumClasses;  /** Class labels for each bag */  protected int[] m_Classes;  /** MI data */  protected double[][][] m_Data;  /** All attribute names */  protected Instances m_Attributes;  /** MI data */	  protected double[][] m_emData;  /** The filter used to standardize/normalize all values. */  protected Filter m_Filter = null;  /** Whether to normalize/standardize/neither, default:standardize */  protected int m_filterType = FILTER_STANDARDIZE;  /** Normalize training data */  public static final int FILTER_NORMALIZE = 0;  /** Standardize training data */  public static final int FILTER_STANDARDIZE = 1;  /** No normalization/standardization */  public static final int FILTER_NONE = 2;  /** The filter to apply to the training data */  public static final Tag[] TAGS_FILTER = {    new Tag(FILTER_NORMALIZE, "Normalize training data"),    new Tag(FILTER_STANDARDIZE, "Standardize training data"),    new Tag(FILTER_NONE, "No normalization/standardization"),  };  /** The filter used to get rid of missing values. */  protected ReplaceMissingValues m_Missing = new ReplaceMissingValues();  /**   * Returns a string describing this filter   *   * @return a description of the filter suitable for   * displaying in the explorer/experimenter gui   */  public String globalInfo() {    return         "EMDD model builds heavily upon Dietterich's Diverse Density (DD) "      + "algorithm.\nIt is a general framework for MI learning of converting "      + "the MI problem to a single-instance setting using EM. In this "      + "implementation, we use most-likely cause DD model and only use 3 "      + "random selected postive bags as initial starting points of EM.\n\n"      + "For more information see:\n\n"      + getTechnicalInformation().toString();  }  /**   * Returns an instance of a TechnicalInformation object, containing    * detailed information about the technical background of this class,   * e.g., paper reference or book this class is based on.   *    * @return the technical information about this class   */  public TechnicalInformation getTechnicalInformation() {    TechnicalInformation 	result;        result = new TechnicalInformation(Type.INPROCEEDINGS);    result.setValue(Field.AUTHOR, "Qi Zhang and Sally A. Goldman");    result.setValue(Field.TITLE, "EM-DD: An Improved Multiple-Instance Learning Technique");    result.setValue(Field.BOOKTITLE, "Advances in Neural Information Processing Systems 14");    result.setValue(Field.YEAR, "2001");    result.setValue(Field.PAGES, "1073-108");    result.setValue(Field.PUBLISHER, "MIT Press");        return result;  }  /**   * 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(          "\tWhether to 0=normalize/1=standardize/2=neither.\n"           + "\t(default 1=standardize)",          "N", 1, "-N <num>"));    Enumeration enm = super.listOptions();    while (enm.hasMoreElements())      result.addElement(enm.nextElement());    return result.elements();  }  /**   * Parses a given list of options. <p/>   *    <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -N &lt;num&gt;   *  Whether to 0=normalize/1=standardize/2=neither.   *  (default 1=standardize)</pre>   *    * <pre> -S &lt;num&gt;   *  Random number seed.   *  (default 1)</pre>   *    * <pre> -D   *  If set, classifier is run in debug mode and   *  may output additional info to the console</pre>   *    <!-- options-end -->   *   * @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;        tmpStr = Utils.getOption('N', options);    if (tmpStr.length() != 0) {      setFilterType(new SelectedTag(Integer.parseInt(tmpStr), TAGS_FILTER));    } else {      setFilterType(new SelectedTag(FILTER_STANDARDIZE, TAGS_FILTER));    }         super.setOptions(options);  }  /**   * Gets the current settings of the classifier.   *   * @return an array of strings suitable for passing to setOptions   */  public String[] getOptions() {    Vector	result;    String[]	options;    int		i;        result  = new Vector();    options = super.getOptions();    for (i = 0; i < options.length; i++)      result.add(options[i]);        result.add("-N");    result.add("" + m_filterType);    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 filterTypeTipText() {    return "The filter type for transforming the training data.";  }  /**   * Gets how the training data will be transformed. Will be one of   * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE.   *   * @return the filtering mode   */  public SelectedTag getFilterType() {    return new SelectedTag(m_filterType, TAGS_FILTER);  }  /**   * Sets how the training data will be transformed. Should be one of   * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE.   *   * @param newType the new filtering mode   */  public void setFilterType(SelectedTag newType) {    if (newType.getTags() == TAGS_FILTER) {      m_filterType = newType.getSelectedTag().getID();    }  }  private class OptEng     extends Optimization {    /**     * 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      for (int i=0; i<m_Classes.length; i++){ // ith bag        double ins=0.0;        for (int k=0; k<m_emData[i].length; k++)  //attribute index          ins += (m_emData[i][k]-x[k*2])*(m_emData[i][k]-x[k*2])*            x[k*2+1]*x[k*2+1];        ins = Math.exp(-ins); // Pr. of being positive        if (m_Classes[i]==1){          if (ins <= m_Zero) ins = m_Zero;          nll -= Math.log(ins); //bag level -LogLikelihood        }        else{          ins = 1.0 - ins;  //Pr. of being negative          if(ins<=m_Zero) ins=m_Zero;          nll -= Math.log(ins);        }      }      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];      for (int i=0; i<m_Classes.length; i++){ // ith bag        double[] numrt = new double[x.length];        double exp=0.0;        for (int k=0; k<m_emData[i].length; k++) //attr index          exp += (m_emData[i][k]-x[k*2])*(m_emData[i][k]-x[k*2])            *x[k*2+1]*x[k*2+1];        exp = Math.exp(-exp);  //Pr. of being positive        //Instance-wise update        for (int p=0; p<m_emData[i].length; p++){  // pth variable          numrt[2*p] = 2.0*(x[2*p]-m_emData[i][p])*x[p*2+1]*x[p*2+1];          numrt[2*p+1] = 2.0*(x[2*p]-m_emData[i][p])*(x[2*p]-m_emData[i][p])            *x[p*2+1];        }        //Bag-wise update        for (int q=0; q<m_emData[i].length; q++){          if (m_Classes[i] == 1) {//derivation of (-LogLikeliHood) for positive bags            grad[2*q] += numrt[2*q];            grad[2*q+1] += numrt[2*q+1];          }          else{ //derivation of (-LogLikeliHood) for negative bags            grad[2*q] -= numrt[2*q]*exp/(1.0-exp);            grad[2*q+1] -= numrt[2*q+1]*exp/(1.0-exp);          }        }      } // one bag      return grad;    }  }  /**   * Returns default capabilities of the classifier.   *   * @return      the capabilities of this classifier   */  public Capabilities getCapabilities() {    Capabilities result = super.getCapabilities();    // attributes

⌨️ 快捷键说明

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