Esempio n. 1
0
  def _get_loss(self, mat_x, labels):
    """Calculates the loss of the current model for the given data, using a
    cross-entropy loss function.
    Args:
      mat_x (:obj:`numpy.ndarray`): The input data (image embeddings) to test, as a matrix of shape
        ``NxD``, where ``N`` is number of inputs to test and ``D`` is the dimension of the
        input feature (length of the feature vector).
      labels (:obj:`numpy.ndarray`): An array of the correct label indices that correspond to the
        test data passed in ``mat_x`` (class label index in one-hot vector). For example, if
        ``mat_x`` is just one image embedding, this array has one number for that image's correct
        label index.
    Returns:
      A 2-tuple with the cross-entropy loss (float) and gradients (a dictionary with ``'mat_w'``
      and ``'vec_b'``, for weight and bias, respectively).
    """
    mat_w_fc1 = self.params['mat_w_fc1']
    vec_b_fc1 = self.params['vec_b_fc1']
    mat_w_fc2 = self.params['mat_w_fc2']
    vec_b_fc2 = self.params['vec_b_fc2']

    scores_1, fc_cached_1 = ops.fc_forward(mat_x, mat_w_fc1, vec_b_fc1)
    scores_2, fc_cached_2 = ops.fc_forward(scores_1, mat_w_fc2, vec_b_fc2)
    # Record min, max value of scores.
    self.min_score = np.min([self.min_score, np.min(scores_2)])
    self.max_score = np.max([self.max_score, np.max(scores_2)])
    loss, dscores = ops.softmax_cross_entropy_loss(scores_2, labels)
    loss += 0.5 * self.reg * np.sum(mat_w_fc2 * mat_w_fc2)

    grads = {}
    dmat_x_fc2, grads['mat_w_fc2'], grads['vec_b_fc2'] = ops.fc_backward(dscores, fc_cached_2)
    dmat_x_fc1, grads['mat_w_fc1'], grads['vec_b_fc1'] = ops.fc_backward(dmat_x_fc2, fc_cached_1)
    #grads['mat_w'] += self.reg * mat_w

    return loss, grads
Esempio n. 2
0
    def test_fc_backward(self):
        np.random.seed(12345)
        mat_x = np.random.randn(5, 3)
        mat_w = np.random.randn(3, 10)
        vec_b = np.random.randn(10)
        mat_y, cached = ops.fc_forward(mat_x, mat_w, vec_b)
        labels = np.random.randint(10, size=5)
        _, dlogits = ops.softmax_cross_entropy_loss(mat_y, labels)
        dmat_x, dmat_w, dvec_b = ops.fc_backward(dlogits, cached)

        # Chain FC layer and softmax loss together.
        # `i` for internal.
        def chained_loss(i_mat_x, i_mat_w, i_vec_b, i_labels):
            i_mat_y, _ = ops.fc_forward(i_mat_x, i_mat_w, i_vec_b)
            loss, _ = ops.softmax_cross_entropy_loss(i_mat_y, i_labels)
            return loss

        numeric_dmat_x = get_numerical_gradient(
            lambda var_mat_x: chained_loss(var_mat_x, mat_w, vec_b, labels),
            mat_x)
        self.assertTrue(np.allclose(numeric_dmat_x, dmat_x))

        numeric_dmat_w = get_numerical_gradient(
            lambda var_mat_w: chained_loss(mat_x, var_mat_w, vec_b, labels),
            mat_w)
        self.assertTrue(np.allclose(numeric_dmat_w, dmat_w))

        numeric_dvec_b = get_numerical_gradient(
            lambda var_vec_b: chained_loss(mat_x, mat_w, var_vec_b, labels),
            vec_b)
        self.assertTrue(np.allclose(numeric_dvec_b, dvec_b))
Esempio n. 3
0
 def test_fc_forward(self):
     mat_x = np.array(range(10)).reshape([2, 5])
     mat_w = mat_x.T
     vec_b = np.ones(2)
     mat_y_expected = np.array([[31, 81], [81, 256]])
     mat_y, _ = ops.fc_forward(mat_x, mat_w, vec_b)
     self.assertTrue(np.allclose(mat_y_expected, mat_y))
Esempio n. 4
0
 def run_inference(self, mat_x):
   """Runs an inference using the current weights.
   Args:
     mat_x (:obj:`numpy.ndarray`): The input data (image embeddings) to infer, as a matrix of shape
       ``NxD``, where ``N`` is number of inputs to infer and ``D`` is the dimension of the
       input feature (length of the feature vector). (This can be one or more image embeddings.)
   Returns:
     The inferred label index (or an array of indices if multiple embeddings given).
   """
   mat_w_fc1 = self.params['mat_w_fc1']
   vec_b_fc1 = self.params['vec_b_fc1']
   mat_w_fc2 = self.params['mat_w_fc2']
   vec_b_fc2 = self.params['vec_b_fc2']
   
   scores, _ = ops.fc_forward(mat_x, mat_w_fc1, vec_b_fc1)
   scores, _ = ops.fc_forward(scores, mat_w_fc2, vec_b_fc2)
   
   if len(scores.shape) == 1:
     return np.argmax(scores)
   else:
     return np.argmax(scores, axis=1)
Esempio n. 5
0
 def chained_loss(i_mat_x, i_mat_w, i_vec_b, i_labels):
     i_mat_y, _ = ops.fc_forward(i_mat_x, i_mat_w, i_vec_b)
     loss, _ = ops.softmax_cross_entropy_loss(i_mat_y, i_labels)
     return loss