예제 #1
0
 def forward(self, bottom, top):
     pred = bottom[0].data()
     label = bottom[1].data()[:, np.newaxis]
     prob = logexp.exp(pred)
     numexpr.evaluate("prob / (1. + prob)", out=prob)
     diff = bottom[0].init_diff(setzero=False)
     numexpr.evaluate("label - prob", out=diff)
     self._loss = np.dot(label.flat, logexp.log(prob).flat) + \
                  np.dot((1. - label).flat, logexp.log(1. - prob).flat)
     # finally, scale down by the number of data points
     # Also, since we we computing the Loss (minimizing), we change the
     # sign of the loss value.
     diff *= - self.spec['weight'] / diff.shape[0]
     self._loss *= - self.spec['weight'] / diff.shape[0]
예제 #2
0
 def forward(self, bottom, top):
     pred = bottom[0].data()
     label = bottom[1].data()[:, np.newaxis]
     prob = logexp.exp(pred)
     numexpr.evaluate("prob / (1. + prob)", out=prob)
     diff = bottom[0].init_diff(setzero=False)
     numexpr.evaluate("label - prob", out=diff)
     self._loss = np.dot(label.flat, logexp.log(prob).flat) + \
                  np.dot((1. - label).flat, logexp.log(1. - prob).flat)
     # finally, scale down by the number of data points
     # Also, since we we computing the Loss (minimizing), we change the
     # sign of the loss value.
     diff *= -self.spec['weight'] / diff.shape[0]
     self._loss *= -self.spec['weight'] / diff.shape[0]
예제 #3
0
 def forward(self, bottom, top):
     prob = bottom[0].data()
     label = bottom[1].data()
     diff = bottom[0].init_diff()
     if label.ndim == 1:
         # The labels are given as a sparse vector.
         indices = np.arange(diff.shape[0])
         prob_sub = np.ascontiguousarray(prob[indices, label])
         diff[indices, label] = 1. / prob_sub
         self._loss = logexp.log(prob_sub).sum()
     else:
         numexpr.evaluate('label / prob', out=diff)
         self._loss = np.dot(label.flat, logexp.log(prob).flat)
     # finally, scale down by the number of data points
     diff *= - self.spec['weight'] / diff.shape[0]
     self._loss *= - self.spec['weight'] / diff.shape[0]
예제 #4
0
 def forward(self, bottom, top):
     prob = bottom[0].data()
     label = bottom[1].data()
     diff = bottom[0].init_diff()
     if label.ndim == 1:
         # The labels are given as a sparse vector.
         indices = np.arange(diff.shape[0])
         prob_sub = np.ascontiguousarray(prob[indices, label])
         diff[indices, label] = 1. / prob_sub
         self._loss = logexp.log(prob_sub).sum()
     else:
         numexpr.evaluate('label / prob', out=diff)
         self._loss = np.dot(label.flat, logexp.log(prob).flat)
     # finally, scale down by the number of data points
     diff *= -self.spec['weight'] / diff.shape[0]
     self._loss *= -self.spec['weight'] / diff.shape[0]
예제 #5
0
 def forward(self, bottom, top):
     pred = bottom[0].data()
     prob = self._prob.init_data(pred.shape, pred.dtype, setdata=False)
     prob[:] = pred
     prob -= prob.max(axis=1)[:, np.newaxis]
     logexp.exp(prob, out=prob)
     prob /= prob.sum(axis=1)[:, np.newaxis]
     diff = bottom[0].init_diff(setzero=False)
     diff[:] = prob
     logexp.log(prob, out=prob)
     label = bottom[1].data()
     if label.ndim == 1:
         # The labels are given as a sparse vector.
         diff[np.arange(diff.shape[0]), label] -= 1.
         self._loss = -prob[np.arange(diff.shape[0]), label].sum()
     else:
         # The labels are given as a dense matrix.
         diff -= label
         self._loss = -np.dot(prob.flat, label.flat)
     # finally, scale down by the number of data points
     diff *= self.spec['weight'] / diff.shape[0]
     self._loss *= self.spec['weight'] / diff.shape[0]
예제 #6
0
 def forward(self, bottom, top):
     pred = bottom[0].data()
     prob = self._prob.init_data(
         pred.shape, pred.dtype, setdata=False)
     prob[:] = pred
     prob -= prob.max(axis=1)[:, np.newaxis]
     logexp.exp(prob, out=prob)
     prob /= prob.sum(axis=1)[:, np.newaxis]
     diff = bottom[0].init_diff(setzero=False)
     diff[:] = prob
     logexp.log(prob, out=prob)
     label = bottom[1].data()
     if label.ndim == 1:
         # The labels are given as a sparse vector.
         diff[np.arange(diff.shape[0]), label] -= 1.
         self._loss = -prob[np.arange(diff.shape[0]), label].sum()
     else:
         # The labels are given as a dense matrix.
         diff -= label
         self._loss = -np.dot(prob.flat, label.flat)
     # finally, scale down by the number of data points
     diff *= self.spec['weight'] / diff.shape[0]
     self._loss *= self.spec['weight'] / diff.shape[0]