Code Monkey home page Code Monkey logo

lbfgs's Introduction

lbfgs

L-BFGS的go语言实现

使用

下面的例子计算x^2+y^2在(1, 0.1)附近的极值。

package main

import (
	"fmt"
	"github.com/huichen/lbfgs"
)

func main() {
	// 初始化LBFGS,存储步长为10,x向量为二维
	optimizer := lbfgs.NewOptimizer(10, 2)

	// x为自变量,g为目标函数的偏微分向量,都是二维向量
	x := lbfgs.NewVector(2)
	g := lbfgs.NewVector(2)

	// x初始化为(1, 0.1)
	x.SetValues([]float32{1, 0.1})

	k := 0
	for {
		fmt.Println("====== 第", k, "次迭代")
		fmt.Println("x =", x)

		// 更新偏导数向量
		g.SetValues([]float32{2*x.Get(0), 2*x.Get(1)})
		fmt.Println("g =", g)

		// 计算x更新的步长
		delta := optimizer.GetDeltaX(x, g)

		// 更新x
		x.Increment(delta, 1)

		// 检查是否满足收敛条件
		if g.Norm() < 0.0001 {
			break
		}
		k++
	}

	// 打印最终的x值
	fmt.Println("==== 结束 ====")
	fmt.Println("x =", x)
}

lbfgs's People

Contributors

huichen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.