def test_matrix_vector(self): xy_lt = core.LabeledTensor(tf.reshape(tf.range(6), (2, 3)), ['x', 'y']) y_lt = core.LabeledTensor(tf.range(3), ['y']) matmul_lt = ops.matmul(xy_lt, y_lt) golden_lt = core.LabeledTensor( tf.matmul(xy_lt.tensor, tf.reshape(y_lt.tensor, (-1, 1)))[:, 0], ['x']) self.assertLabeledTensorsEqual(matmul_lt, golden_lt) matmul_lt = ops.matmul(y_lt, xy_lt) self.assertLabeledTensorsEqual(matmul_lt, golden_lt)
def test_matrix_matrix_axis_order(self): xy_lt = core.LabeledTensor(tf.reshape(tf.range(6), (2, 3)), ['x', 'y']) yz_lt = core.LabeledTensor(tf.reshape(tf.range(12), (3, 4)), ['y', 'z']) golden_lt = core.LabeledTensor( tf.matmul(xy_lt.tensor, yz_lt.tensor), ['x', 'z']) with core.axis_order_scope(['x', 'y', 'z']): matmul_lt = ops.matmul(xy_lt, yz_lt) self.assertLabeledTensorsEqual(matmul_lt, golden_lt) matmul_lt = ops.matmul(yz_lt, xy_lt) self.assertLabeledTensorsEqual(matmul_lt, golden_lt)
def test_invalid(self): scalar_lt = core.LabeledTensor(array_ops.ones(()), []) x_lt = core.LabeledTensor(array_ops.ones((2, )), ['x']) x2_lt = core.LabeledTensor(array_ops.ones((3, )), ['x']) y_lt = core.LabeledTensor(array_ops.ones((3, )), ['y']) xy_lt = core.LabeledTensor(array_ops.ones((2, 3)), ['x', 'y']) xyz_lt = core.LabeledTensor(array_ops.ones((2, 3, 1)), ['x', 'y', 'z']) with self.assertRaisesRegexp(ValueError, 'inputs with at least rank'): ops.matmul(x_lt, scalar_lt) with self.assertRaises(NotImplementedError): ops.matmul(x_lt, xyz_lt) with self.assertRaisesRegexp(ValueError, 'exactly one axis in common'): ops.matmul(x_lt, y_lt) with self.assertRaises(NotImplementedError): ops.matmul(xy_lt, xy_lt) with self.assertRaisesRegexp(ValueError, 'does not match'): ops.matmul(x_lt, x2_lt)
def test_invalid(self): scalar_lt = core.LabeledTensor(array_ops.ones(()), []) x_lt = core.LabeledTensor(array_ops.ones((2,)), ['x']) x2_lt = core.LabeledTensor(array_ops.ones((3,)), ['x']) y_lt = core.LabeledTensor(array_ops.ones((3,)), ['y']) xy_lt = core.LabeledTensor(array_ops.ones((2, 3)), ['x', 'y']) xyz_lt = core.LabeledTensor(array_ops.ones((2, 3, 1)), ['x', 'y', 'z']) with self.assertRaisesRegexp(ValueError, 'inputs with at least rank'): ops.matmul(x_lt, scalar_lt) with self.assertRaises(NotImplementedError): ops.matmul(x_lt, xyz_lt) with self.assertRaisesRegexp(ValueError, 'exactly one axis in common'): ops.matmul(x_lt, y_lt) with self.assertRaises(NotImplementedError): ops.matmul(xy_lt, xy_lt) with self.assertRaisesRegexp(ValueError, 'does not match'): ops.matmul(x_lt, x2_lt)
def test_matrix_matrix(self): xy_lt = core.LabeledTensor(tf.reshape(tf.range(6), (2, 3)), ['x', 'y']) yz_lt = core.LabeledTensor(tf.reshape(tf.range(12), (3, 4)), ['y', 'z']) matmul_lt = ops.matmul(xy_lt, yz_lt) golden_lt = core.LabeledTensor( tf.matmul(xy_lt.tensor, yz_lt.tensor), ['x', 'z']) self.assertLabeledTensorsEqual(matmul_lt, golden_lt) transpose = lambda x: core.transpose(x, list(x.axes.keys())[::-1]) matmul_lt = ops.matmul(xy_lt, transpose(yz_lt)) self.assertLabeledTensorsEqual(matmul_lt, golden_lt) matmul_lt = ops.matmul(transpose(xy_lt), yz_lt) self.assertLabeledTensorsEqual(matmul_lt, golden_lt) matmul_lt = ops.matmul(transpose(xy_lt), transpose(yz_lt)) self.assertLabeledTensorsEqual(matmul_lt, golden_lt) matmul_lt = ops.matmul(yz_lt, xy_lt) self.assertLabeledTensorsEqual(matmul_lt, transpose(golden_lt))
def test_vector_vector(self): x_lt = core.LabeledTensor(math_ops.range(3), ['x']) matmul_lt = ops.matmul(x_lt, x_lt) golden_lt = core.convert_to_labeled_tensor(5) self.assertLabeledTensorsEqual(matmul_lt, golden_lt)
def test_name(self): x_lt = core.LabeledTensor(array_ops.ones((3, )), ['x']) matmul_lt = ops.matmul(x_lt, x_lt) self.assertIn('lt_matmul', matmul_lt.name)
def test_vector_vector(self): x_lt = core.LabeledTensor(math_ops.range(3), ['x']) matmul_lt = ops.matmul(x_lt, x_lt) golden_lt = core.convert_to_labeled_tensor(5) self.assertLabeledTensorsEqual(matmul_lt, golden_lt)
def test_name(self): x_lt = core.LabeledTensor(array_ops.ones((3,)), ['x']) matmul_lt = ops.matmul(x_lt, x_lt) self.assertIn('lt_matmul', matmul_lt.name)