当前位置: 首页>>代码示例>>Golang>>正文


Golang math.Tanh函数代码示例

本文整理汇总了Golang中math.Tanh函数的典型用法代码示例。如果您正苦于以下问题:Golang Tanh函数的具体用法?Golang Tanh怎么用?Golang Tanh使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Tanh函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestTanhKernelShouldPass1

func TestTanhKernelShouldPass1(t *testing.T) {
	k := TanhKernel(1)

	// test different dot products which
	// should be valid

	// when constant is 0, default to -1.0

	assert.InDelta(t, math.Tanh(1.0-1.0), k([]float64{
		0.0, 1.0, 1.0, 0.0,
	}, []float64{
		0.0, 1.0, 0.0, 0.0,
	}), 5e-4, "Dot product should be valid")

	assert.InDelta(t, math.Tanh(6.0-1.0), k([]float64{
		15.0, 1.0, -1.0, 0.0,
	}, []float64{
		1.0, 1.0, 10.0, 0.0,
	}), 5e-4, "Dot product should be valid")

	assert.InDelta(t, math.Tanh(-84.0-1.0), k([]float64{
		15.0, 1.0, -1.0, 0.0,
	}, []float64{
		1.0, 1.0, 100.0, 0.0,
	}), 5e-4, "Dot product should be valid")
}
开发者ID:gao8954,项目名称:goml,代码行数:26,代码来源:kernel_test.go

示例2: PiAnom

// Evaluate the anomalous retarded pair Green's function,
// Pi^A(k, omega)_{xx, xy, yy}. k must be a two-dimensional vector.
func PiAnom(env *tempAll.Environment, k vec.Vector, omega float64) vec.Vector {
	piInner := func(q vec.Vector, out *vec.Vector) {
		// Do vector operations on out to avoid allocation:
		//  first case, out = k/2 + q
		(*out)[0] = k[0]/2.0 + q[0]
		(*out)[1] = k[1]/2.0 + q[1]
		Delta1 := env.Delta_h(*out)
		E1 := env.BogoEnergy(*out)
		//  second case, out = k/2 - q
		(*out)[0] = k[0]/2.0 - q[0]
		(*out)[1] = k[1]/2.0 - q[1]
		Delta2 := env.Delta_h(*out)
		E2 := env.BogoEnergy(*out)
		// Get part of result that's the same for all (xx, xy, yy):
		t1 := math.Tanh(env.Beta * E1 / 2.0)
		t2 := math.Tanh(env.Beta * E2 / 2.0)
		common := -Delta1 * Delta2 / (4.0 * E1 * E2) * ((t1+t2)*(1.0/(omega+E1+E2)-1.0/(omega-E1-E2)) + (t1-t2)*(1.0/(omega-E1+E2)-1.0/(omega+E1-E2)))
		// Set out = result:
		sx := math.Sin(q[0])
		sy := math.Sin(q[1])
		(*out)[0] = sx * sx * common
		(*out)[1] = sx * sy * common
		(*out)[2] = sy * sy * common
	}
	return bzone.VectorAvg(env.PointsPerSide, 2, 3, piInner)
}
开发者ID:tflovorn,项目名称:scExplorer,代码行数:28,代码来源:pi_anom.go

示例3: Pi

// Evaluate the retarded pair Green's function Pi_R(k, omega)_{xx, xy, yy}.
// k must be a two-dimensional vector.
func Pi(env *tempAll.Environment, k vec.Vector, omega float64) vec.Vector {
	var piInner func(k vec.Vector, out *vec.Vector)
	// TODO: should this comparison be math.Abs(env.F0)? Not using that to
	// avoid going to finite F0 procedure when F0 < 0 (since F0 is
	// positive by choice of gauge). Also - would it be better to just
	// test if F0 == 0.0? Would prefer to avoid equality comparison
	// on float.
	if math.Abs(env.F0) < 1e-9 {
		piInner = func(q vec.Vector, out *vec.Vector) {
			// do vector operations on out to avoid allocation:
			// out = k/2 + q
			(*out)[0] = k[0]/2.0 + q[0]
			(*out)[1] = k[1]/2.0 + q[1]
			xp := env.Xi_h(*out)
			// out = k/2 - q
			(*out)[0] = k[0]/2.0 - q[0]
			(*out)[1] = k[1]/2.0 - q[1]
			xm := env.Xi_h(*out)

			tp := math.Tanh(env.Beta * xp / 2.0)
			tm := math.Tanh(env.Beta * xm / 2.0)
			common := -(tp + tm) / (omega - xp - xm)
			sx := math.Sin(q[0])
			sy := math.Sin(q[1])
			// out = result
			(*out)[0] = sx * sx * common
			(*out)[1] = sx * sy * common
			(*out)[2] = sy * sy * common
		}
	} else {
		piInner = func(q vec.Vector, out *vec.Vector) {
			// out = k/2 + q
			(*out)[0] = k[0]/2.0 + q[0]
			(*out)[1] = k[1]/2.0 + q[1]
			xi1 := env.Xi_h(*out)
			E1 := env.BogoEnergy(*out)
			// out = k/2 - q
			(*out)[0] = k[0]/2.0 - q[0]
			(*out)[1] = k[1]/2.0 - q[1]
			xi2 := env.Xi_h(*out)
			E2 := env.BogoEnergy(*out)

			A1 := 0.5 * (1.0 + xi1/E1)
			A2 := 0.5 * (1.0 + xi2/E2)
			B1 := 0.5 * (1.0 - xi1/E1)
			B2 := 0.5 * (1.0 - xi2/E2)
			t1 := math.Tanh(env.Beta * E1 / 2.0)
			t2 := math.Tanh(env.Beta * E2 / 2.0)
			common := -(t1+t2)*(A1*A2/(omega-E1-E2)-B1*B2/(omega+E1+E2)) - (t1-t2)*(A1*B2/(omega-E1+E2)-B1*A2/(omega+E1-E2))
			sx := math.Sin(q[0])
			sy := math.Sin(q[1])
			// out = result
			(*out)[0] = sx * sx * common
			(*out)[1] = sx * sy * common
			(*out)[2] = sy * sy * common
		}
	}
	return bzone.VectorAvg(env.PointsPerSide, 2, 3, piInner)
}
开发者ID:tflovorn,项目名称:scExplorer,代码行数:61,代码来源:pi.go

示例4: NewActivationFunction

func NewActivationFunction(name ActivationName) ActivationFunction {
	switch name {
	case ActivationName_LINEAR:
		return func(x mat64.Matrix, y *mat64.Dense) { y.Clone(x) }
	case ActivationName_LOGISTIC:
		return func(x mat64.Matrix, y *mat64.Dense) {
			y.Apply(func(r, c int, v float64) float64 {
				return 1 / (1 + math.Exp(-v))
			}, x)
		}
	case ActivationName_RELU:
		return func(x mat64.Matrix, y *mat64.Dense) {
			y.Apply(func(r, c int, v float64) float64 { return math.Max(0, v) }, x)
		}
	case ActivationName_TANH:
		return func(x mat64.Matrix, y *mat64.Dense) {
			y.Apply(func(r, c int, v float64) float64 { return math.Tanh(v) }, x)
		}
	case ActivationName_SOFTMAX:
		return func(x mat64.Matrix, y *mat64.Dense) {
			r, c := x.Dims()
			for i := 0; i < r; i++ {
				exp_sum := 0.0
				for j := 0; j < c; j++ {
					exp_sum = exp_sum + math.Exp(x.At(i, j))
				}
				for j := 0; j < c; j++ {
					y.Set(i, j, math.Exp(x.At(i, j))/exp_sum)
				}
			}
		}
	}
	return nil
}
开发者ID:evilrobot69,项目名称:NeuralGo,代码行数:34,代码来源:activation_function.go

示例5: innerMu_h

func innerMu_h(env *tempAll.Environment, k vec.Vector) float64 {
	sxy := math.Sin(k[0]) - math.Sin(k[1])
	numer := sxy * sxy * math.Tanh(env.Beta*env.Xi_h(k)/2.0)
	denom := env.Mu_b + 2.0*env.Xi_h(k)
	//denom := env.Mu_b - 2.0*env.Be_field*env.A + 2.0*env.Xi_h(k)
	return numer / denom
}
开发者ID:tflovorn,项目名称:scExplorer,代码行数:7,代码来源:systemMu_h.go

示例6: TanhKernel

// TanhKernel takes in a required Kappa modifier
// parameter (defaults to 1.0 if 0.0 given,) and
// optional float64 args afterwords which will be
// added together to create a constant term (general
// reccomended use is to just pass one arg as the
// constant if you need it.)
//
//     K(x, x`) = tanh(κx*x` + c)
//
// https://en.wikipedia.org/wiki/Hyperbolic_function
// https://en.wikipedia.org/wiki/Support_vector_machine#Nonlinear_classification
//
// Note that c must be less than 0 (if >= 0 default
// to -1.0) and κ (for most cases, but not all -
// hence no default) must be greater than 0
func TanhKernel(k float64, constants ...float64) func([]float64, []float64) float64 {
	if k == 0.0 {
		k = 1.0
	}

	var c float64
	if len(constants) != 0 {
		for _, val := range constants {
			c += val
		}
	}

	if c >= 0.0 {
		c = -1.0
	}

	return func(X []float64, x []float64) float64 {
		// don't throw error but fail peacefully
		//
		// returning "not at all similar", basically
		if len(X) != len(x) {
			return 0.0
		}

		var dot float64

		for i := range X {
			dot += k * X[i] * x[i]
		}

		return math.Tanh(dot + c)
	}
}
开发者ID:gao8954,项目名称:goml,代码行数:48,代码来源:kernel.go

示例7: Tanh

func Tanh(x2 Audio) (x Audio) {
	var v Audio
	var v2 Audio
	var v3 Audio
	var v4 int
	v2 = x2
	v3 = x2
	x22 := len(v2)
	v4 = x22
	x3 := make(Audio, v4)
	x = x3
	v = x3
	for k := range v {
		var v5 = &v[k]
		var v6 *float64
		var v7 int
		var v8 float64
		var v9 float64
		v7 = k
		v6 = v5
		x4 := &v3[v7]
		v8 = *x4
		x5 := math.Tanh(v8)
		v9 = x5
		*v6 = v9
	}
	return
}
开发者ID:gordonklaus,项目名称:flux,代码行数:28,代码来源:Tanh.flux.go

示例8: Expit

func Expit(x float64) (out float64) {
	//return 1.0 / (1.0 + math.Exp(-1.0*x))
	out = 0.5 * x
	out = math.Tanh(out)
	out += 1.0
	out *= 0.5
	return out
}
开发者ID:goist,项目名称:CloudForest,代码行数:8,代码来源:gradboostclasstarget.go

示例9: release

func (k *pluckedKey) release(loc geom.Point) {
	amp := math.Max(-6, math.Log2(math.Tanh(dist(loc, k.pressLoc)/64)))

	updateKeys(k.ratio)
	v := newPluckedTone(amp, math.Exp2(k.pitch))
	tones.Add(v)
	k.keyBase.voice = v
}
开发者ID:gordonklaus,项目名称:justkeys,代码行数:8,代码来源:key.go

示例10: D2ActivateDCombination

func (Tanh) D2ActivateDCombination(sum, output float64) float64 {
	// http://www.wolframalpha.com/input/?i=1.14393+Sech[%282+x%29%2F3]^2
	// -1.52524 tanh(2/3 * x) * sech2(2/3 * x)
	// tanh^2 = 1 - sech^2
	tanh := math.Tanh(twoThirds * sum)
	sec2 := 1 - tanh*tanh
	return tanhHessConst * tanh * sec2
}
开发者ID:reggo,项目名称:reggo,代码行数:8,代码来源:activator.go

示例11: AddTagScore

// AddTagScore should be called for every tag occurrence in the database. The
// 'dist' argument refers to the number of days between the date that
// suggestions are being generated for and the date of the closest serving of
// the meal the tag is associated with.
func (s *Scorer) AddTagScore(tag string, dist int) {
	score, ok := s.tagScores[tag]
	if !ok {
		score = 1.0 // the default
	}

	score *= 0.1 + float32(math.Tanh(float64(dist)*0.2))
	s.tagScores[tag] = score
}
开发者ID:kierdavis,项目名称:mealplanner,代码行数:13,代码来源:score.go

示例12: move

func (k *bowedKey) move(loc geom.Point) {
	dx := dist(loc, k.moveLoc)
	dt := time.Now().Sub(k.moveTime).Seconds()
	amp := math.Max(-12, math.Log2(math.Tanh(dx/dt/128)))
	a := math.Pow(.999, 1/dt)
	k.amp = a*amp + (1-a)*k.amp
	k.voice.attack(k.amp)

	k.moveLoc = loc
	k.moveTime = time.Now()
}
开发者ID:gordonklaus,项目名称:justkeys,代码行数:11,代码来源:key.go

示例13: TestMathTanh

func TestMathTanh(t *testing.T) {
	// This is just an interface to Go's function, so just a quick simple test
	ctx := runtime.NewCtx(nil, nil)
	mm := new(MathMod)
	mm.SetCtx(ctx)
	val := 1.12
	ret := mm.math_Tanh(runtime.Number(val))
	exp := math.Tanh(val)
	if ret.Float() != exp {
		t.Errorf("expected %f, got %f", exp, ret.Float())
	}
}
开发者ID:jmptrader,项目名称:agora,代码行数:12,代码来源:math_test.go

示例14: Sing

func (v *bowedTone) Sing() float64 {
	select {
	case targetAmp := <-v.ampChan:
		v.targetAmp = math.Max(v.targetAmp, targetAmp)
	default:
	}
	decay := 3.0 / 48000
	v.targetAmp -= decay
	da := 16.0 / 48000
	v.amp_ += math.Min(da, math.Max(-da, v.targetAmp-v.amp_))
	return math.Exp2(v.amp_) * math.Tanh(2*v.Osc.Sine())
}
开发者ID:gordonklaus,项目名称:justkeys,代码行数:12,代码来源:audio.go

示例15: Forward

// Apply a forward pass with the identity activation function
// g(x) = tanh(x).
func (l TanhLayer) Forward(x []float32) ([]float32, error) {
	outputs, err := l.IdentityLayer.Forward(x)
	if err != nil {
		return nil, err
	}

	for idx, val := range outputs {
		outputs[idx] = float32(math.Tanh(float64(val)))
	}

	return outputs, nil
}
开发者ID:danieldk,项目名称:dparnn,代码行数:14,代码来源:layers.go


注:本文中的math.Tanh函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。