コード例 #1
0
ファイル: network_block.go プロジェクト: unixpickle/weakai
// NewNetworkBlock creates a NetworkBlock.
func NewNetworkBlock(n neuralnet.Network, stateSize int) *NetworkBlock {
	return &NetworkBlock{
		batcherBlock: &BatcherBlock{
			B:         n.BatchLearner(),
			StateSize: stateSize,
			Start:     &autofunc.Variable{Vector: make(linalg.Vector, stateSize)},
		},
		network: n,
	}
}
コード例 #2
0
func TestStackedBlock(t *testing.T) {
	testVars := []*autofunc.Variable{
		{Vector: []float64{0.098591, -0.595453, -0.751214, 0.266051}},
		{Vector: []float64{0.988517, 0.107284, -0.331529, 0.028565}},
		{Vector: []float64{-0.150604, 0.889039, 0.120916, 0.240999}},
		{Vector: []float64{0.961058, 0.878608, 0.052284, -0.635746}},
		{Vector: []float64{0.31415, -0.2718}},
		{Vector: []float64{-0.6}},
	}
	testSeqs := [][]*autofunc.Variable{
		{testVars[0], testVars[2]},
		{testVars[1]},
		{testVars[2], testVars[1], testVars[3]},
	}
	testRV := autofunc.RVector{
		testVars[0]: []float64{0.62524, 0.52979, 0.33020, 0.54462},
		testVars[1]: []float64{0.13498, 0.12607, 0.35989, 0.23255},
		testVars[2]: []float64{0.85996, 0.68435, 0.68506, 0.96907},
		testVars[3]: []float64{0.79095, 0.33867, 0.86759, 0.16159},
		testVars[4]: []float64{-0.79095, 0.33867},
		testVars[5]: []float64{0.33867},
	}
	net1 := neuralnet.Network{
		&neuralnet.DenseLayer{
			InputCount:  6,
			OutputCount: 6,
		},
		&neuralnet.HyperbolicTangent{},
	}
	net1.Randomize()
	net2 := neuralnet.Network{
		&neuralnet.DenseLayer{
			InputCount:  5,
			OutputCount: 5,
		},
		&neuralnet.HyperbolicTangent{},
	}
	net2.Randomize()
	block := &rnn.StackedBlock{
		&rnn.BatcherBlock{B: net1.BatchLearner(), StateSize: 2, Start: testVars[4]},
		&rnn.BatcherBlock{B: net2.BatchLearner(), StateSize: 1, Start: testVars[5]},
	}
	checker := &BlockChecker{
		B:     block,
		Input: testSeqs,
		Vars:  testVars,
		RV:    testRV,
	}
	checker.FullCheck(t)
}
コード例 #3
0
func TestStateOutBlock(t *testing.T) {
	net := neuralnet.Network{
		&neuralnet.DenseLayer{
			InputCount:  8,
			OutputCount: 4,
		},
		&neuralnet.HyperbolicTangent{},
	}
	net.Randomize()
	startVar := &autofunc.Variable{Vector: []float64{0.3, -0.3, 0.2, 0.5}}
	block := &rnn.StateOutBlock{
		Block: &rnn.BatcherBlock{
			B:         net.BatchLearner(),
			StateSize: 4,
			Start:     startVar,
		},
	}
	learner := append(stateOutBlockLearner{startVar}, net.Parameters()...)
	NewChecker4In(block, learner).FullCheck(t)
}
コード例 #4
0
ファイル: baseline_test.go プロジェクト: unixpickle/weakai
func TestBaselineChecks(t *testing.T) {
	network := neuralnet.Network{
		&neuralnet.DenseLayer{
			InputCount:  4,
			OutputCount: 6,
		},
		neuralnet.HyperbolicTangent{},
	}
	network.Randomize()

	for stateSize := 0; stateSize < 4; stateSize++ {
		start := &autofunc.Variable{Vector: make(linalg.Vector, stateSize)}
		for i := range start.Vector {
			start.Vector[i] = rand.NormFloat64()
		}
		toTest := &rnn.BlockSeqFunc{
			B: &rnn.BatcherBlock{
				B:         network.BatchLearner(),
				StateSize: stateSize,
				Start:     start,
			},
		}
		seqs, rv := randBaselineTestSeqs(network, 4-stateSize)
		rv[start] = make(linalg.Vector, len(start.Vector))
		for i := range rv[start] {
			rv[start][i] = rand.NormFloat64()
		}
		vars := make([]*autofunc.Variable, 0, len(rv))
		for v := range rv {
			vars = append(vars, v)
		}
		checker := &functest.SeqRFuncChecker{
			F:     toTest,
			Vars:  vars,
			Input: seqs,
			RV:    rv,
		}
		checker.FullCheck(t)
	}
}
コード例 #5
0
ファイル: main.go プロジェクト: unixpickle/weakai
func trainClassifier(n neuralnet.Network, d mnist.DataSet) {
	log.Println("Training classifier (ctrl+C to finish)...")

	killChan := make(chan struct{})

	go func() {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt)
		<-c
		signal.Stop(c)
		fmt.Println("\nCaught interrupt. Ctrl+C again to terminate.")
		close(killChan)
	}()

	inputs := make([]linalg.Vector, len(d.Samples))
	outputs := make([]linalg.Vector, len(d.Samples))
	for i, x := range d.IntensityVectors() {
		inputs[i] = x
	}
	for i, x := range d.LabelVectors() {
		outputs[i] = x
	}
	samples := neuralnet.VectorSampleSet(inputs, outputs)
	batcher := &neuralnet.BatchRGradienter{
		Learner:  n.BatchLearner(),
		CostFunc: neuralnet.MeanSquaredCost{},
	}

	crossValidation := mnist.LoadTestingDataSet()

	sgd.SGDInteractive(batcher, samples, ClassifierStepSize,
		ClassifierBatchSize, func() bool {
			printScore("Training", n, d)
			printScore("Cross", n, crossValidation)
			return true
		})
}
コード例 #6
0
ファイル: train.go プロジェクト: unixpickle/weakai
func TrainCmd(netPath, dirPath string) {
	log.Println("Loading samples...")
	images, width, height, err := LoadTrainingImages(dirPath)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	log.Println("Creating network...")

	var network neuralnet.Network
	networkData, err := ioutil.ReadFile(netPath)
	if err == nil {
		network, err = neuralnet.DeserializeNetwork(networkData)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Failed to load network:", err)
			os.Exit(1)
		}
		log.Println("Loaded network from file.")
	} else {
		mean, stddev := sampleStatistics(images)
		convLayer := &neuralnet.ConvLayer{
			FilterCount:  FilterCount,
			FilterWidth:  4,
			FilterHeight: 4,
			Stride:       2,

			InputWidth:  width,
			InputHeight: height,
			InputDepth:  ImageDepth,
		}
		maxLayer := &neuralnet.MaxPoolingLayer{
			XSpan:       3,
			YSpan:       3,
			InputWidth:  convLayer.OutputWidth(),
			InputHeight: convLayer.OutputHeight(),
			InputDepth:  convLayer.OutputDepth(),
		}
		convLayer1 := &neuralnet.ConvLayer{
			FilterCount:  FilterCount1,
			FilterWidth:  3,
			FilterHeight: 3,
			Stride:       2,

			InputWidth:  maxLayer.OutputWidth(),
			InputHeight: maxLayer.OutputHeight(),
			InputDepth:  maxLayer.InputDepth,
		}
		network = neuralnet.Network{
			&neuralnet.RescaleLayer{
				Bias:  -mean,
				Scale: 1 / stddev,
			},
			convLayer,
			neuralnet.HyperbolicTangent{},
			maxLayer,
			neuralnet.HyperbolicTangent{},
			convLayer1,
			neuralnet.HyperbolicTangent{},
			&neuralnet.DenseLayer{
				InputCount: convLayer1.OutputWidth() * convLayer1.OutputHeight() *
					convLayer1.OutputDepth(),
				OutputCount: HiddenSize,
			},
			neuralnet.HyperbolicTangent{},
			&neuralnet.DenseLayer{
				InputCount:  HiddenSize,
				OutputCount: len(images),
			},
			&neuralnet.LogSoftmaxLayer{},
		}
		network.Randomize()
		log.Println("Created new network.")
	}

	samples := neuralSamples(images)
	sgd.ShuffleSampleSet(samples)

	validationCount := int(ValidationFraction * float64(samples.Len()))
	validationSamples := samples.Subset(0, validationCount)
	trainingSamples := samples.Subset(validationCount, samples.Len())

	costFunc := neuralnet.DotCost{}
	gradienter := &sgd.Adam{
		Gradienter: &neuralnet.BatchRGradienter{
			Learner: network.BatchLearner(),
			CostFunc: &neuralnet.RegularizingCost{
				Variables: network.Parameters(),
				Penalty:   Regularization,
				CostFunc:  costFunc,
			},
		},
	}
	sgd.SGDInteractive(gradienter, trainingSamples, StepSize, BatchSize, func() bool {
		log.Printf("Costs: validation=%d/%d cost=%f",
			countCorrect(network, validationSamples), validationSamples.Len(),
			neuralnet.TotalCost(costFunc, network, trainingSamples))
		return true
	})

	data, _ := network.Serialize()
	if err := ioutil.WriteFile(netPath, data, 0755); err != nil {
		fmt.Fprintln(os.Stderr, "Failed to save:", err)
		os.Exit(1)
	}
}
コード例 #7
0
ファイル: baseline_test.go プロジェクト: unixpickle/weakai
// TestBaselineOutput makes sure that the BatcherBlock +
// BlockSeqFunc combo produces the right output, since
// that combo will be used for the rest of the tests.
func TestBaselineOutput(t *testing.T) {
	network := neuralnet.Network{
		&neuralnet.DenseLayer{
			InputCount:  4,
			OutputCount: 6,
		},
		neuralnet.HyperbolicTangent{},
	}
	network.Randomize()

	for stateSize := 0; stateSize < 4; stateSize++ {
		start := &autofunc.Variable{Vector: make(linalg.Vector, stateSize)}
		for i := range start.Vector {
			start.Vector[i] = rand.NormFloat64()
		}
		toTest := rnn.BlockSeqFunc{
			B: &rnn.BatcherBlock{
				B:         network.BatchLearner(),
				StateSize: stateSize,
				Start:     start,
			},
		}
		seqs, rv := randBaselineTestSeqs(network, 4-stateSize)
		rv[start] = make(linalg.Vector, len(start.Vector))
		for i := range rv[start] {
			rv[start][i] = rand.NormFloat64()
		}
		res := toTest.ApplySeqsR(rv, seqfunc.VarRResult(rv, seqs))
		actual := res.OutputSeqs()
		actualR := res.ROutputSeqs()
		expected, expectedR := manualNetworkSeq(rv, network, start, seqs, stateSize)
		if len(expected) != len(actual) {
			t.Errorf("stateSize %d: len(expected) [%d] != len(actual) [%d]", stateSize,
				len(expected), len(actual))
			continue
		}
		for i, act := range actual {
			actR := actualR[i]
			exp := expected[i]
			expR := expectedR[i]
			if len(act) != len(exp) {
				t.Errorf("stateSize %d seq %d: len(act) [%d] != len(exp) [%d]",
					stateSize, i, len(act), len(act))
				continue
			}
			for j, a := range act {
				x := exp[j]
				if len(a) != len(x) || x.Copy().Scale(-1).Add(a).MaxAbs() > 1e-5 {
					t.Errorf("stateSize %d seq %d entry %d: expected %v got %v",
						stateSize, i, j, x, a)
				}
			}
			for j, a := range actR {
				x := expR[j]
				if len(a) != len(x) || x.Copy().Scale(-1).Add(a).MaxAbs() > 1e-5 {
					t.Errorf("stateSize %d seq %d entry %d (R): expected %v got %v",
						stateSize, i, j, x, a)
				}
			}
		}
	}
}