gradientascent.java

来自「常用机器学习算法,java编写源代码,内含常用分类算法,包括说明文档」· Java 代码 · 共 66 行

JAVA
66
字号
/* Copyright (C) 2002 Univ. of Massachusetts Amherst, Computer Science Dept.   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).   http://www.cs.umass.edu/~mccallum/mallet   This software is provided under the terms of the Common Public License,   version 1.0, as published by http://www.opensource.org.  For further   information, see the file `LICENSE' included with this distribution. *//**    @author Andrew McCallum <a href="mailto:mccallum@cs.umass.edu">mccallum@cs.umass.edu</a> */package edu.umass.cs.mallet.base.maximize;import edu.umass.cs.mallet.base.util.MalletLogger;import java.util.logging.*;// Gradient Ascentpublic class GradientAscent implements Maximizer.ByGradient{	private static Logger logger = MalletLogger.getLogger(GradientAscent.class.getName());	double initialStepSize = 0.2;	double tolerance = 0.001;	int maxIterations = 200;	LineMaximizer.ByGradient lineMaximizer = new BackTrackLineSearch();	// "eps" is a small number to recitify the special case of converging	// to exactly zero function value	final double eps = 1.0e-10;		public GradientAscent ()	{	}	public boolean maximize (Maximizable.ByGradient maxable)	{		return maximize (maxable, maxIterations);	}		public boolean maximize (Maximizable.ByGradient maxable, int numIterations)	{		int iterations;		double step = initialStepSize;		double fret;		double fp = maxable.getValue ();    double[] xi = new double [maxable.getNumParameters()];		maxable.getValueGradient(xi);		for (iterations = 0; iterations < numIterations; iterations++) {			logger.info ("At iteration "+iterations+", cost = "+fp);			step = lineMaximizer.maximize (maxable, xi, step);			fret = maxable.getValue ();			if (2.0*Math.abs(fret-fp) <= tolerance*(Math.abs(fret)+Math.abs(fp)+eps))				return true;			fp = fret;			maxable.getValueGradient(xi);		}		return false;	}	}

⌨️ 快捷键说明

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