Skip to content
Snippets Groups Projects
Commit 3238b155 authored by Ludovic Noels's avatar Ludovic Noels
Browse files
parents f86a5f53 f0c7a8d4
No related branches found
No related tags found
1 merge request!309Master
...@@ -65,14 +65,15 @@ class RectifierActivationFunction: public activationFunction ...@@ -65,14 +65,15 @@ class RectifierActivationFunction: public activationFunction
{ {
protected: protected:
double _fact; double _fact;
double _offset;
public: public:
RectifierActivationFunction(double f=10.): activationFunction(),_fact(f){} RectifierActivationFunction(double f=10.): activationFunction(),_fact(f),_offset(0.){}
RectifierActivationFunction(const RectifierActivationFunction& src): activationFunction(src){} RectifierActivationFunction(const RectifierActivationFunction& src): activationFunction(src){}
virtual ~RectifierActivationFunction(){} virtual ~RectifierActivationFunction(){}
virtual activationFunction::functionType getType() const {return activationFunction::rectifier;}; virtual activationFunction::functionType getType() const {return activationFunction::rectifier;};
virtual std::string getName() const; virtual std::string getName() const;
virtual double getVal(double x) const {return (log(1.+exp(_fact*x)))/_fact;}; virtual double getVal(double x) const {return (_offset+log(1.+exp(_fact*x)))/_fact;};
virtual double getReciprocalVal(double y) const {return (log(exp(_fact*y)-1.))/_fact;}; // inverse function virtual double getReciprocalVal(double y) const {return (log(exp(_fact*(y-_offset))-1.))/_fact;}; // inverse function
virtual double getDiff(double x) const {return exp(_fact*x)/(1.+exp(_fact*x));}; virtual double getDiff(double x) const {return exp(_fact*x)/(1.+exp(_fact*x));};
virtual activationFunction* clone() const {return new RectifierActivationFunction(*this);}; virtual activationFunction* clone() const {return new RectifierActivationFunction(*this);};
}; };
......
...@@ -980,9 +980,14 @@ void BimaterialHomogenization::compute(const std::vector<fullMatrix<double> >& C ...@@ -980,9 +980,14 @@ void BimaterialHomogenization::compute(const std::vector<fullMatrix<double> >& C
{ {
DCeffDnormal.resize(3); DCeffDnormal.resize(3);
} }
};
computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal,Ceff, computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal,Ceff,
stiff,&DCeffDcin[0],&DCeffDcin[1],&DCeffDf[0],&DCeffDf[1],&DCeffDnormal[0],&DCeffDnormal[1],&DCeffDnormal[2]); stiff,&DCeffDcin[0],&DCeffDcin[1],&DCeffDf[0],&DCeffDf[1],&DCeffDnormal[0],&DCeffDnormal[1],&DCeffDnormal[2]);
}
else
{
computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal,Ceff);
};
}; };
void LaminateHomogenization::compute(const fullMatrix<double>& C1, const fullMatrix<double>& C2, const fullMatrix<double>& C3, void LaminateHomogenization::compute(const fullMatrix<double>& C1, const fullMatrix<double>& C2, const fullMatrix<double>& C3,
...@@ -1104,6 +1109,8 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin ...@@ -1104,6 +1109,8 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin
} }
} }
else if (numPhase == 2) else if (numPhase == 2)
{
if (stiff)
{ {
computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal,Ceff, computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal,Ceff,
stiff,&DCeffDcin[0],&DCeffDcin[1], stiff,&DCeffDcin[0],&DCeffDcin[1],
...@@ -1111,6 +1118,11 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin ...@@ -1111,6 +1118,11 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin
&DCeffDnormal[1],&DCeffDnormal[2]); &DCeffDnormal[1],&DCeffDnormal[2]);
} }
else else
{
computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal,Ceff);
}
}
else
{ {
// //
auto getSumFaction = [](const std::vector<double>& ff, int i, fullVector<double>& DoutDf) auto getSumFaction = [](const std::vector<double>& ff, int i, fullVector<double>& DoutDf)
...@@ -1173,7 +1185,14 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin ...@@ -1173,7 +1185,14 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin
}; };
} }
// two phase // two phase
if (stiff)
{
computeBimaterial(CC[i],Cin[i+1],f0i,f1i,normal,CC[i+1],stiff,&A[i],&B[i],&F0[i],&F1[i],&N0[i],&N1[i],&N2[i]); computeBimaterial(CC[i],Cin[i+1],f0i,f1i,normal,CC[i+1],stiff,&A[i],&B[i],&F0[i],&F1[i],&N0[i],&N1[i],&N2[i]);
}
else
{
computeBimaterial(CC[i],Cin[i+1],f0i,f1i,normal,CC[i+1]);
}
}; };
// //
Ceff = CC[numPhase-1]; Ceff = CC[numPhase-1];
...@@ -1264,6 +1283,8 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin ...@@ -1264,6 +1283,8 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin
} }
} }
else if (numPhase == 2) else if (numPhase == 2)
{
if (stiff)
{ {
computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal[0],Ceff, computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal[0],Ceff,
stiff,&DCeffDcin[0],&DCeffDcin[1], stiff,&DCeffDcin[0],&DCeffDcin[1],
...@@ -1271,6 +1292,11 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin ...@@ -1271,6 +1292,11 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin
&DCeffDnormal[0][1],&DCeffDnormal[0][2]); &DCeffDnormal[0][1],&DCeffDnormal[0][2]);
} }
else else
{
computeBimaterial(Cin[0],Cin[1],f[0],f[1],normal[0],Ceff);
}
}
else
{ {
// //
auto getSumFaction = [](const std::vector<double>& ff, int i, fullVector<double>& DoutDf) auto getSumFaction = [](const std::vector<double>& ff, int i, fullVector<double>& DoutDf)
...@@ -1333,7 +1359,14 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin ...@@ -1333,7 +1359,14 @@ void LaminateHomogenization::compute(const std::vector<fullMatrix<double> >& Cin
}; };
} }
// two phase // two phase
if (stiff)
{
computeBimaterial(CC[i],Cin[i+1],f0i,f1i,normal[i],CC[i+1],stiff,&A[i],&B[i],&F0[i],&F1[i],&N0[i],&N1[i],&N2[i]); computeBimaterial(CC[i],Cin[i+1],f0i,f1i,normal[i],CC[i+1],stiff,&A[i],&B[i],&F0[i],&F1[i],&N0[i],&N1[i],&N2[i]);
}
else
{
computeBimaterial(CC[i],Cin[i+1],f0i,f1i,normal[i],CC[i+1]);
}
}; };
// //
Ceff = CC[numPhase-1]; Ceff = CC[numPhase-1];
...@@ -1696,7 +1729,8 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los ...@@ -1696,7 +1729,8 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los
WBest = Wcur; WBest = Wcur;
Wprev = Wcur; Wprev = Wcur;
g.resize(numDof,true); g.resize(numDof,true);
double costfuncPrev = 0.; double costfuncPrev = evaluateTrainingSet(*lossEval);
Msg::Info("costfuncPrev = %e",costfuncPrev);
//OfflineData //OfflineData
int epoch = 0; int epoch = 0;
...@@ -1705,6 +1739,7 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los ...@@ -1705,6 +1739,7 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los
int Ntrain = _XTrain.size(); int Ntrain = _XTrain.size();
double lrCur = lr; double lrCur = lr;
int numEpochIncrease = 0; int numEpochIncrease = 0;
int numEpochLrMin = 0;
int numEpochDecrease = 0; int numEpochDecrease = 0;
while (true) while (true)
{ {
...@@ -1773,17 +1808,23 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los ...@@ -1773,17 +1808,23 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los
double costfunc = evaluateTrainingSet(*lossEval); double costfunc = evaluateTrainingSet(*lossEval);
if (numberBatches==Ntrain) if (numberBatches==Ntrain)
{ {
if ((costfunc > costfuncPrev) && (epoch > 0)) if (costfunc > costfuncPrev)
{ {
numEpochIncrease ++; numEpochIncrease ++;
if (numEpochIncrease == 1) if (numEpochIncrease == 1)
{ {
numEpochIncrease = 0; numEpochIncrease = 0;
Msg::Info("reduce learning rate from %.5e to %.5e",lrCur,0.8*lrCur); Msg::Info("costfunc = %e, reduce learning rate from %.5e to %.5e",costfunc,lrCur,0.8*lrCur);
lrCur *= 0.8; lrCur *= 0.8;
if (lrCur < lrmin) if (lrCur < lrmin)
{ {
lrCur = lrmin; lrCur = lrmin;
numEpochLrMin ++;
if (numEpochLrMin>10)
{
Msg::Info("maximal number of iterations %d at minimal learning step is reached !!",numEpochLrMin);
break;
}
} }
numEpochDecrease = 0; numEpochDecrease = 0;
Wcur = Wprev; Wcur = Wprev;
...@@ -1844,8 +1885,10 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los ...@@ -1844,8 +1885,10 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los
if (removeZeroContribution) if (removeZeroContribution)
{ {
if ((epoch+1) %numStepRemove==0) if ((epoch+1) %numStepRemove==0)
{ {
double costfuncPrev_pre = evaluateTrainingSet(*lossEval);
Msg::Info("tree removing"); Msg::Info("tree removing");
bool removed = _T->removeLeavesWithZeroContribution(tolRemove); bool removed = _T->removeLeavesWithZeroContribution(tolRemove);
if (removed) if (removed)
...@@ -1858,7 +1901,10 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los ...@@ -1858,7 +1901,10 @@ void TrainingDeepMaterialNetwork::train(double lr, int maxEpoch, std::string los
Wprev = Wcur; Wprev = Wcur;
g.resize(numDof,true); g.resize(numDof,true);
} }
costfuncPrev = evaluateTrainingSet(*lossEval);
Msg::Info("pre-removing costfuncPrev = %e and after removing costfuncPrev = %e",costfuncPrev_pre,costfuncPrev);
} }
} }
epoch++; epoch++;
...@@ -3158,7 +3204,7 @@ void DeepMaterialNetwork::downscale(const STensor3& Fcur, STensor3& F, const Tr ...@@ -3158,7 +3204,7 @@ void DeepMaterialNetwork::downscale(const STensor3& Fcur, STensor3& F, const Tr
double wChild = getWeight(node); double wChild = getWeight(node);
int childOrder = node->childOrder; int childOrder = node->childOrder;
// //
double f = wChild/wParent; //double f = wChild/wParent;
double fact = 1./wChild; double fact = 1./wChild;
/* /*
......
...@@ -142,10 +142,10 @@ class BimaterialHomogenization : public Homogenization ...@@ -142,10 +142,10 @@ class BimaterialHomogenization : public Homogenization
fullMatrix<double>& DNDn2) const; fullMatrix<double>& DNDn2) const;
virtual void computeBimaterial(const fullMatrix<double>& C1, const fullMatrix<double>& C2, double f1, double f2, const SVector3& normal, virtual void computeBimaterial(const fullMatrix<double>& C1, const fullMatrix<double>& C2, double f1, double f2, const SVector3& normal,
fullMatrix<double>& Ceff, fullMatrix<double>& Ceff,
bool stiff, bool stiff=false,
hyperFullMatrix* DCeffDC1, hyperFullMatrix* DCeffDC2, hyperFullMatrix* DCeffDC1=NULL, hyperFullMatrix* DCeffDC2=NULL,
fullMatrix<double>* DCeffDf1, fullMatrix<double>* DCeffDf2, fullMatrix<double>* DCeffDf1=NULL, fullMatrix<double>* DCeffDf2=NULL,
fullMatrix<double>* DCeffDnormal1,fullMatrix<double>* DCeffDnormal2, fullMatrix<double>* DCeffDnormal3) const; fullMatrix<double>* DCeffDnormal1=NULL,fullMatrix<double>* DCeffDnormal2=NULL, fullMatrix<double>* DCeffDnormal3=NULL) const;
#endif //SWIG #endif //SWIG
}; };
......
...@@ -244,7 +244,7 @@ void Tree::createSpecialTripleTree(int numNodes, int numDirVars, const std::stri ...@@ -244,7 +244,7 @@ void Tree::createSpecialTripleTree(int numNodes, int numDirVars, const std::stri
} }
}; };
void Tree::createRandomTreeSameDepthForNodes(int nbLeaves, int numChildMax, int leafPerNode, int numDirVars,const std::string affunc, bool randomChild) void Tree::createRandomTreeSameDepthForNodes(int nbLeaves, int numChildMax, int leafPerNode, int numDirVars,const std::string affunc, bool randomChild, bool randomMat)
{ {
auto makeGroup = [&](const std::vector<int>& input, std::vector<int>& outPut, bool first) auto makeGroup = [&](const std::vector<int>& input, std::vector<int>& outPut, bool first)
{ {
...@@ -269,7 +269,7 @@ void Tree::createRandomTreeSameDepthForNodes(int nbLeaves, int numChildMax, int ...@@ -269,7 +269,7 @@ void Tree::createRandomTreeSameDepthForNodes(int nbLeaves, int numChildMax, int
if (first) if (first)
{ {
numChilds = leafPerNode; numChilds = leafPerNode;
if (randomChild) if (randomMat)
{ {
numChilds = rand() % (leafPerNode-1) + 2; // number takes a random value from 2 to leafPerNode numChilds = rand() % (leafPerNode-1) + 2; // number takes a random value from 2 to leafPerNode
} }
...@@ -971,7 +971,7 @@ int Tree::getNumberOfNodes() const ...@@ -971,7 +971,7 @@ int Tree::getNumberOfNodes() const
return allNodes.size(); return allNodes.size();
}; };
bool Tree::removeZeroLeaves(double tol) bool Tree::removeZeroLeaves(double tol, bool iteration)
{ {
bool ok = false; bool ok = false;
int maxInter = 100; int maxInter = 100;
...@@ -1122,6 +1122,11 @@ bool Tree::removeZeroLeaves(double tol) ...@@ -1122,6 +1122,11 @@ bool Tree::removeZeroLeaves(double tol)
Msg::Exit(0); Msg::Exit(0);
} }
}; };
if (!iteration)
{
break;
}
}; };
return false; return false;
}; };
...@@ -1904,7 +1909,8 @@ void Tree::initialize(bool rand) ...@@ -1904,7 +1909,8 @@ void Tree::initialize(bool rand)
for (int i=0; i< allLeaves.size(); i++) for (int i=0; i< allLeaves.size(); i++)
{ {
TreeNode* n = allLeaves[i]; TreeNode* n = allLeaves[i];
n->weight *= (1./toltalWeightLeaves); double vv = (n->af->getVal(n->weight))/toltalWeightLeaves;
n->weight *= n->af->getReciprocalVal(vv);
// propagate data // propagate data
double w = n->weight; double w = n->weight;
while (n->parent != NULL) while (n->parent != NULL)
......
...@@ -53,7 +53,7 @@ class Tree ...@@ -53,7 +53,7 @@ class Tree
void createPerfectTree(int maxDepth, int numChildRegular, int numChildLeaf, int numDirVars, const std::string affunc="relu", int lastRepeat=1); void createPerfectTree(int maxDepth, int numChildRegular, int numChildLeaf, int numDirVars, const std::string affunc="relu", int lastRepeat=1);
void createMixedTree(int maxDepth, int numChildMax, int leafPerNode, int numDirVars,const std::string affunc="relu"); void createMixedTree(int maxDepth, int numChildMax, int leafPerNode, int numDirVars,const std::string affunc="relu");
void createRandomTree(int nbLeaves, int numChildMax, int leafPerNode, int numDirVars,const std::string affunc="relu", bool randomSubdivion=true, bool randomChild=true, bool sameNbChildLayer=true); void createRandomTree(int nbLeaves, int numChildMax, int leafPerNode, int numDirVars,const std::string affunc="relu", bool randomSubdivion=true, bool randomChild=true, bool sameNbChildLayer=true);
void createRandomTreeSameDepthForNodes(int nbLeaves, int numChildMax, int leafPerNode, int numDirVars,const std::string affunc="relu", bool randomChild=true); void createRandomTreeSameDepthForNodes(int nbLeaves, int numChildMax, int leafPerNode, int numDirVars,const std::string affunc="relu", bool randomChild=true, bool randomMat=true);
void createSpecialBinaryTree(int numNodes, int numDirVars,const std::string affunc="relu"); void createSpecialBinaryTree(int numNodes, int numDirVars,const std::string affunc="relu");
void createSpecialTripleTree(int numNodes, int numDirVars,const std::string affunc="relu"); void createSpecialTripleTree(int numNodes, int numDirVars,const std::string affunc="relu");
void printTree() const; void printTree() const;
...@@ -78,7 +78,7 @@ class Tree ...@@ -78,7 +78,7 @@ class Tree
void printNodeFraction() const; void printNodeFraction() const;
bool removeLeavesWithZeroContribution(double tol=1e-6); bool removeLeavesWithZeroContribution(double tol=1e-6);
bool removeZeroLeaves(double tol); bool removeZeroLeaves(double tol, bool iteration=true);
bool treeMerging(); bool treeMerging();
#ifndef SWIG #ifndef SWIG
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment