Ejemplo n.º 1
0
func main() {
	rand.Seed(time.Now().UnixNano())

	sampleSet := sgd.SliceSampleSet{}
	for i := 0; i < TrainingCount; i++ {
		inSeq, outSeq := genEvenOddSeq(rand.Intn(MaxSeqLen-MinSeqLen) + MinSeqLen)
		sampleSet = append(sampleSet, seqtoseq.Sample{
			Inputs:  inSeq,
			Outputs: outSeq,
		})
	}

	outNet := neuralnet.Network{
		&neuralnet.DenseLayer{
			InputCount:  HiddenSize,
			OutputCount: 2,
		},
	}
	outNet.Randomize()
	outBlock := rnn.NewNetworkBlock(outNet, 0)
	lstm := rnn.NewLSTM(2, HiddenSize)
	net := rnn.StackedBlock{lstm, outBlock}

	gradienter := &sgd.RMSProp{
		Gradienter: &seqtoseq.Gradienter{
			SeqFunc:  &rnn.BlockSeqFunc{B: net},
			Learner:  net,
			CostFunc: neuralnet.SigmoidCECost{},
			MaxLanes: 1,
		},
	}

	sgd.SGD(gradienter, sampleSet, StepSize, Epochs, BatchSize)

	outNet = append(outNet, neuralnet.Sigmoid{})

	var scoreSum float64
	var scoreTotal float64
	for i := 0; i < TestingCount; i++ {
		size := rand.Intn(MaxSeqLen-MinSeqLen) + MinSeqLen
		ins, outs := genEvenOddSeq(size)
		score := runTestSample(ins, outs, net)
		scoreSum += score
		scoreTotal += 1
	}

	fmt.Println("Testing success rate:", scoreSum/scoreTotal)
}
Ejemplo n.º 2
0
func TestLSTM(t *testing.T) {
	b := rnn.NewLSTM(4, 2)
	NewChecker4In(b, b).FullCheck(t)
}