Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

invalidArgumentError required broadcastable shapes at locUnknown.

Home/ Questions/Q 302
Next
Answered
invalidArgumentError required broadcastable shapes at locUnknown.
jcortez1706
jcortez1706 Begginer

I am a coding newbie and I am trying to run a machine learning program on my Python environment. However, I am encountering some issues with the shapes of arrays and broadcasting them. I am quite unsure of how to handle this situation.
I have a series of arrays with differing shapes that I want to broadcast together to find their dot product. However, when I run my code, I get an `InvalidArgumentError` saying that the shapes of the arrays are not broadcastable. I am not sure how to go about rectifying this error. Here’s an example of my code:

import tensorflow as tf
a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([10, 20])
c = tf.constant([[100], [200]])
result = tf.tensordot(a*b, c, axes=1)
with tf.session() as sess:
output = sess.run(result)

Can someone please help me understand why I am not able to broadcast the arrays and what I can do to fix this code? Thank you in advance for your help.

arraysbroadcastingdebugginginvalidargumenterrornumpynumpy-broadcastingpython
  • 677
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    rachel.westphal Teacher
    2018-12-16T14:13:28+00:00Added an answer about 4 years ago

    Hello and welcome! I would be happy to help you with your question. From what you’ve described, it sounds like you are experiencing an “InvalidArgumentError: required broadcastable shapes” error in your code. This error typically occurs when you are trying to perform an operation on two arrays that are not compatible with each other.
    To fix this error, you will need to ensure that the shapes of the two arrays you are trying to operate on are compatible with each other. You can do this by checking the shape of each array using the “shape” attribute and making sure they have the same number of dimensions.
    For example, if you are trying to perform a multiplication operation on two arrays, you will need to make sure that they have the same number of dimensions and that the size of each dimension is either equal or 1. If the dimensions are not equal and not 1, then you will need to use the “broadcasting” rules to adjust the shapes to be compatible with each other.
    In terms of checking the shape of each array, you can print the shape of each array using the following code:
    “`
    import numpy as np
    arr1 = np.array([1,2,3])
    arr2 = np.array([4,5,6])
    print(arr1.shape)
    print(arr2.shape)
    “`
    This will print the shape of each array to the console. If the shapes are different, you will need to adjust one or both of the arrays to make them compatible with each other.
    I hope this helps! Let me know if you have any other questions.

    • 47
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. mauricio_guarda Begginer
    2018-12-24T18:41:47+00:00Added an answer about 4 years ago

    One possible reason for the “InvalidArgumentError: required broadcastable shapes at loc unknown” could be due to mismatched shapes of the arrays or tensors being used. It’s important to ensure that the dimensions of your inputs are compatible and can be broadcasted without any issues. You can try printing the shapes of your arrays or tensors using the shape attribute to check if they align correctly.
    In my experience, double-checking the shapes of my inputs has saved me a lot of time debugging issues. If you’re unsure about the shape of your arrays or tensors, you can also try reshaping them or adding dimensions as needed, using functions like reshape() or expand_dims(). This can ensure that the inputs match the expected shape of the model or function.
    It’s also important to keep in mind the broadcasting rules in NumPy or TensorFlow, depending on what you’re using. Broadcasting rules can vary depending on the situations and understanding them can help in preventing similar errors. In summary, making sure that your inputs have compatible shapes and understanding the broadcasting rules can go a long way in resolving this error.

    • 47
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. crismendieta94 Teacher
    2018-12-19T23:22:18+00:00Added an answer about 4 years ago

    One possible solution to the error is checking the dimensions of the arrays involved in the operation. If two arrays do not have the same number of dimensions and shapes, the error can be thrown. Another possibility is that the error could be caused by using incompatible data types, such as trying to concatenate strings with numbers.
    In my experience, I once encountered a similar problem while working on a Python project. I was trying to manipulate two numpy arrays with different shapes and I kept getting a value error. It took me a while to realize that the issue was with the different shapes of the arrays. Once I resolved the issue by ensuring that both arrays had the same shape, the error was solved.
    Therefore, I would suggest thoroughly checking and double-checking the dimensions and data types of the arrays involved in the operation causing the error. From my experience, this often resolves errors similar to what you are experiencing.

    • 32
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  4. adri_wy Teacher
    2018-12-31T13:54:00+00:00Added an answer about 4 years ago

    One possible reason for the “invalid argument error: required broadcastable shapes at loc unknown” message is that there’s an issue with the dimensions of the arrays you are trying to operate on. The error message might indicate that NumPy is expecting the arrays to have compatible shapes because you’re trying to perform an operation that involves arrays with different dimensions, but it doesn’t know where exactly the issue is arising from.
    If you’re not sure where the dimensions are mismatched, you could try printing the shapes of the arrays you’re working with using the `shape` method before you perform any operations on them. This will help you identify any inconsistencies that might be causing the error. Then, you could try to reshape the arrays or perform operations that would make them compatible.
    Another thing you could try is to explicitly specify the axis along which you want to perform the operation, using the `axis` parameter. This can help avoid any confusion that might arise from arrays with different dimensions.

    In conclusion, “invalid argument error: required broadcastable shapes at loc unknown” is often an indicator that there’s a dimensions-related mismatch in the arrays you’re trying to work with. The best approach is to identify where exactly the mismatch is occurring, and then take steps to address it, such as reshaping the arrays or using the `axis` parameter to explicitly specify the axis along which you want to perform the operation.

    • 9
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  5. punkprincess_joy Teacher
    2018-12-31T20:21:43+00:00Added an answer about 4 years ago

    To avoid the `InvalidArgumentError: Required broadcastable shapes` error, you need to ensure that the shapes of the tensors you are trying to perform an operation with are compatible. You can use NumPy’s broadcasting feature to help with shape compatibility. Understanding broadcasting is particularly helpful when working with data of different shapes.

    Let’s say you have two tensors that you want to add together. The shapes of the tensors are `(4, 3)` and `(4, 1)`. Broadcasting the shape `(4, 1)` of the second tensor to shape `(4, 3)` of the first tensor makes it possible to add the two tensors elementwise.

    Here’s an example:

    “`python
    import numpy as np

    x = np.ones((4, 3))
    y = np.zeros((4, 1))

    result = x + y

    print(result.shape) # outputs: (4, 3)
    “`

    In this example, we use NumPy’s `ones` and `zeros` functions to create two matrices of different shapes. We then add them together, which would usually throw the `InvalidArgumentError`. However, since we broadcast the shape of the second matrix, we can perform the operation elementwise and avoid the error.

    • 4
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.