Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Generative Adversarial Networks (GANs) #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tmva/tmva/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ set(headers1 Configurable.h Factory.h MethodBase.h MethodCompositeBase.h
MethodKNN.h MethodCFMlpANN.h MethodCFMlpANN_Utils.h MethodLikelihood.h
MethodHMatrix.h MethodPDERS.h MethodBDT.h MethodDT.h MethodSVM.h MethodBayesClassifier.h
MethodFDA.h MethodMLP.h MethodBoost.h
MethodPDEFoam.h MethodLD.h MethodCategory.h MethodDNN.h MethodDL.h
MethodPDEFoam.h MethodLD.h MethodCategory.h MethodDNN.h MethodDL.h MethodGAN.h
MethodCrossValidation.h)
set(headers2 TSpline2.h TSpline1.h PDF.h BinaryTree.h BinarySearchTreeNode.h BinarySearchTree.h
Timer.h RootFinder.h CrossEntropy.h DecisionTree.h DecisionTreeNode.h MisClassificationError.h
Expand Down
1 change: 1 addition & 0 deletions tmva/tmva/inc/LinkDef1.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@
#pragma link C++ class TMVA::MethodDNN+;
#pragma link C++ class TMVA::MethodCrossValidation+;
#pragma link C++ class TMVA::MethodDL+;
#pragma link C++ class TMVA::MethodGAN+;

#endif
36 changes: 18 additions & 18 deletions tmva/tmva/inc/TMVA/DNN/CNN/ConvLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TConvLayer : public VGeneralLayer<Architecture_t> {

std::vector<int> fForwardIndices; ///< Vector of indices used for a fast Im2Col in forward pass
std::vector<int> fBackwardIndices; ///< Vector of indices used for a fast Im2Col in backward pass


EActivationFunction fF; ///< Activation function of the layer.
ERegularization fReg; ///< The regularization method.
Expand Down Expand Up @@ -152,7 +152,7 @@ TConvLayer<Architecture_t>::TConvLayer(size_t batchSize, size_t inputDepth, size
size_t filterWidth, size_t strideRows, size_t strideCols, size_t paddingHeight,
size_t paddingWidth, Scalar_t dropoutProbability, EActivationFunction f,
ERegularization reg, Scalar_t weightDecay)
: VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, depth, height, width, 1,
: VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, depth, height, width, "CONV", 1,
weightsNRows, weightsNCols, 1, biasesNRows, biasesNCols, outputNSlices, outputNRows,
outputNCols, init),
fFilterDepth(filterDepth), fFilterHeight(filterHeight), fFilterWidth(filterWidth), fStrideRows(strideRows),
Expand Down Expand Up @@ -220,43 +220,43 @@ auto TConvLayer<Architecture_t>::Forward(std::vector<Matrix_t> &input, bool appl

fForwardIndices.resize(this->GetNLocalViews() * this->GetNLocalViewPixels() );

R__ASSERT( input.size() > 0);
R__ASSERT( input.size() > 0);
Architecture_t::Im2colIndices(fForwardIndices, input[0], this->GetNLocalViews(), this->GetInputHeight(), this->GetInputWidth(), this->GetFilterHeight(),
this->GetFilterWidth(), this->GetStrideRows(), this->GetStrideCols(),
this->GetPaddingHeight(), this->GetPaddingWidth());


Architecture_t::ConvLayerForward(this->GetOutput(), this->GetDerivatives(), input, this->GetWeightsAt(0), this->GetBiasesAt(0),
fF, fForwardIndices, this->GetNLocalViews(), this->GetNLocalViewPixels(),
this->GetDropoutProbability(), applyDropout );
this->GetDropoutProbability(), applyDropout );

#if 0
#if 0
// in printciple I could make the indices data member of the class
Matrix_t inputTr(this->GetNLocalViews(), this->GetNLocalViewPixels());
//Matrix_t inputTr2(this->GetNLocalViews(), this->GetNLocalViewPixels());
std::vector<int> vIndices(inputTr.GetNrows() * inputTr.GetNcols() );
R__ASSERT( input.size() > 0);
R__ASSERT( input.size() > 0);
Architecture_t::Im2colIndices(vIndices, input[0], this->GetNLocalViews(), this->GetInputHeight(), this->GetInputWidth(), this->GetFilterHeight(),
this->GetFilterWidth(), this->GetStrideRows(), this->GetStrideCols(),
this->GetPaddingHeight(), this->GetPaddingWidth());
// batch size loop
// batch size loop
for (size_t i = 0; i < this->GetBatchSize(); i++) {

if (applyDropout && (this->GetDropoutProbability() != 1.0)) {
Architecture_t::Dropout(input[i], this->GetDropoutProbability());
}

inputTr.Zero();
//inputTr2.Zero();
inputTr.Zero();
//inputTr2.Zero();
// Architecture_t::Im2col(inputTr2, input[i], this->GetInputHeight(), this->GetInputWidth(), this->GetFilterHeight(),
// this->GetFilterWidth(), this->GetStrideRows(), this->GetStrideCols(),
// this->GetPaddingHeight(), this->GetPaddingWidth());
Architecture_t::Im2colFast(inputTr, input[i], vIndices);
// bool diff = false;
// for (int j = 0; j < inputTr.GetNrows(); ++j) {
// bool diff = false;
// for (int j = 0; j < inputTr.GetNrows(); ++j) {
// for (int k = 0; k < inputTr.GetNcols(); ++k) {
// if ( inputTr2(j,k) != inputTr(j,k) ) {
// diff = true;
// diff = true;
// std::cout << "different im2col for " << j << " , " << k << " " << inputTr(j,k) << " shoud be " << inputTr2(j,k) << std::endl;
// }
// }
Expand All @@ -268,15 +268,15 @@ auto TConvLayer<Architecture_t>::Forward(std::vector<Matrix_t> &input, bool appl
// this->GetPaddingHeight(), this->GetPaddingWidth() );
// // PrintMatrix(inputTr);
// //PrintMatrix(inputTr2);
// }
// R__ASSERT(!diff);
// }
// R__ASSERT(!diff);
Architecture_t::MultiplyTranspose(this->GetOutputAt(i), this->GetWeightsAt(0), inputTr);
Architecture_t::AddConvBiases(this->GetOutputAt(i), this->GetBiasesAt(0));

evaluateDerivative<Architecture_t>(this->GetDerivativesAt(i), fF, this->GetOutputAt(i));
evaluate<Architecture_t>(this->GetOutputAt(i), fF);
}
#endif
#endif
}

//______________________________________________________________________________
Expand Down Expand Up @@ -335,7 +335,7 @@ void TConvLayer<Architecture_t>::AddWeightsXMLTo(void *parent)
gTools().xmlengine().NewAttr(layerxml, 0, "ActivationFunction",
TString::Itoa(activationFunction, 10));

// write weights and bias matrix
// write weights and bias matrix
this->WriteMatrixToXML(layerxml, "Weights", this -> GetWeightsAt(0));
this->WriteMatrixToXML(layerxml, "Biases", this -> GetBiasesAt(0));

Expand Down
4 changes: 2 additions & 2 deletions tmva/tmva/inc/TMVA/DNN/CNN/MaxPoolLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ TMaxPoolLayer<Architecture_t>::TMaxPoolLayer(size_t batchSize, size_t inputDepth
size_t height, size_t width, size_t outputNSlices, size_t outputNRows,
size_t outputNCols, size_t frameHeight, size_t frameWidth,
size_t strideRows, size_t strideCols, Scalar_t dropoutProbability)
: VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, inputDepth, height, width, 0, 0, 0,
: VGeneralLayer<Architecture_t>(batchSize, inputDepth, inputHeight, inputWidth, inputDepth, height, width, "MAXPOOL", 0, 0, 0,
0, 0, 0, outputNSlices, outputNRows, outputNCols, EInitialization::kZero),
indexMatrix(), fFrameHeight(frameHeight), fFrameWidth(frameWidth), fStrideRows(strideRows),
fStrideCols(strideCols), fNLocalViewPixels(inputDepth * frameHeight * frameWidth), fNLocalViews(height * width),
Expand Down Expand Up @@ -236,7 +236,7 @@ void TMaxPoolLayer<Architecture_t>::AddWeightsXMLTo(void *parent)
template <typename Architecture_t>
void TMaxPoolLayer<Architecture_t>::ReadWeightsFromXML(void * /*parent */)
{
// all info is read before - nothing to do
// all info is read before - nothing to do
}

} // namespace CNN
Expand Down
8 changes: 4 additions & 4 deletions tmva/tmva/inc/TMVA/DNN/DeepNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ auto TDeepNet<Architecture_t, Layer_t>::calculateDimension(int imgDim, int fltDi
{
Scalar_t dimension = ((imgDim - fltDim + 2 * padding) / stride) + 1;
if (!isInteger(dimension) || dimension <= 0) {
this->Print();
int iLayer = fLayers.size();
this->Print();
int iLayer = fLayers.size();
Fatal("calculateDimension","Not compatible hyper parameters for layer %d - (imageDim, filterDim, padding, stride) %d , %d , %d , %d",
iLayer, imgDim, fltDim, padding, stride);
// std::cout << " calculateDimension - Not compatible hyper parameters (imgDim, fltDim, padding, stride)"
Expand Down Expand Up @@ -702,7 +702,7 @@ TReshapeLayer<Architecture_t> *TDeepNet<Architecture_t, Layer_t>::AddReshapeLaye
outputNCols = inputNCols;
depth = 1;
height = 1;
width = outputNCols;
width = outputNCols;
} else {
outputNSlices = this->GetBatchSize();
outputNRows = depth;
Expand Down Expand Up @@ -921,7 +921,7 @@ auto TDeepNet<Architecture_t, Layer_t>::Backward(std::vector<Matrix_t> &input, c
}

// need to have a dummy tensor (size=0) to pass for activation gradient backward which
// are not computed for the first layer
// are not computed for the first layer
std::vector<Matrix_t> dummy;
fLayers[0]->Backward(dummy, input, inp1, inp2);
}
Expand Down
8 changes: 4 additions & 4 deletions tmva/tmva/inc/TMVA/DNN/DenseLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ template <typename Architecture_t>
TDenseLayer<Architecture_t>::TDenseLayer(size_t batchSize, size_t inputWidth, size_t width, EInitialization init,
Scalar_t dropoutProbability, EActivationFunction f, ERegularization reg,
Scalar_t weightDecay)
: VGeneralLayer<Architecture_t>(batchSize, 1, 1, inputWidth, 1, 1, width, 1, width, inputWidth, 1, width, 1, 1,
: VGeneralLayer<Architecture_t>(batchSize, 1, 1, inputWidth, 1, 1, width, "DENSE", 1, width, inputWidth, 1, width, 1, 1,
batchSize, width, init),
fDerivatives(), fDropoutProbability(dropoutProbability), fF(f), fReg(reg), fWeightDecay(weightDecay)
{
Expand Down Expand Up @@ -199,7 +199,7 @@ template <typename Architecture_t>
void TDenseLayer<Architecture_t>::Print() const
{
std::cout << " DENSE Layer: \t ";
std::cout << " ( Input = " << this->GetWeightsAt(0).GetNcols(); // input size
std::cout << " ( Input = " << this->GetWeightsAt(0).GetNcols(); // input size
std::cout << " , Width = " << this->GetWeightsAt(0).GetNrows() << " ) "; // layer width
if (this->GetOutput().size() > 0) {
std::cout << "\tOutput = ( " << this->GetOutput().size() << " , " << this->GetOutput()[0].GetNrows() << " , " << this->GetOutput()[0].GetNcols() << " ) ";
Expand All @@ -223,7 +223,7 @@ void TDenseLayer<Architecture_t>::AddWeightsXMLTo(void *parent)
int activationFunction = static_cast<int>(this -> GetActivationFunction());
gTools().xmlengine().NewAttr(layerxml, 0, "ActivationFunction",
TString::Itoa(activationFunction, 10));
// write weights and bias matrix
// write weights and bias matrix
this->WriteMatrixToXML(layerxml, "Weights", this -> GetWeightsAt(0));
this->WriteMatrixToXML(layerxml, "Biases", this -> GetBiasesAt(0));
}
Expand All @@ -235,7 +235,7 @@ void TDenseLayer<Architecture_t>::ReadWeightsFromXML(void *parent)
// Read layer weights and biases from XML
this->ReadMatrixXML(parent,"Weights", this -> GetWeightsAt(0));
this->ReadMatrixXML(parent,"Biases", this -> GetBiasesAt(0));

}

} // namespace DNN
Expand Down
Loading