C3_W2_Practice1 | Not a problem, just curious about vectorization


I calculated the cost using vectorization for exercice 1, and the code I used worked but is very different from the vectorized code that is given to us after the exercice.

The code I used:
{Moderator’s Edit: Solution Code Removed}

Code given to us:
{Moderator’s Edit: Solution Code Removed}

I am curious about the different ‘fonctions’ I used vs the ones used in the notebook. I personally used np.sum(), np.dot, np.multiply() and * since this is what I learned watching Andrew’s courses, but is there something wrong with it, or is it more computationally efficient to use TensorFlow fonctions? Apart from the code maybe looking cleaner than what I did.
For exemple I used a lot of transposes, could that be a problem?

Also, that moment when you manage to do the vectorized implementation first and it works and then they tell you that you can try to do the vectorized way yourself if you are an “expert” in linear algebra (which I’m not), man that feels good :smiley:
So thank you to Andrew, DeepLearning.AI, Stanford and everyone who worked on the course!

Thank you,


Hey @Axelibnida,

There’s nothing wrong with using different functions from different packages, as long as you have correctly implemented the formulation, and it works in code. However, when the question comes to “Computational Efficiency”, one might have some reason to believe that Tensorflow functions will supposedly work better on Tensorflow data and Numpy functions will supposedly work better on Numpy data. Now, I am not familiar with whether these packages inherently converts the data from one format to another, but if they do, then I am sure there must be a “conversion cost” involved, and depending on how much it is, you can decide whether to use Tensorflow functions or Numpy functions. If it’s considerable, then there’s no harm in using the functions from the package to which the data belongs to, and if it’s not, then you can use either, no issues in that!

Now, as to the above query, I am not sure as to how much computation is required to take the transpose of a matrix, but even if we assume that 0-computation is required, taking unessential transpose is only going to reduce the ease of reading your code, so, if they can be eliminated, why not to do it?

I have DM you a less transposed and a more readable form of the same code as yours. I hope that helps.

P.S. - It is against the community guidelines to post any solution code publicly. If any mentor needs to take a look at your code, he/she will ask you to DM it.


1 Like