SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TSigmoid Class Reference

#include <linalg.h>

Public Member Functions

 TSigmoid ()
 
 TSigmoid (const double &A_, const double &B_)
 
 TSigmoid (const TFltIntKdV &data)
 
 TSigmoid (TSIn &SIn)
 
void Load (TSIn &SIn)
 
void Save (TSOut &SOut) const
 
double GetVal (const double &x) const
 
double operator() (const double &x) const
 
void GetSigmoidAB (double &A_, double &B_)
 

Static Private Member Functions

static double EvaluateFit (const TFltIntKdV &data, const double A, const double B)
 
static void EvaluateFit (const TFltIntKdV &data, const double A, const double B, double &J, double &JA, double &JB)
 
static void EvaluateFit (const TFltIntKdV &data, const double A, const double B, const double U, const double V, const double lambda, double &J, double &JJ, double &JJJ)
 

Private Attributes

TFlt A
 
TFlt B
 

Detailed Description

Definition at line 454 of file linalg.h.

Constructor & Destructor Documentation

TSigmoid::TSigmoid ( )
inline

Definition at line 474 of file linalg.h.

474 { };
TSigmoid::TSigmoid ( const double &  A_,
const double &  B_ 
)
inline

Definition at line 475 of file linalg.h.

475 : A(A_), B(B_) { };
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
TSigmoid::TSigmoid ( const TFltIntKdV data)

Definition at line 1548 of file linalg.cpp.

1548  {
1549  // Let z_i be the projection of the i'th training example, and y_i \in {-1, +1} be its class label.
1550  // Our sigmoid is: P(Y = y | Z = z) = 1 / [1 + e^{-Az + B}]
1551  // and we want to maximize \prod_i P(Y = y_i | Z = z_i)
1552  // = \prod_{i : y_i = 1} 1 / [1 + e^{-Az_i + B}] \prod_{i : y_i = -1} e^{-Az_i + B} / [1 + e^{-Az_i + B}]
1553  // or minimize its negative logarithm,
1554  // J(A, B) = \sum_{i : y_i = 1} ln [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} [ln [1 + e^{-Az_i + B}] - {-Az_i + B}]
1555  // = \sum_i ln [1 + e^{-Az_i + B}] - \sum_{i : y_i = -1} {-Az_i + B}.
1556  // partial J / partial A = \sum_i (-z_i) e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} Az_i.
1557  // partial J / partial B = \sum_i e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} (-1).
1558  double minProj = data[0].Key, maxProj = data[0].Key;
1559  {for (int i = 1; i < data.Len(); i++) {
1560  double zi = data[i].Key; if (zi < minProj) minProj = zi; if (zi > maxProj) maxProj = zi; }}
1561  //const bool dump = false;
1562  A = 1.0; B = 0.5 * (minProj + maxProj);
1563  double bestJ = 0.0, bestA = 0.0, bestB = 0.0, lambda = 1.0;
1564  for (int nIter = 0; nIter < 50; nIter++)
1565  {
1566  double J, JA, JB; TSigmoid::EvaluateFit(data, A, B, J, JA, JB);
1567  if (nIter == 0 || J < bestJ) { bestJ = J; bestA = A; bestB = B; }
1568  // How far should we move?
1569  //if (dump) printf("Iter %2d: A = %.5f, B = %.5f, J = %.5f, partial = (%.5f, %.5f)\n", nIter, A, B, J, JA, JB);
1570  double norm = TMath::Sqr(JA) + TMath::Sqr(JB);
1571  if (norm < 1e-10) break;
1572  const int cl = -1; // should be -1
1573 
1574  double Jc = TSigmoid::EvaluateFit(data, A + cl * lambda * JA / norm, B + cl * lambda * JB / norm);
1575  //if (dump) printf(" At lambda = %.5f, Jc = %.5f\n", lambda, Jc);
1576  if (Jc > J) {
1577  while (lambda > 1e-5) {
1578  lambda = 0.5 * lambda;
1579  Jc = TSigmoid::EvaluateFit(data, A + cl * lambda * JA / norm, B + cl * lambda * JB / norm);
1580  //if (dump) printf(" At lambda = %.5f, Jc = %.5f\n", lambda, Jc);
1581  } }
1582  else if (Jc < J) {
1583  while (lambda < 1e5) {
1584  double lambda2 = 2 * lambda;
1585  double Jc2 = TSigmoid::EvaluateFit(data, A + cl * lambda2 * JA / norm, B + cl * lambda2 * JB / norm);
1586  //if (dump) printf(" At lambda = %.5f, Jc = %.5f\n", lambda2, Jc2);
1587  if (Jc2 > Jc) break;
1588  lambda = lambda2; Jc = Jc2; } }
1589  if (Jc >= J) break;
1590  A += cl * lambda * JA / norm; B += cl * lambda * JB / norm;
1591  //if (dump) printf(" Lambda = %.5f, new A = %.5f, new B = %.5f, new J = %.5f\n", lambda, A, B, Jc);
1592  }
1593  A = bestA; B = bestB;
1594 }
static double EvaluateFit(const TFltIntKdV &data, const double A, const double B)
Definition: linalg.cpp:1490
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TFlt A
Definition: linalg.h:456
static double Sqr(const double &x)
Definition: xmath.h:12
TFlt B
Definition: linalg.h:457
TSigmoid::TSigmoid ( TSIn SIn)
inline

Definition at line 480 of file linalg.h.

480 { A.Load(SIn); B.Load(SIn); }
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
void Load(TSIn &SIn)
Definition: dt.h:1312

Member Function Documentation

double TSigmoid::EvaluateFit ( const TFltIntKdV data,
const double  A,
const double  B 
)
staticprivate

Definition at line 1490 of file linalg.cpp.

1491 {
1492  double J = 0.0;
1493  for (int i = 0; i < data.Len(); i++)
1494  {
1495  double zi = data[i].Key; int yi = data[i].Dat;
1496  double e = exp(-A * zi + B);
1497  double denum = 1.0 + e;
1498  double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
1499  J -= log(prob < 1e-20 ? 1e-20 : prob);
1500  }
1501  return J;
1502 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
void TSigmoid::EvaluateFit ( const TFltIntKdV data,
const double  A,
const double  B,
double &  J,
double &  JA,
double &  JB 
)
staticprivate

Definition at line 1504 of file linalg.cpp.

1505 {
1506  // J(A, B) = \sum_{i : y_i = 1} ln [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} [ln [1 + e^{-Az_i + B}] - {-Az_i + B}]
1507  // = \sum_i ln [1 + e^{-Az_i + B}] - \sum_{i : y_i = -1} {-Az_i + B}.
1508  // partial J / partial A = \sum_i (-z_i) e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} Az_i.
1509  // partial J / partial B = \sum_i e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} (-1).
1510  J = 0.0; double sum_all_PyNeg = 0.0, sum_all_ziPyNeg = 0.0, sum_yNeg_zi = 0.0, sum_yNeg_1 = 0.0;
1511  for (int i = 0; i < data.Len(); i++)
1512  {
1513  double zi = data[i].Key; int yi = data[i].Dat;
1514  double e = exp(-A * zi + B);
1515  double denum = 1.0 + e;
1516  double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
1517  J -= log(prob < 1e-20 ? 1e-20 : prob);
1518  sum_all_PyNeg += e / denum;
1519  sum_all_ziPyNeg += zi * e / denum;
1520  if (yi < 0) { sum_yNeg_zi += zi; sum_yNeg_1 += 1; }
1521  }
1522  JA = -sum_all_ziPyNeg + sum_yNeg_zi;
1523  JB = sum_all_PyNeg - sum_yNeg_1;
1524 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
void TSigmoid::EvaluateFit ( const TFltIntKdV data,
const double  A,
const double  B,
const double  U,
const double  V,
const double  lambda,
double &  J,
double &  JJ,
double &  JJJ 
)
staticprivate

Definition at line 1526 of file linalg.cpp.

1528 {
1529  // Let E_i = e^{-(A + lambda U) z_i + (B + lambda V)}. Then we have
1530  // J(lambda) = \sum_i ln [1 + E_i] - \sum_{i : y_i = -1} {-(A + lambda U)z_i + (B + lambda V)}.
1531  // J'(lambda) = \sum_i (V - U z_i) E_i / [1 + E_i] - \sum_{i : y_i = -1} {V - U z_i).
1532  // = \sum_i (V - U z_i) [1 - 1 / [1 + E_i]] - \sum_{i : y_i = -1} {V - U z_i).
1533  // J"(lambda) = \sum_i (V - U z_i)^2 E_i / [1 + E_i]^2.
1534  J = 0.0; JJ = 0.0; JJJ = 0.0;
1535  for (int i = 0; i < data.Len(); i++)
1536  {
1537  double zi = data[i].Key; int yi = data[i].Dat;
1538  double e = exp(-A * zi + B);
1539  double denum = 1.0 + e;
1540  double prob = (yi > 0) ? (1.0 / denum) : (e / denum);
1541  J -= log(prob < 1e-20 ? 1e-20 : prob);
1542  double VU = V - U * zi;
1543  JJ += VU * (e / denum); if (yi < 0) JJ -= VU;
1544  JJJ += VU * VU * e / denum / denum;
1545  }
1546 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
void TSigmoid::GetSigmoidAB ( double &  A_,
double &  B_ 
)
inline

Definition at line 489 of file linalg.h.

489 { A_=A; B_=B; }
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
double TSigmoid::GetVal ( const double &  x) const
inline

Definition at line 484 of file linalg.h.

484  {
485  return 1.0 / (1.0 + exp(-A * x + B)); }
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
void TSigmoid::Load ( TSIn SIn)
inline

Definition at line 481 of file linalg.h.

481 { A.Load(SIn); B.Load(SIn); }
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
void Load(TSIn &SIn)
Definition: dt.h:1312
double TSigmoid::operator() ( const double &  x) const
inline

Definition at line 486 of file linalg.h.

486  {
487  return GetVal(x); }
double GetVal(const double &x) const
Definition: linalg.h:484
void TSigmoid::Save ( TSOut SOut) const
inline

Definition at line 482 of file linalg.h.

482 {A.Save(SOut); B.Save(SOut);}
TFlt A
Definition: linalg.h:456
TFlt B
Definition: linalg.h:457
void Save(TSOut &SOut) const
Definition: dt.h:1309

Member Data Documentation

TFlt TSigmoid::A
private

Definition at line 456 of file linalg.h.

TFlt TSigmoid::B
private

Definition at line 457 of file linalg.h.


The documentation for this class was generated from the following files: