本文整理匯總了Golang中github.com/unixpickle/weakai/neuralnet.Network.Apply方法的典型用法代碼示例。如果您正苦於以下問題:Golang Network.Apply方法的具體用法?Golang Network.Apply怎麽用?Golang Network.Apply使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/unixpickle/weakai/neuralnet.Network
的用法示例。
在下文中一共展示了Network.Apply方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: printScore
func printScore(prefix string, n neuralnet.Network, d mnist.DataSet) {
classifier := func(v []float64) int {
r := n.Apply(&autofunc.Variable{v})
return networkOutput(r)
}
correctCount := d.NumCorrect(classifier)
histogram := d.CorrectnessHistogram(classifier)
log.Printf("%s: %d/%d - %s", prefix, correctCount, len(d.Samples), histogram)
}
示例2: firstBitTest
// firstBitTest builds a neural network to:
// - output 0 for inputs starting with a 1
// - output 1 for inputs starting with a 0.
func firstBitTest() {
trainingSamples := make([]linalg.Vector, FirstBitTrainingSize)
trainingOutputs := make([]linalg.Vector, FirstBitTrainingSize)
for i := range trainingSamples {
trainingSamples[i] = make(linalg.Vector, FirstBitInputSize)
for j := range trainingSamples[i] {
trainingSamples[i][j] = float64(rand.Intn(2))
}
trainingOutputs[i] = []float64{1 - trainingSamples[i][0]}
}
samples := neuralnet.VectorSampleSet(trainingSamples, trainingOutputs)
network := neuralnet.Network{
&neuralnet.DenseLayer{
InputCount: FirstBitInputSize,
OutputCount: FirstBitHiddenSize,
},
&neuralnet.Sigmoid{},
&neuralnet.DenseLayer{
InputCount: FirstBitHiddenSize,
OutputCount: 1,
},
&neuralnet.Sigmoid{},
}
network.Randomize()
batcher := &neuralnet.SingleRGradienter{
Learner: network,
CostFunc: neuralnet.MeanSquaredCost{},
}
sgd.SGD(batcher, samples, 0.2, 100000, 1)
var totalError float64
var maxPossibleError float64
for i := 0; i < 50; i++ {
sample := make([]float64, FirstBitInputSize)
for j := range sample {
sample[j] = float64(rand.Intn(2))
}
result := network.Apply(&autofunc.Variable{sample})
output := result.Output()[0]
amountError := math.Abs(output - (1 - sample[0]))
totalError += amountError
maxPossibleError += 1.0
}
fmt.Printf("firstBitTest() error rate: %f\n", totalError/maxPossibleError)
}
示例3: runHorizontalLineTest
func runHorizontalLineTest(name string, network neuralnet.Network) {
trainingSamples := make([]linalg.Vector, GridTrainingSize)
trainingOutputs := make([]linalg.Vector, GridTrainingSize)
for i := range trainingSamples {
trainingSamples[i] = randomBitmap()
if bitmapHasHorizontal(trainingSamples[i]) {
trainingOutputs[i] = []float64{1}
} else {
trainingOutputs[i] = []float64{0}
}
}
samples := neuralnet.VectorSampleSet(trainingSamples, trainingOutputs)
network.Randomize()
batcher := &neuralnet.SingleRGradienter{
Learner: network,
CostFunc: neuralnet.MeanSquaredCost{},
}
sgd.SGD(batcher, samples, 0.1, 1000, 100)
var trainingError float64
var maxTrainingError float64
for i, sample := range trainingSamples {
result := network.Apply(&autofunc.Variable{sample})
output := result.Output()[0]
amountError := math.Abs(output - trainingOutputs[i][0])
trainingError += amountError
maxTrainingError += 1.0
}
var totalError float64
var maxPossibleError float64
for i := 0; i < 50; i++ {
sample := randomBitmap()
var expected float64
if bitmapHasHorizontal(sample) {
expected = 1
}
result := network.Apply(&autofunc.Variable{sample})
output := result.Output()[0]
amountError := math.Abs(output - expected)
totalError += amountError
maxPossibleError += 1.0
}
fmt.Printf("%s() training error: %f; cross error: %f\n", name,
trainingError/maxTrainingError, totalError/maxPossibleError)
}
示例4: countCorrect
func countCorrect(n neuralnet.Network, s sgd.SampleSet) int {
var count int
for i := 0; i < s.Len(); i++ {
sample := s.GetSample(i).(neuralnet.VectorSample)
output := n.Apply(&autofunc.Variable{Vector: sample.Input}).Output()
var maxIdx int
var maxVal float64
for j, x := range output {
if x > maxVal || j == 0 {
maxIdx = j
maxVal = x
}
}
if sample.Output[maxIdx] == 1 {
count++
}
}
return count
}