예제 #1
0
파일: util.go 프로젝트: unixpickle/weakai
func costFuncDeriv(c neuralnet.CostFunc, expected, actual linalg.Vector) linalg.Vector {
	variable := &autofunc.Variable{Vector: actual}
	result := make(linalg.Vector, len(actual))
	res := c.Cost(expected, variable)
	res.PropagateGradient([]float64{1}, autofunc.Gradient{variable: result})
	return result
}
예제 #2
0
파일: util.go 프로젝트: unixpickle/weakai
func costFuncRDeriv(c neuralnet.CostFunc, expected, actual,
	actualR linalg.Vector) (deriv, rDeriv linalg.Vector) {
	variable := &autofunc.RVariable{
		Variable:   &autofunc.Variable{Vector: actual},
		ROutputVec: actualR,
	}
	deriv = make(linalg.Vector, len(actual))
	rDeriv = make(linalg.Vector, len(actual))
	res := c.CostR(autofunc.RVector{}, expected, variable)
	res.PropagateRGradient([]float64{1}, []float64{0},
		autofunc.RGradient{variable.Variable: rDeriv},
		autofunc.Gradient{variable.Variable: deriv})
	return
}
예제 #3
0
파일: cost.go 프로젝트: unixpickle/weakai
// TotalCostBlock runs an rnn.Block on a set of Samples
// and evaluates the total output cost.
//
// The batchSize specifies how many samples to run in
// batches while computing the cost.
func TotalCostBlock(b rnn.Block, batchSize int, s sgd.SampleSet, c neuralnet.CostFunc) float64 {
	runner := &rnn.Runner{Block: b}

	var cost float64
	for i := 0; i < s.Len(); i += batchSize {
		var inSeqs, outSeqs [][]linalg.Vector
		for j := i; j < i+batchSize && j < s.Len(); j++ {
			seq := s.GetSample(j).(Sample)
			inSeqs = append(inSeqs, seq.Inputs)
			outSeqs = append(outSeqs, seq.Outputs)
		}
		output := runner.RunAll(inSeqs)
		for j, outSeq := range outSeqs {
			for t, actual := range output[j] {
				expected := outSeq[t]
				actualVar := &autofunc.Variable{Vector: actual}
				cost += c.Cost(expected, actualVar).Output()[0]
			}
		}
	}
	return cost
}
예제 #4
0
파일: cost.go 프로젝트: unixpickle/weakai
// TotalCostSeqFunc runs a seqfunc.RFunc on a set of
// Samples and evaluates the total output cost.
//
// The batchSize specifies how many samples to run in
// batches while computing the cost.
func TotalCostSeqFunc(f seqfunc.RFunc, batchSize int, s sgd.SampleSet,
	c neuralnet.CostFunc) float64 {
	var totalCost float64
	for i := 0; i < s.Len(); i += batchSize {
		var inSeqs [][]linalg.Vector
		var outSeqs [][]linalg.Vector
		for j := i; j < i+batchSize && j < s.Len(); j++ {
			seq := s.GetSample(j).(Sample)
			inSeqs = append(inSeqs, seq.Inputs)
			outSeqs = append(outSeqs, seq.Outputs)
		}
		output := f.ApplySeqs(seqfunc.ConstResult(inSeqs))
		for j, actualSeq := range output.OutputSeqs() {
			expectedSeq := outSeqs[j]
			for k, actual := range actualSeq {
				expected := expectedSeq[k]
				actualVar := &autofunc.Variable{Vector: actual}
				totalCost += c.Cost(expected, actualVar).Output()[0]
			}
		}
	}
	return totalCost
}