Matrix Subtract like Matrix Multiplication in tensorflow












1














This is my first post I usually found all my answers in the archives, but having a hard time with this one, thanks for the help!



I have two matrix A and B. Performing a matrix multiplication operation is trivial using tf.matmult. But I want to do matrix subtract similar to how matrix multiplication works. Eg if I have.



A = tf.constant([[1, 1, 1, 2, 3, 1],[1,2,3,4,5,6],[4,3,2,1,6,5]])
B = tf.constant([[1,3,1],[2,1,1]])

#B*A
X = tf.matmult(B,A)
>>>X = [[8,10,12,15,24,24],[7,7,7,9,17,13]]


What I want to do is do a similar operation like matmult, but instead of multiply I want subtract and square. Eg...



for x11, where the subscript 11 is row 1, column 1 of matrix X.



= (-b11 + a11)2 + (-b12 + a21)2 + (-b13 + a31)2



and



x12 = (-b11 + a12)2 + (-b12 + a22)2 + (-b13 + a32)2



and so on similar to how matrix multiplication works.



So if we take matrix A and B above and perform the operation described above (call it matmultsubtract), we get,



tf.matmultsubtract(B,A) gives:



[[(-1+1)2+(-3+1)2+(-1+4)2, (-1+1)2+(-3+2)2+(-1+3)2,...],



[(-2+1)2+(-1+1)2+(-1+4)2, (-2+1)2+(-1+2)2+(-1+3)2, ...]]



This isn't that hard if working with numpy arrays (you can use two nested for loops) by iterating manually rather than np.matmult, but tensorflow has a problem with for loops and I'm not sure how to do it.



Thanks for the help.










share|improve this question
























  • If you don't mind me asking, why do you need this? I have never seen someone asking to do that... Matrix multiplication is the way it is because that's how we would compose linear operators, but why would you need a similar structure for [squared] subtraction?
    – Fred
    Dec 28 '18 at 1:07










  • I imagine it has something to do with MSE. With that in mind I'm wondering what you want to do because there might be a better way of doing it
    – Fred
    Dec 28 '18 at 1:08










  • You can do that using tf.while_loop, it's just a bit complex-ish. Let me give it a go
    – Fred
    Dec 28 '18 at 1:11
















1














This is my first post I usually found all my answers in the archives, but having a hard time with this one, thanks for the help!



I have two matrix A and B. Performing a matrix multiplication operation is trivial using tf.matmult. But I want to do matrix subtract similar to how matrix multiplication works. Eg if I have.



A = tf.constant([[1, 1, 1, 2, 3, 1],[1,2,3,4,5,6],[4,3,2,1,6,5]])
B = tf.constant([[1,3,1],[2,1,1]])

#B*A
X = tf.matmult(B,A)
>>>X = [[8,10,12,15,24,24],[7,7,7,9,17,13]]


What I want to do is do a similar operation like matmult, but instead of multiply I want subtract and square. Eg...



for x11, where the subscript 11 is row 1, column 1 of matrix X.



= (-b11 + a11)2 + (-b12 + a21)2 + (-b13 + a31)2



and



x12 = (-b11 + a12)2 + (-b12 + a22)2 + (-b13 + a32)2



and so on similar to how matrix multiplication works.



So if we take matrix A and B above and perform the operation described above (call it matmultsubtract), we get,



tf.matmultsubtract(B,A) gives:



[[(-1+1)2+(-3+1)2+(-1+4)2, (-1+1)2+(-3+2)2+(-1+3)2,...],



[(-2+1)2+(-1+1)2+(-1+4)2, (-2+1)2+(-1+2)2+(-1+3)2, ...]]



This isn't that hard if working with numpy arrays (you can use two nested for loops) by iterating manually rather than np.matmult, but tensorflow has a problem with for loops and I'm not sure how to do it.



Thanks for the help.










share|improve this question
























  • If you don't mind me asking, why do you need this? I have never seen someone asking to do that... Matrix multiplication is the way it is because that's how we would compose linear operators, but why would you need a similar structure for [squared] subtraction?
    – Fred
    Dec 28 '18 at 1:07










  • I imagine it has something to do with MSE. With that in mind I'm wondering what you want to do because there might be a better way of doing it
    – Fred
    Dec 28 '18 at 1:08










  • You can do that using tf.while_loop, it's just a bit complex-ish. Let me give it a go
    – Fred
    Dec 28 '18 at 1:11














1












1








1







This is my first post I usually found all my answers in the archives, but having a hard time with this one, thanks for the help!



I have two matrix A and B. Performing a matrix multiplication operation is trivial using tf.matmult. But I want to do matrix subtract similar to how matrix multiplication works. Eg if I have.



A = tf.constant([[1, 1, 1, 2, 3, 1],[1,2,3,4,5,6],[4,3,2,1,6,5]])
B = tf.constant([[1,3,1],[2,1,1]])

#B*A
X = tf.matmult(B,A)
>>>X = [[8,10,12,15,24,24],[7,7,7,9,17,13]]


What I want to do is do a similar operation like matmult, but instead of multiply I want subtract and square. Eg...



for x11, where the subscript 11 is row 1, column 1 of matrix X.



= (-b11 + a11)2 + (-b12 + a21)2 + (-b13 + a31)2



and



x12 = (-b11 + a12)2 + (-b12 + a22)2 + (-b13 + a32)2



and so on similar to how matrix multiplication works.



So if we take matrix A and B above and perform the operation described above (call it matmultsubtract), we get,



tf.matmultsubtract(B,A) gives:



[[(-1+1)2+(-3+1)2+(-1+4)2, (-1+1)2+(-3+2)2+(-1+3)2,...],



[(-2+1)2+(-1+1)2+(-1+4)2, (-2+1)2+(-1+2)2+(-1+3)2, ...]]



This isn't that hard if working with numpy arrays (you can use two nested for loops) by iterating manually rather than np.matmult, but tensorflow has a problem with for loops and I'm not sure how to do it.



Thanks for the help.










share|improve this question















This is my first post I usually found all my answers in the archives, but having a hard time with this one, thanks for the help!



I have two matrix A and B. Performing a matrix multiplication operation is trivial using tf.matmult. But I want to do matrix subtract similar to how matrix multiplication works. Eg if I have.



A = tf.constant([[1, 1, 1, 2, 3, 1],[1,2,3,4,5,6],[4,3,2,1,6,5]])
B = tf.constant([[1,3,1],[2,1,1]])

#B*A
X = tf.matmult(B,A)
>>>X = [[8,10,12,15,24,24],[7,7,7,9,17,13]]


What I want to do is do a similar operation like matmult, but instead of multiply I want subtract and square. Eg...



for x11, where the subscript 11 is row 1, column 1 of matrix X.



= (-b11 + a11)2 + (-b12 + a21)2 + (-b13 + a31)2



and



x12 = (-b11 + a12)2 + (-b12 + a22)2 + (-b13 + a32)2



and so on similar to how matrix multiplication works.



So if we take matrix A and B above and perform the operation described above (call it matmultsubtract), we get,



tf.matmultsubtract(B,A) gives:



[[(-1+1)2+(-3+1)2+(-1+4)2, (-1+1)2+(-3+2)2+(-1+3)2,...],



[(-2+1)2+(-1+1)2+(-1+4)2, (-2+1)2+(-1+2)2+(-1+3)2, ...]]



This isn't that hard if working with numpy arrays (you can use two nested for loops) by iterating manually rather than np.matmult, but tensorflow has a problem with for loops and I'm not sure how to do it.



Thanks for the help.







python tensorflow matrix matrix-multiplication






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 1:15

























asked Dec 28 '18 at 0:46









Harp

84




84












  • If you don't mind me asking, why do you need this? I have never seen someone asking to do that... Matrix multiplication is the way it is because that's how we would compose linear operators, but why would you need a similar structure for [squared] subtraction?
    – Fred
    Dec 28 '18 at 1:07










  • I imagine it has something to do with MSE. With that in mind I'm wondering what you want to do because there might be a better way of doing it
    – Fred
    Dec 28 '18 at 1:08










  • You can do that using tf.while_loop, it's just a bit complex-ish. Let me give it a go
    – Fred
    Dec 28 '18 at 1:11


















  • If you don't mind me asking, why do you need this? I have never seen someone asking to do that... Matrix multiplication is the way it is because that's how we would compose linear operators, but why would you need a similar structure for [squared] subtraction?
    – Fred
    Dec 28 '18 at 1:07










  • I imagine it has something to do with MSE. With that in mind I'm wondering what you want to do because there might be a better way of doing it
    – Fred
    Dec 28 '18 at 1:08










  • You can do that using tf.while_loop, it's just a bit complex-ish. Let me give it a go
    – Fred
    Dec 28 '18 at 1:11
















If you don't mind me asking, why do you need this? I have never seen someone asking to do that... Matrix multiplication is the way it is because that's how we would compose linear operators, but why would you need a similar structure for [squared] subtraction?
– Fred
Dec 28 '18 at 1:07




If you don't mind me asking, why do you need this? I have never seen someone asking to do that... Matrix multiplication is the way it is because that's how we would compose linear operators, but why would you need a similar structure for [squared] subtraction?
– Fred
Dec 28 '18 at 1:07












I imagine it has something to do with MSE. With that in mind I'm wondering what you want to do because there might be a better way of doing it
– Fred
Dec 28 '18 at 1:08




I imagine it has something to do with MSE. With that in mind I'm wondering what you want to do because there might be a better way of doing it
– Fred
Dec 28 '18 at 1:08












You can do that using tf.while_loop, it's just a bit complex-ish. Let me give it a go
– Fred
Dec 28 '18 at 1:11




You can do that using tf.while_loop, it's just a bit complex-ish. Let me give it a go
– Fred
Dec 28 '18 at 1:11












1 Answer
1






active

oldest

votes


















0














Trying a vectorization operation that may not be taken as matrix subtract.



# shape=(2,3,6)
B_new = tf.tile(tf.expand_dims(B,axis=-1),multiples=[1,1,A.shape[1]])
# shape=(2,3,6)
A_new = tf.tile(tf.expand_dims(A,axis=0),multiples=[B.shape[0],1,1])
# shape=(2,6)
result = tf.reduce_sum(tf.square(A_new - B_new),axis=1)
with tf.Session() as sess:
print(sess.run(result))

[[13 5 1 2 33 25]
[10 6 6 9 42 42]]





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952470%2fmatrix-subtract-like-matrix-multiplication-in-tensorflow%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Trying a vectorization operation that may not be taken as matrix subtract.



    # shape=(2,3,6)
    B_new = tf.tile(tf.expand_dims(B,axis=-1),multiples=[1,1,A.shape[1]])
    # shape=(2,3,6)
    A_new = tf.tile(tf.expand_dims(A,axis=0),multiples=[B.shape[0],1,1])
    # shape=(2,6)
    result = tf.reduce_sum(tf.square(A_new - B_new),axis=1)
    with tf.Session() as sess:
    print(sess.run(result))

    [[13 5 1 2 33 25]
    [10 6 6 9 42 42]]





    share|improve this answer


























      0














      Trying a vectorization operation that may not be taken as matrix subtract.



      # shape=(2,3,6)
      B_new = tf.tile(tf.expand_dims(B,axis=-1),multiples=[1,1,A.shape[1]])
      # shape=(2,3,6)
      A_new = tf.tile(tf.expand_dims(A,axis=0),multiples=[B.shape[0],1,1])
      # shape=(2,6)
      result = tf.reduce_sum(tf.square(A_new - B_new),axis=1)
      with tf.Session() as sess:
      print(sess.run(result))

      [[13 5 1 2 33 25]
      [10 6 6 9 42 42]]





      share|improve this answer
























        0












        0








        0






        Trying a vectorization operation that may not be taken as matrix subtract.



        # shape=(2,3,6)
        B_new = tf.tile(tf.expand_dims(B,axis=-1),multiples=[1,1,A.shape[1]])
        # shape=(2,3,6)
        A_new = tf.tile(tf.expand_dims(A,axis=0),multiples=[B.shape[0],1,1])
        # shape=(2,6)
        result = tf.reduce_sum(tf.square(A_new - B_new),axis=1)
        with tf.Session() as sess:
        print(sess.run(result))

        [[13 5 1 2 33 25]
        [10 6 6 9 42 42]]





        share|improve this answer












        Trying a vectorization operation that may not be taken as matrix subtract.



        # shape=(2,3,6)
        B_new = tf.tile(tf.expand_dims(B,axis=-1),multiples=[1,1,A.shape[1]])
        # shape=(2,3,6)
        A_new = tf.tile(tf.expand_dims(A,axis=0),multiples=[B.shape[0],1,1])
        # shape=(2,6)
        result = tf.reduce_sum(tf.square(A_new - B_new),axis=1)
        with tf.Session() as sess:
        print(sess.run(result))

        [[13 5 1 2 33 25]
        [10 6 6 9 42 42]]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 28 '18 at 3:57









        giser_yugang

        1,3941318




        1,3941318






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952470%2fmatrix-subtract-like-matrix-multiplication-in-tensorflow%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas