util

package
v0.0.0-...-afed897 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2015 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expect

func Expect(t *testing.T, expect string, actual interface{})

func ExpectNear

func ExpectNear(t *testing.T, expect float64, actual float64, acc float64)

func MatrixDotProduct

func MatrixDotProduct(one, two *Matrix) float64

func Mod

func Mod(a, b int) int

计算 a mod b

func VecDotProduct

func VecDotProduct(Vector1, Vector2 *Vector) float64

计算点乘积 Vector1^T * Vector2

Types

type CircularBuffer

type CircularBuffer struct {
	// contains filtered or unexported fields
}

环形缓存 数据从0位置开始添加,当溢出时指针重置为0,新数据从0位置开始覆盖

func NewCircularBuffer

func NewCircularBuffer(size int) *CircularBuffer

必须使用该函数创建环形缓存,size为缓存区长度

func (*CircularBuffer) NumValues

func (cb *CircularBuffer) NumValues() int

返回环形缓存中的实际存储的数值个数

func (*CircularBuffer) Push

func (cb *CircularBuffer) Push(v float64)

向缓存中添加一个数

func (*CircularBuffer) Sum

func (cb *CircularBuffer) Sum() (s float64)

计算缓存中所有数之和

type Matrix

type Matrix struct {
	// contains filtered or unexported fields
}

矩阵,用以存储多个标注对应的权重值

矩阵根据存储不同分为稀疏矩阵和稠密矩阵,区别见vector.go文件中对Vector结构体的注释

请不要直接创建Matrix,而是通过NewMatrix函数(稠密矩阵)和NewSparseMatrix(稀疏矩阵) 函数进行创建。

func NewMatrix

func NewMatrix(numLabels, numValues int) *Matrix

创建一个稠密矩阵,该矩阵有numLabels个标注,每个标注有numValues个值

func NewSparseMatrix

func NewSparseMatrix(numLabels int) *Matrix

创建一个稀疏矩阵,该矩阵有numLabels个标注,每个标注有numValues个值

func (*Matrix) Clear

func (m *Matrix) Clear()

清空矩阵中的值,此函数不改变矩阵的维度

func (*Matrix) DeepCopy

func (m *Matrix) DeepCopy(that *Matrix)

深度拷贝that矩阵

func (*Matrix) Get

func (m *Matrix) Get(label, index int) float64

得到矩阵中第label个标注的第index个值 如果index越界或者该值不存在,则返回0

func (*Matrix) GetValues

func (m *Matrix) GetValues(label int) *Vector

得到第label个标注的值向量

func (*Matrix) Increment

func (m *Matrix) Increment(that *Matrix, alpha float64)

m = m + alpha * that

func (*Matrix) IsSparse

func (m *Matrix) IsSparse() bool

返回矩阵是否为稀疏矩阵

func (*Matrix) MarshalJSON

func (m *Matrix) MarshalJSON() ([]byte, error)

对Matrix结构体进行JSON串行化

func (*Matrix) Norm

func (m *Matrix) Norm() float64

返回矩阵中所有元素的2-Norm

func (*Matrix) NumLabels

func (m *Matrix) NumLabels() int

返回矩阵的标注数目

func (*Matrix) NumValues

func (m *Matrix) NumValues() int

返回标注值向量的长度 注意只能调用稠密矩阵的NumValues函数,对稀疏矩阵调用此函数非法

func (*Matrix) Opposite

func (m *Matrix) Opposite() *Matrix

返回 -m

func (*Matrix) Populate

func (m *Matrix) Populate() *Matrix

返回一个空矩阵,此矩阵的类型(是否稀疏)和维度和m相同

func (*Matrix) Scale

func (m *Matrix) Scale(s float64)

m = s * m

func (*Matrix) Set

func (m *Matrix) Set(label, index int, value float64)

设置m矩阵label标注向量的index元素的值为value

func (*Matrix) UnmarshalJSON

func (m *Matrix) UnmarshalJSON(b []byte) error

对Matrix结构体进行JSON反串行化

func (*Matrix) WeightedSum

func (m *Matrix) WeightedSum(m1, m2 *Matrix, a, b float64)

m = a * m1 + b * m2 注意m(指针)不能等于m1或者m2

type MatrixJSON

type MatrixJSON struct {
	Values    []*Vector
	NumValues int
	IsSparse  bool
}

Matrix结构体JSON串行化/反串行化临时存储结构体

type Vector

type Vector struct {
	// contains filtered or unexported fields
}

向量分两种类型:

  1. 稠密向量,元素的key从0开始到N结束,实际值保存在切片中 使用NewVector(length)开辟新的稠密向量 稠密向量的长度在新建向量时已经指定好,无法更改
  1. 稀疏向量,元素的key可以是不连续的值,保存在map中 使用NewSparseVector()开辟新的稀疏向量 稀疏向量主要用于处理特征稀疏的超大规模机器学习问题

需要注意的是

1. 请不要混合使用稀疏和稠密向量 2. 和其他类型一样,向量是协程不安全的

请使用如下方法遍历向量中元素的值

for _, key := range(vector.Keys()) {
  vector.Get(key)
}

func NewSparseVector

func NewSparseVector() *Vector

构造稀疏向量

func NewVector

func NewVector(length int) *Vector

构造长度(维度)为length的稠密向量

func (*Vector) Clear

func (v *Vector) Clear()

向量值清零

func (*Vector) DeepCopy

func (v *Vector) DeepCopy(that *Vector)

复制元素的值

func (*Vector) Get

func (v *Vector) Get(index int) float64

得到向量中单个元素的值 如果index不存在或者越界,返回0

func (*Vector) Increment

func (v *Vector) Increment(that *Vector, alpha float64)

v = v + alpha * that

func (*Vector) IsHomogeneous

func (v *Vector) IsHomogeneous(that *Vector) bool

返回两个向量是否同质,同质的两个向量稀疏类型相同,如果都是稠密矩阵则长度也需要相同

func (*Vector) IsSparse

func (v *Vector) IsSparse() bool

返回向量是否为稀疏向量

func (*Vector) Keys

func (v *Vector) Keys() []int

返回向量索引的键值,用于遍历向量中的元素,使用方法见Vector结构体注释

func (*Vector) MarshalJSON

func (v *Vector) MarshalJSON() ([]byte, error)

对Vector结构体进行JSON串行化

func (*Vector) Multiply

func (v *Vector) Multiply(a, b float64, that *Vector)

v_i = (v_i*a + b) * that_i

func (*Vector) Norm

func (v *Vector) Norm() float64

向量的2-模

func (*Vector) Opposite

func (v *Vector) Opposite() *Vector

得到 -v

func (*Vector) Populate

func (v *Vector) Populate() *Vector

返回一个空向量,此向量的类型(是否稀疏)和维度和v相同

func (*Vector) Scale

func (v *Vector) Scale(s float64)

更新 v = s * v

func (*Vector) Set

func (v *Vector) Set(index int, value float64)

设置向量中单个元素的值

func (*Vector) SetAll

func (v *Vector) SetAll(value float64)

设置向量中所有元素的值为value

func (*Vector) SetValues

func (v *Vector) SetValues(values []float64)

设置向量中多个元素的值,第i个元素设为为values[i]

func (*Vector) UnmarshalJSON

func (v *Vector) UnmarshalJSON(b []byte) error

对Vector结构体进行JSON反串行化

func (*Vector) WeightedSum

func (v *Vector) WeightedSum(Vector1, Vector2 *Vector, a, b float64)

计算两个向量的线性求和 v = a * Vector1 + b * Vector2

type VectorJSON

type VectorJSON struct {
	Values   []float64
	ValueMap map[string]float64
	Keys     []int
	IsSparse bool
}

Matrix结构体JSON串行化/反串行化临时存储结构体

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL