Esempio n. 1
0
    def testMismatchedRanksFails(self):
        data_1d = self.dtype([0.3, 0.7])
        data_2d = self.dtype([[1., .4], [.4, .2]])
        data_3d = self.dtype([[[1., .4], [.2, .5]]])

        data_1d = make_tensor_hiding_attributes(
            data_1d, hide_shape=self.use_dynamic_shape)
        data_2d = make_tensor_hiding_attributes(
            data_2d, hide_shape=self.use_dynamic_shape)
        data_3d = make_tensor_hiding_attributes(
            data_3d, hide_shape=self.use_dynamic_shape)

        with self.assertRaisesRegexp(Exception, 'similarly batched'):
            self.evaluate(
                matvecmul(data_2d,
                          data_3d,
                          validate_args=self.use_dynamic_shape))
        with self.assertRaisesRegexp(Exception, 'similarly batched'):
            self.evaluate(
                matvecmul(data_2d,
                          data_2d,
                          validate_args=self.use_dynamic_shape))
        with self.assertRaisesRegexp(Exception, 'similarly batched'):
            self.evaluate(
                matvecmul(data_3d,
                          data_3d,
                          validate_args=self.use_dynamic_shape))
        with self.assertRaisesRegexp(Exception, 'similarly batched'):
            self.evaluate(
                matvecmul(data_3d,
                          data_1d,
                          validate_args=self.use_dynamic_shape))
Esempio n. 2
0
  def testMismatchedRanksFails(self):
    data_1d = self.dtype([0.3, 0.7])
    data_2d = self.dtype([[1., .4],
                          [.4, .2]])
    data_3d = self.dtype([[[1., .4],
                           [.2, .5]]])

    data_1d = make_tensor_hiding_attributes(data_1d,
                                            hide_shape=self.use_dynamic_shape)
    data_2d = make_tensor_hiding_attributes(data_2d,
                                            hide_shape=self.use_dynamic_shape)
    data_3d = make_tensor_hiding_attributes(data_3d,
                                            hide_shape=self.use_dynamic_shape)

    with self.assertRaisesRegexp(Exception, 'similarly batched'):
      self.evaluate(matvecmul(data_2d, data_3d,
                              validate_args=self.use_dynamic_shape))
    with self.assertRaisesRegexp(Exception, 'similarly batched'):
      self.evaluate(matvecmul(data_2d, data_2d,
                              validate_args=self.use_dynamic_shape))
    with self.assertRaisesRegexp(Exception, 'similarly batched'):
      self.evaluate(matvecmul(data_3d, data_3d,
                              validate_args=self.use_dynamic_shape))
    with self.assertRaisesRegexp(Exception, 'similarly batched'):
      self.evaluate(matvecmul(data_3d, data_1d,
                              validate_args=self.use_dynamic_shape))
Esempio n. 3
0
def calculate_linear_predictor(model_matrix, model_coefficients, offset=None,
                               name=None):
  """Computes `model_matrix @ model_coefficients + offset`."""
  with tf.name_scope(name, 'calculate_linear_predictor',
                     [model_matrix, model_coefficients, offset]):
    predicted_linear_response = matvecmul(model_matrix, model_coefficients)
    if offset is not None:
      predicted_linear_response += offset
    return predicted_linear_response
Esempio n. 4
0
    def testLiteralsWithMismatchedDtypes(self):
        a = np.array([[1, 2], [3, 4]], np.float64)
        b = [1., 1.]
        expected_result = np.matmul(a, b)

        result = matvecmul(a, b)
        result = self.evaluate(result)

        self.assertAllClose(expected_result, result)
Esempio n. 5
0
  def testLiteralsWithMismatchedDtypes(self):
    a = np.array([[1, 2], [3, 4]], np.float64)
    b = [1., 1.]
    expected_result = np.matmul(a, b)

    result = matvecmul(a, b)
    result = self.evaluate(result)

    self.assertAllClose(expected_result, result)
Esempio n. 6
0
    def testTransposedMultiplicationWorks(self):
        a = self.dtype([[1., .4, .5], [.4, .2, .25]])
        b = self.dtype([0.3, 0.7])
        expected_result = np.matmul(np.transpose(a), b)

        a = make_tensor_hiding_attributes(a, hide_shape=self.use_dynamic_shape)
        b = make_tensor_hiding_attributes(b, hide_shape=self.use_dynamic_shape)
        result = matvecmul(a, b, transpose_a=True)
        result = self.evaluate(result)

        self.assertAllClose(expected_result, result)
Esempio n. 7
0
  def testTransposedMultiplicationWorks(self):
    a = self.dtype([[1., .4, .5],
                    [.4, .2, .25]])
    b = self.dtype([0.3, 0.7])
    expected_result = np.matmul(np.transpose(a), b)

    a = make_tensor_hiding_attributes(a, hide_shape=self.use_dynamic_shape)
    b = make_tensor_hiding_attributes(b, hide_shape=self.use_dynamic_shape)
    result = matvecmul(a, b, transpose_a=True)
    result = self.evaluate(result)

    self.assertAllClose(expected_result, result)
Esempio n. 8
0
    def testBatchedMultiplicationWorks(self):
        a = self.dtype([[[1., .4, .5], [.4, .2, .25]],
                        [[2, .3, .4], [.5, .6, .7]]])
        b = self.dtype([[0.3, 0.7, 0.5], [1.0, 2.1, 3.2]])
        expected_result = np.stack(
            [np.matmul(a[0, ...], b[0, ...]),
             np.matmul(a[1, ...], b[1, ...])])

        a = make_tensor_hiding_attributes(a, hide_shape=self.use_dynamic_shape)
        b = make_tensor_hiding_attributes(b, hide_shape=self.use_dynamic_shape)
        result = matvecmul(a, b)
        result = self.evaluate(result)

        self.assertAllClose(expected_result, result)
Esempio n. 9
0
  def testBatchedMultiplicationWorks(self):
    a = self.dtype([[[1., .4, .5],
                     [.4, .2, .25]],
                    [[2, .3, .4],
                     [.5, .6, .7]]])
    b = self.dtype([[0.3, 0.7, 0.5],
                    [1.0, 2.1, 3.2]])
    expected_result = np.stack([np.matmul(a[0, ...], b[0, ...]),
                                np.matmul(a[1, ...], b[1, ...])])

    a = make_tensor_hiding_attributes(a, hide_shape=self.use_dynamic_shape)
    b = make_tensor_hiding_attributes(b, hide_shape=self.use_dynamic_shape)
    result = matvecmul(a, b)
    result = self.evaluate(result)

    self.assertAllClose(expected_result, result)
Esempio n. 10
0
 def _joint_log_prob(model_coefficients_):
     predicted_linear_response_ = matvecmul(x_, model_coefficients_)
     return tf.reduce_sum(model.log_prob(y_,
                                         predicted_linear_response_))