Example #1
0
func NewRBM(visibleCount, hiddenCount int) *RBM {
	return &RBM{
		Weights:       linalg.NewMatrix(hiddenCount, visibleCount),
		HiddenBiases:  make(linalg.Vector, hiddenCount),
		VisibleBiases: make(linalg.Vector, visibleCount),
	}
}
Example #2
0
// NewNPRNN creates an np-RNN with the given input
// and hidden sizes.
func NewNPRNN(inputSize, hiddenSize int) NPRNN {
	matrix := &neuralnet.DenseLayer{
		InputCount:  inputSize + hiddenSize,
		OutputCount: hiddenSize,
		Biases: &autofunc.LinAdd{
			Var: &autofunc.Variable{
				Vector: make(linalg.Vector, hiddenSize),
			},
		},
		Weights: &autofunc.LinTran{
			Rows: hiddenSize,
			Cols: inputSize + hiddenSize,
			Data: &autofunc.Variable{
				Vector: make(linalg.Vector, hiddenSize*(inputSize+hiddenSize)),
			},
		},
	}
	weights := matrix.Weights.Data.Vector

	normalMat := linalg.NewMatrix(hiddenSize, hiddenSize)
	for i := range normalMat.Data {
		normalMat.Data[i] = rand.NormFloat64()
	}
	normalMat = normalMat.Mul(normalMat.Transpose())
	normalMat.Scale(1 / float64(hiddenSize))
	for i := 0; i < hiddenSize; i++ {
		normalMat.Set(i, i, 1+normalMat.Get(i, i))
	}
	normalMat.Scale(1 / maxEigenvalue(normalMat))

	for i := 0; i < hiddenSize; i++ {
		for j := 0; j < hiddenSize; j++ {
			x := normalMat.Get(i, j)
			weights[j+inputSize+i*(hiddenSize+inputSize)] = x
		}
	}

	weightScale := 1 / math.Sqrt(float64(inputSize))
	for j := 0; j < hiddenSize; j++ {
		for i := 0; i < inputSize; i++ {
			weights[i+j*(inputSize+hiddenSize)] = (rand.Float64()*2 - 1) * weightScale
		}
	}

	network := neuralnet.Network{
		matrix,
		&neuralnet.ReLU{},
	}

	return &StateOutBlock{
		Block: NewNetworkBlock(network, hiddenSize),
	}
}
Example #3
0
func newGradientIterator(p *Problem, maxCoeff float64) *gradientIterator {
	varCount := len(p.Positives) + len(p.Negatives)
	posCount := len(p.Positives)
	signVec := make(linalg.Vector, varCount)

	for i := 0; i < len(p.Positives); i++ {
		signVec[i] = 1
	}
	for i := 0; i < len(p.Negatives); i++ {
		signVec[i+posCount] = -1
	}

	res := &gradientIterator{
		activeSet: newActiveSet(signVec, maxCoeff),
		matrix:    linalg.NewMatrix(varCount, varCount),
		solution:  make(linalg.Vector, varCount),
	}

	for i := 0; i < varCount; i++ {
		var iSample Sample
		if i >= posCount {
			iSample = p.Negatives[i-posCount]
		} else {
			iSample = p.Positives[i]
		}
		res.matrix.Set(i, i, p.Kernel(iSample, iSample))
		for j := 0; j < i; j++ {
			var jSample Sample
			if j >= posCount {
				jSample = p.Negatives[j-posCount]
			} else {
				jSample = p.Positives[j]
			}
			product := p.Kernel(iSample, jSample)
			res.matrix.Set(i, j, product)
			res.matrix.Set(j, i, product)
		}
	}

	for i := 0; i < varCount; i++ {
		for j := 0; j < varCount; j++ {
			oldVal := res.matrix.Get(i, j)
			oldVal *= signVec[i]
			oldVal *= signVec[j]
			res.matrix.Set(i, j, oldVal)
		}
	}

	res.updateCaches()

	return res
}