本文整理汇总了Golang中github.com/gonum/matrix/mat64.Vector.At方法的典型用法代码示例。如果您正苦于以下问题:Golang Vector.At方法的具体用法?Golang Vector.At怎么用?Golang Vector.At使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/gonum/matrix/mat64.Vector
的用法示例。
在下文中一共展示了Vector.At方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GradientDescent
func GradientDescent(X *mat64.Dense, y *mat64.Vector, alpha, tolerance float64, maxIters int) *mat64.Vector {
// m = Number of Training Examples
// n = Number of Features
m, n := X.Dims()
h := mat64.NewVector(m, nil)
partials := mat64.NewVector(n, nil)
new_theta := mat64.NewVector(n, nil)
Regression:
for i := 0; i < maxIters; i++ {
// Calculate partial derivatives
h.MulVec(X, new_theta)
for el := 0; el < m; el++ {
val := (h.At(el, 0) - y.At(el, 0)) / float64(m)
h.SetVec(el, val)
}
partials.MulVec(X.T(), h)
// Update theta values
for el := 0; el < n; el++ {
new_val := new_theta.At(el, 0) - (alpha * partials.At(el, 0))
new_theta.SetVec(el, new_val)
}
// Check the "distance" to the local minumum
dist := math.Sqrt(mat64.Dot(partials, partials))
if dist <= tolerance {
break Regression
}
}
return new_theta
}
示例2: findIn
// findIn returns the indexes of the values in vec that match scalar
func findIn(scalar float64, vec *mat.Vector) *mat.Vector {
var result []float64
for i := 0; i < vec.Len(); i++ {
if scalar == vec.At(i, 0) {
result = append(result, float64(i))
}
}
return mat.NewVector(len(result), result)
}
示例3: rowIndexIn
// rowIndexIn returns a matrix contains the rows in indexes vector
func rowIndexIn(indexes *mat.Vector, M mat.Matrix) mat.Matrix {
m := indexes.Len()
_, n := M.Dims()
Res := mat.NewDense(m, n, nil)
for i := 0; i < m; i++ {
Res.SetRow(i, mat.Row(
nil,
int(indexes.At(i, 0)),
M))
}
return Res
}
示例4: Cost
func Cost(x *mat64.Dense, y, theta *mat64.Vector) float64 {
//initialize receivers
m, _ := x.Dims()
h := mat64.NewDense(m, 1, make([]float64, m))
squaredErrors := mat64.NewDense(m, 1, make([]float64, m))
//actual calculus
h.Mul(x, theta)
squaredErrors.Apply(func(r, c int, v float64) float64 {
return math.Pow(h.At(r, c)-y.At(r, c), 2)
}, h)
j := mat64.Sum(squaredErrors) * 1.0 / (2.0 * float64(m))
return j
}
示例5: Map
// Map produces a vector that is within the bounds of the
// rectangular manifold of toroidal space, given a vector
// that is on the torus but may be outside these bounds.
func (t Torus) Map(v *mat64.Vector) {
x := v.At(0, 0)
y := v.At(1, 0)
remx := x
right := t.W / 2
if math.Abs(x) > right {
remx = math.Mod(t.W, -x)
}
remy := y
top := t.H / 2
if math.Abs(y) > top {
remy = math.Mod(t.H, -y)
}
v.SetVec(0, remx)
v.SetVec(1, remy)
}
示例6: StdDevBatch
// StdDevBatch predicts the standard deviation at a set of locations of x.
func (g *GP) StdDevBatch(std []float64, x mat64.Matrix) []float64 {
r, c := x.Dims()
if c != g.inputDim {
panic(badInputLength)
}
if std == nil {
std = make([]float64, r)
}
if len(std) != r {
panic(badStorage)
}
// For a single point, the stddev is
// sigma = k(x,x) - k_*^T * K^-1 * k_*
// where k is the vector of kernels between the input points and the output points
// For many points, the formula is:
// nu_* = k(x_*, k_*) - k_*^T * K^-1 * k_*
// This creates the full covariance matrix which is an rxr matrix. However,
// the standard deviations are just the diagonal of this matrix. Instead, be
// smart about it and compute the diagonal terms one at a time.
kStar := g.formKStar(x)
var tmp mat64.Dense
tmp.SolveCholesky(g.cholK, kStar)
// set k(x_*, x_*) into std then subtract k_*^T K^-1 k_* , computed one row at a time
var tmp2 mat64.Vector
row := make([]float64, c)
for i := range std {
for k := 0; k < c; k++ {
row[k] = x.At(i, k)
}
std[i] = g.kernel.Distance(row, row)
tmp2.MulVec(kStar.ColView(i).T(), tmp.ColView(i))
rt, ct := tmp2.Dims()
if rt != 1 && ct != 1 {
panic("bad size")
}
std[i] -= tmp2.At(0, 0)
std[i] = math.Sqrt(std[i])
}
// Need to scale the standard deviation to be in the same units as y.
floats.Scale(g.std, std)
return std
}
示例7: StdDev
// StdDev predicts the standard deviation of the function at x.
func (g *GP) StdDev(x []float64) float64 {
if len(x) != g.inputDim {
panic(badInputLength)
}
// nu_* = k(x_*, k_*) - k_*^T * K^-1 * k_*
n := len(g.outputs)
kstar := mat64.NewVector(n, nil)
for i := 0; i < n; i++ {
v := g.kernel.Distance(g.inputs.RawRowView(i), x)
kstar.SetVec(i, v)
}
self := g.kernel.Distance(x, x)
var tmp mat64.Vector
tmp.SolveCholeskyVec(g.cholK, kstar)
var tmp2 mat64.Vector
tmp2.MulVec(kstar.T(), &tmp)
rt, ct := tmp2.Dims()
if rt != 1 || ct != 1 {
panic("bad size")
}
return math.Sqrt(self-tmp2.At(0, 0)) * g.std
}
示例8: csrMulMatVec
func csrMulMatVec(y *mat64.Vector, alpha float64, transA bool, a *CSR, x *mat64.Vector) {
r, c := a.Dims()
if transA {
if r != x.Len() || c != y.Len() {
panic("sparse: dimension mismatch")
}
} else {
if r != y.Len() || c != x.Len() {
panic("sparse: dimension mismatch")
}
}
if alpha == 0 {
return
}
yRaw := y.RawVector()
if transA {
row := Vector{N: y.Len()}
for i := 0; i < r; i++ {
start := a.rowIndex[i]
end := a.rowIndex[i+1]
row.Data = a.values[start:end]
row.Indices = a.columns[start:end]
Axpy(y, alpha*x.At(i, 0), &row)
}
} else {
row := Vector{N: x.Len()}
for i := 0; i < r; i++ {
start := a.rowIndex[i]
end := a.rowIndex[i+1]
row.Data = a.values[start:end]
row.Indices = a.columns[start:end]
yRaw.Data[i*yRaw.Inc] += alpha * Dot(&row, x)
}
}
}
示例9: ConditionNormal
// ConditionNormal returns the Normal distribution that is the receiver conditioned
// on the input evidence. The returned multivariate normal has dimension
// n - len(observed), where n is the dimension of the original receiver. The updated
// mean and covariance are
// mu = mu_un + sigma_{ob,un}^T * sigma_{ob,ob}^-1 (v - mu_ob)
// sigma = sigma_{un,un} - sigma_{ob,un}^T * sigma_{ob,ob}^-1 * sigma_{ob,un}
// where mu_un and mu_ob are the original means of the unobserved and observed
// variables respectively, sigma_{un,un} is the unobserved subset of the covariance
// matrix, sigma_{ob,ob} is the observed subset of the covariance matrix, and
// sigma_{un,ob} are the cross terms. The elements of x_2 have been observed with
// values v. The dimension order is preserved during conditioning, so if the value
// of dimension 1 is observed, the returned normal represents dimensions {0, 2, ...}
// of the original Normal distribution.
//
// ConditionNormal returns {nil, false} if there is a failure during the update.
// Mathematically this is impossible, but can occur with finite precision arithmetic.
func (n *Normal) ConditionNormal(observed []int, values []float64, src *rand.Rand) (*Normal, bool) {
if len(observed) == 0 {
panic("normal: no observed value")
}
if len(observed) != len(values) {
panic("normal: input slice length mismatch")
}
for _, v := range observed {
if v < 0 || v >= n.Dim() {
panic("normal: observed value out of bounds")
}
}
ob := len(observed)
unob := n.Dim() - ob
obMap := make(map[int]struct{})
for _, v := range observed {
if _, ok := obMap[v]; ok {
panic("normal: observed dimension occurs twice")
}
obMap[v] = struct{}{}
}
if len(observed) == n.Dim() {
panic("normal: all dimensions observed")
}
unobserved := make([]int, 0, unob)
for i := 0; i < n.Dim(); i++ {
if _, ok := obMap[i]; !ok {
unobserved = append(unobserved, i)
}
}
mu1 := make([]float64, unob)
for i, v := range unobserved {
mu1[i] = n.mu[v]
}
mu2 := make([]float64, ob) // really v - mu2
for i, v := range observed {
mu2[i] = values[i] - n.mu[v]
}
n.setSigma()
var sigma11, sigma22 mat64.SymDense
sigma11.SubsetSym(n.sigma, unobserved)
sigma22.SubsetSym(n.sigma, observed)
sigma21 := mat64.NewDense(ob, unob, nil)
for i, r := range observed {
for j, c := range unobserved {
v := n.sigma.At(r, c)
sigma21.Set(i, j, v)
}
}
var chol mat64.Cholesky
ok := chol.Factorize(&sigma22)
if !ok {
return nil, ok
}
// Compute sigma_{2,1}^T * sigma_{2,2}^-1 (v - mu_2).
v := mat64.NewVector(ob, mu2)
var tmp, tmp2 mat64.Vector
err := tmp.SolveCholeskyVec(&chol, v)
if err != nil {
return nil, false
}
tmp2.MulVec(sigma21.T(), &tmp)
// Compute sigma_{2,1}^T * sigma_{2,2}^-1 * sigma_{2,1}.
// TODO(btracey): Should this be a method of SymDense?
var tmp3, tmp4 mat64.Dense
err = tmp3.SolveCholesky(&chol, sigma21)
if err != nil {
return nil, false
}
tmp4.Mul(sigma21.T(), &tmp3)
for i := range mu1 {
mu1[i] += tmp2.At(i, 0)
}
// TODO(btracey): If tmp2 can constructed with a method, then this can be
// replaced with SubSym.
//.........这里部分代码省略.........
示例10: vec3
func vec3(vec *mat64.Vector) Vec3 {
return Vec3{vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}
}
示例11: BlasVec2UserVec
func BlasVec2UserVec(v *mat64.Vector) UserVec {
u := UserVec{}
u.X = v.At(0, 0)
u.Y = v.At(1, 0)
return u
}