Use MakeAlias() instead.

This commit is contained in:
Ryan Curtin
2022-04-03 14:26:05 -04:00
parent c599ecfd1a
commit 2f0a9419ef
6 changed files with 39 additions and 52 deletions
+1 -1
View File
@@ -95,7 +95,7 @@ template<typename MatType>
void AddType<MatType>::SetWeights(typename MatType::elem_type* weightPtr)
{
// Set the weights to wrap the given memory.
weights = MatType(weightPtr, 1, outSize, false, true);
MakeAlias(weights, weightPtr, 1, outSize);
}
template<typename MatType>
@@ -89,11 +89,10 @@ template<typename MatType, typename RegularizerType>
void Linear3DType<MatType, RegularizerType>::SetWeights(
typename MatType::elem_type* weightsPtr)
{
weights = MatType(weightsPtr, outSize * this->inputDimensions[0] + outSize,
1, false, false);
weight = MatType(weightsPtr, outSize, this->inputDimensions[0], false,
false);
bias = MatType(weightsPtr + weight.n_elem, outSize, 1, false, false);
MakeAlias(weights, weightsPtr, outSize * this->inputDimensions[0] + outSize,
1);
MakeAlias(weight, weightsPtr, outSize, this->inputDimensions[0]);
MakeAlias(bias, weightsPtr + weight.n_elem, outSize, 1);
}
template<typename MatType, typename RegularizerType>
+3 -3
View File
@@ -97,9 +97,9 @@ template<typename MatType, typename RegularizerType>
void LinearType<MatType, RegularizerType>::SetWeights(
typename MatType::elem_type* weightsPtr)
{
weights = MatType(weightsPtr, outSize * inSize + outSize, 1, false, false);
weight = MatType(weightsPtr, outSize, inSize, false, false);
bias = MatType(weightsPtr + weight.n_elem, outSize, 1, false, false);
MakeAlias(weights, weightsPtr, outSize * inSize + outSize, 1);
MakeAlias(weight, weightsPtr, outSize, inSize);
MakeAlias(bias, weightsPtr + weight.n_elem, outSize, 1);
}
template<typename MatType, typename RegularizerType>
@@ -98,7 +98,7 @@ template<typename MatType, typename RegularizerType>
void LinearNoBiasType<MatType, RegularizerType>::SetWeights(
typename MatType::elem_type* weightsPtr)
{
weight = arma::mat(weightsPtr, outSize, inSize, false, false);
MakeAlias(weight, weightsPtr, outSize, inSize);
}
template<typename MatType, typename RegularizerType>
+23 -34
View File
@@ -98,66 +98,55 @@ void LSTMType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
{
// Set the weight parameter for the output gate.
input2GateOutputWeight = MatType(weightsPtr, outSize, inSize,
false, false);
input2GateOutputBias = MatType(weightsPtr + input2GateOutputWeight.n_elem,
outSize, 1, false, false);
size_t offset = input2GateOutputWeight.n_elem + input2GateOutputBias.n_elem;
MakeAlias(input2GateOutputWeight, weightsPtr, outSize, inSize);
size_t offset = input2GateOutputWeight.n_elem;
MakeAlias(input2GateOutputBias, weightsPtr + offset, outSize, 1);
offset += input2GateOutputBias.n_elem;
// Set the weight parameter for the forget gate.
input2GateForgetWeight = MatType(weightsPtr + offset, outSize, inSize,
false, false);
input2GateForgetBias = MatType(weightsPtr + offset +
input2GateForgetWeight.n_elem, outSize, 1, false, false);
offset += input2GateForgetWeight.n_elem + input2GateForgetBias.n_elem;
MakeAlias(input2GateForgetWeight, weightsPtr + offset, outSize, inSize);
offset += input2GateForgetWeight.n_elem;
MakeAlias(input2GateForgetBias, weightsPtr + offset, outSize, 1);
offset += input2GateForgetBias.n_elem;
// Set the weight parameter for the input gate.
input2GateInputWeight = MatType(weightsPtr + offset, outSize, inSize,
false, false);
input2GateInputBias = MatType(weightsPtr + offset +
input2GateInputWeight.n_elem, outSize, 1, false, false);
offset += input2GateInputWeight.n_elem + input2GateInputBias.n_elem;
MakeAlias(input2GateInputWeight, weightsPtr + offset, outSize, inSize);
offset += input2GateInputWeight.n_elem;
MakeAlias(input2GateInputBias, weightsPtr + offset, outSize, 1);
offset += input2GateInputBias.n_elem;
// Set the weight parameter for the hidden gate.
input2HiddenWeight = MatType(weightsPtr + offset, outSize, inSize, false,
false);
input2HiddenBias = MatType(weightsPtr + offset + input2HiddenWeight.n_elem,
outSize, 1, false, false);
offset += input2HiddenWeight.n_elem + input2HiddenBias.n_elem;
MakeAlias(input2HiddenWeight, weightsPtr + offset, outSize, inSize);
offset += input2HiddenWeight.n_elem;
MakeAlias(input2HiddenBias, weightsPtr + offset, outSize, 1);
offset += input2HiddenBias.n_elem;
// Set the weight parameter for the output multiplication.
output2GateOutputWeight = MatType(weightsPtr + offset, outSize, outSize,
false, false);
MakeAlias(output2GateOutputWeight, weightsPtr + offset, outSize, outSize);
offset += output2GateOutputWeight.n_elem;
// Set the weight parameter for the output multiplication.
output2GateForgetWeight = MatType(weightsPtr + offset, outSize, outSize,
false, false);
MakeAlias(output2GateForgetWeight, weightsPtr + offset, outSize, outSize);
offset += output2GateForgetWeight.n_elem;
// Set the weight parameter for the input multiplication.
output2GateInputWeight = MatType(weightsPtr + offset, outSize, outSize,
false, false);
MakeAlias(output2GateInputWeight, weightsPtr + offset, outSize, outSize);
offset += output2GateInputWeight.n_elem;
// Set the weight parameter for the hidden multiplication.
output2HiddenWeight = MatType(weightsPtr + offset, outSize, outSize, false,
false);
MakeAlias(output2HiddenWeight, weightsPtr + offset, outSize, outSize);
offset += output2HiddenWeight.n_elem;
// Set the weight parameter for the cell multiplication.
cell2GateOutputWeight = MatType(weightsPtr + offset, outSize, 1, false,
false);
MakeAlias(cell2GateOutputWeight, weightsPtr + offset, outSize, 1);
offset += cell2GateOutputWeight.n_elem;
// Set the weight parameter for the cell - forget gate multiplication.
cell2GateForgetWeight = MatType(weightsPtr + offset, outSize, 1, false,
false);
MakeAlias(cell2GateForgetWeight, weightsPtr + offset, outSize, 1);
offset += cell2GateOutputWeight.n_elem;
// Set the weight parameter for the cell - input gate multiplication.
cell2GateInputWeight = MatType(weightsPtr + offset, outSize, 1, false,
false);
MakeAlias(cell2GateInputWeight, weightsPtr + offset, outSize, 1);
}
// Forward when cellState is not needed.
@@ -77,15 +77,14 @@ template<typename MatType>
void NoisyLinearType<MatType>::SetWeights(
typename MatType::elem_type* weightsPtr)
{
weights = MatType(weightsPtr, 1, (outSize * inSize + outSize) * 2, false,
true);
MakeAlias(weights, weightsPtr, 1, (outSize * inSize + outSize) * 2);
weightMu = MatType(weightsPtr, outSize, inSize, false, true);
biasMu = MatType(weightsPtr + weightMu.n_elem, outSize, 1, false, true);
weightSigma = MatType(weightsPtr + weightMu.n_elem + biasMu.n_elem,
outSize, inSize, false, true);
biasSigma = MatType(weightsPtr + weightMu.n_elem * 2 + biasMu.n_elem,
outSize, 1, false, true);
MakeAlias(weightMu, weightsPtr, outSize, inSize);
MakeAlias(biasMu, weightsPtr + weightMu.n_elem, outSize, 1);
MakeAlias(weightSigma, weightsPtr + weightMu.n_elem + biasMu.n_elem, outSize,
inSize);
MakeAlias(biasSigma, weightsPtr + weightMu.n_elem * 2 + biasMu.n_elem,
outSize, 1);
this->ResetNoise();
}