Beta

Weird matrix multiplication

Description
Loading description...
Fundamentals
NumPy
Matrix
  • Please sign in or sign up to leave a comment.
  • Blind4Basics Avatar
    • seems you have a lot of doule assertions/tests
    • your random tests seem wrong, aren't they?

    ***** mark a call to my function, then I print A, B and output:

    ******
    []
    -----
    [[0.91697826 0.58649042 0.97603993 ... 0.06190448 0.05158904 0.92087939]
     [0.00909259 0.96935466 0.00113195 ... 0.7943393  0.88921109 0.82804434]
     [0.8280752  0.71887745 0.9913926  ... 0.07471627 0.712064   0.3432194 ]
     ...
     [0.06660736 0.02377495 0.68543642 ... 0.00815495 0.92083984 0.67133174]
     [0.34291357 0.9817805  0.69119256 ... 0.55731202 0.89012486 0.26771119]
     [0.66525979 0.85857665 0.25828924 ... 0.90030969 0.93074113 0.97010068]]
    -----
    []
    ******
    []
    -----
    [[0.91697826 0.58649042 0.97603993 ... 0.06190448 0.05158904 0.92087939]
     [0.00909259 0.96935466 0.00113195 ... 0.7943393  0.88921109 0.82804434]
     [0.8280752  0.71887745 0.9913926  ... 0.07471627 0.712064   0.3432194 ]
     ...
     [0.06660736 0.02377495 0.68543642 ... 0.00815495 0.92083984 0.67133174]
     [0.34291357 0.9817805  0.69119256 ... 0.55731202 0.89012486 0.26771119]
     [0.66525979 0.85857665 0.25828924 ... 0.90030969 0.93074113 0.97010068]]
    -----
    []
    (0, 0) should equal (318, 0)  <<< What the...? :o
    
    • FuffiKFuffiK Avatar

      Thanks for the feedback, I will checke everything again tomorrow :)

    • Blind4Basics Avatar

      (note: I meant "double assertions", like in the above, there asre two calls with the same inputs)

    • FuffiKFuffiK Avatar

      I fixed both issues: there are no more double calls in random tests as well as there are no more mistakes there (caused by generating 0-shape matrices).

      Also I have changed the description and the solution: now if warrior tries to pass wrong- or 0-shaped matrices, function should return None.

      Issue marked resolved by FuffiKFuffiK 7 years ago
    • Blind4Basics Avatar

      Almost good! Still 2 3 problems, though:

      • you still have a double assertion in the sample tests.
      • use the Test.it decorator for the random tests instead of the Test.describe one
      • third test in "Edge cases" part:

      I encounter that error:

      Unexpected exception raised

      A = [[1 2]
           [3 4]]
      B = [6.94965891e-310 4.68139363e-310 6.94965966e-310 4.68133491e-310
           4.68139350e-310 4.79243676e-322 4.68139369e-310 4.68139354e-310
           6.94965871e-310 1.72922976e-322]
      my output:
      [[6.94965891e-310 1.38993178e-309]
       [4.68139363e-310 9.36278726e-310]
       [6.94965966e-310 1.38993193e-309]
       [4.68133491e-310 9.36266983e-310]
       [4.68139350e-310 9.36278701e-310]
       [4.79243676e-322 9.58487353e-322]
       [4.68139369e-310 9.36278738e-310]
       [4.68139354e-310 9.36278709e-310]
       [6.94965871e-310 1.38993174e-309]
       [1.72922976e-322 3.45845952e-322]
       [2.08489767e-309 2.77986356e-309]
       [1.40441809e-309 1.87255745e-309]
       [2.08489790e-309 2.77986387e-309]
       [1.40440047e-309 1.87253397e-309]
       [1.40441805e-309 1.87255740e-309]
       [1.43773103e-321 1.91697471e-321]
       [1.40441811e-309 1.87255748e-309]
       [1.40441806e-309 1.87255742e-309]
       [2.08489761e-309 2.77986348e-309]
       [5.18768928e-322 6.91691904e-322]]
       
       
       Traceback (most recent call last):
        File "/runner/frameworks/python/cw-2.py", line 83, in wrapper
          try: func()
        File "main.py", line 79, in edge_tests
          test.assert_equals(weird_mul(A, C), None, 'Should return None in case of 1d input')
        File "/runner/frameworks/python/cw-2.py", line 31, in assert_equals
          expect(actual == expected, message, allow_raise)
        File "/runner/frameworks/python/cw-2.py", line 15, in expect
          if passed:
      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
      

      Seems to me your B input is not valid ;)

    • FuffiKFuffiK Avatar

      Hi!

      1. It is done intentionally: to show that output of weird_mul(A, B) and weird_mul(B, A) is the same in first case (the calls of function are formally different)
      2. fixed
      3. In the description I have marked that if the shape of the input matrix is not 2d, function should return None, so I guess in this example (with not valid B) you have an output, which is a 2d matrix and the test to compare it to None fails. But in my opininon, it is a problem of the limitations of test framework, not working with 2d matrices. No idea how to fix this. This is the code I use here:

      test.assert_equals(weird_mul(A, C), None, 'Should return None in case of 1d input')

      Again, if the output is 2d numpy array assertion throws an error

    • Blind4Basics Avatar
      1. x/ damn, I didn't even see the inversion of the parameters... :/
      2. /
      3. ah ok, my bad again (I didn't read the description again). So maybe just add meaningful feedback to the assertion?
    • FuffiKFuffiK Avatar

      Thanks for your feedback :)

  • Blind4Basics Avatar

    the assertions are still done the wrong way: you're using assert_equals exactly in the manner of expect!

    Traceback (most recent call last):
      File "/runner/frameworks/python/cw-2.py", line 83, in wrapper
        try: func()
      File "main.py", line 20, in example_tests
        test.assert_equals((weird_mul(A, B) == AB).all(), True)
    AttributeError: 'bool' object has no attribute 'all'
    

    so, absolutely not that:

    test.assert_equals((weird_mul(B, A) == AB).all(), True)
    

    but that instead:

    test.assert_equals(weird_mul(A, B), AB)
    
    • FuffiKFuffiK Avatar

      I have used decorators, fixed the input from A = [2] to A = [[2]], but...

      I can't use

      test.assert_equals(weird_mul(A, B), AB)

      I've got an error:

      ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    • Blind4Basics Avatar

      Mmmmh... :/

      Seems related to the fact I know almost shit to numpy. The assertion worked in the trainer, on my side. I return a basic np.array([...]) instance.

    • FuffiKFuffiK Avatar

      It works with 1d arrays, but not with 2d arrays.

    • Blind4Basics Avatar

      you didn't add the new sample tests to the full test suite

    • Blind4Basics Avatar

      and I believe you should change the way you test for the edge cases:

      A: [[1 2]
         [3 4]]
      -----
      B: []
      -----
      output: []
      Should return (0, 0)-shape array in case of 0-shape input: (0,) should equal (0, 0)
      

      an empty numpy array should pass (EDIT: errr... Or are you actually expecting [[]]? x) -> told you, I know shit to numpty x) ).

      Note: why the hell are there more than 3500 tests!??? Moreover, it seems you do a lot of tests in the same output, that's weird.

      And I still get that:

      Unexpected exception raised

      Traceback (most recent call last):
        File "/runner/frameworks/python/cw-2.py", line 83, in wrapper
          try: func()
        File "main.py", line 110, in random_tests
          test.assert_equals((AB[i*m: (i+1)*m, j*n: (j+1)*n] == A[i,j]*B).all(), True)
      IndexError: too many indices for array
      
    • Blind4Basics Avatar

      well ok, ignore the message above, I'm resolving that part: with the 'almost correct" code, the assertion works. But that means that users will have A LOT of problems trying to achieve the task, since they'll get tones of error messages as long as they don't do the "correct" stuff. And that means you'll have a lot of issues raised here...

      see the new one at the top, now. ;)

      Issue marked resolved by Blind4Basics 7 years ago
  • Blind4Basics Avatar

    Not enough fixed/sample tests:

    • you should provide at least one other sample test with different shapes for the inputs
    • and at least one of the edge cases (all of them could be good too)

    and your Check resulting shape test fails without saying anything about what's encountered and moreover what is expected.

    • Blind4Basics Avatar

      This comment has been hidden.

    • FuffiKFuffiK Avatar

      I have done more examples and tried to described 'edge_classes' example in the description

      Issue marked resolved by FuffiKFuffiK 7 years ago
    • Blind4Basics Avatar
      • are you sure about that kind of input: A=[2]? Shouldn't it be A=[[2]]?
      • you mixed the two frameworks functionnalities. Use rather the decorators so that the functions are launched automatically

      Like the following:

      @test.describe('Fixed_tests')
      def fixed_tests():
      
      
          @test.it('Example_Cases')
          def example_tests():
      
              
              A = np.array([2])
      
              B = np.array([[1, 2],
                            [3, 4]])
      
              AB = np.array([[2, 4],
                             [6, 8]])
              
               
              test.assert_equals((weird_mul(A, B) == AB).all(), True)
              test.assert_equals((weird_mul(B, A) == AB).all(), True)
      
              A = np.array([2, 3])
      
              B = np.array([[40],
                            [50]])
      
              AB = np.array([[80,  120],
                             [100, 150]])
          
              test.assert_equals((weird_mul(A, B) == AB).all(), True)
      
              A = np.array([[0, 1],
                            [2, 3]])
      
              B = np.array([[1, 1, 1],
                            [1, 1, 1],
                            [1, 1, 1],
                            [1, 1, 1]])
              
              AB = np.array([[ 0,  0,  0,  1,  1,  1],
                             [ 0,  0,  0,  1,  1,  1],
                             [ 0,  0,  0,  1,  1,  1],
                             [ 0,  0,  0,  1,  1,  1],
                             [ 2,  2,  2,  3,  3,  3],
                             [ 2,  2,  2,  3,  3,  3],
                             [ 2,  2,  2,  3,  3,  3],
                             [ 2,  2,  2,  3,  3,  3]])
      
              test.assert_equals((weird_mul(A, B) == AB).all(), True)
          
              A = np.array([[1, 2],
                            [3, 4]])
      
              B = np.array([[10, 20, 30],
                            [40, 50, 60]])
               
              AB = np.array([[ 10,  20,  30,  20,  40,  60],
                             [ 40,  50,  60,  80, 100, 120],
                             [ 30,  60,  90,  40,  80, 120],
                             [120, 150, 180, 160, 200, 240]])
                         
              test.assert_equals((weird_mul(A, B) == AB).all(), True)
          
          @test.it('Edge_Cases')
          def edge_tests():
      
              
              A = np.array([[1, 2],
                            [3, 4]])
      
              B = np.zeros(shape=(0, 0))
          
              test.assert_equals(weird_mul(A, B).shape, (0, 0), 'Should return (0, 0)-shape array in case of 0-shape input')
              
      
  • Blind4Basics Avatar

    Hi,

    Well... don't EVER use Test.expect if you do no provide a meaningful error message: 'Array does not have correct value' without telling what is the expected output, without saying what are the input either (that part isn't mandatory) is not meaningful.

    All the same, the way you do the checks is plainly wrong. Try this:

    A = np.array([[1, 2],
                 [3, 4]])
    
    B = np.array([[10, 20, 30],
                 [40, 50, 60]])
         
    AB = np.array([[ 10,  20,  30,  20,  40,  60],
                  [ 40,  50,  60,  80, 100, 120],
                  [ 30,  60,  90,  40,  80, 120],
                  [120, 150, 180, 160, 200, 240]])
    
    test.expect((A == AB).all, 'Array does not have correct value')
    
    => gives you:
    
    Traceback (most recent call last):
      File "main.py", line 20, in <module>
        test.expect((A == AB).all, 'Array does not have correct value')
    AttributeError: 'bool' object has no attribute 'all'
    

    Meaning that most of the time, the tests won't even run if the solution isn't correct.

    CCL: do the checks in a "normal manner": use test.assert_equals.