Installing Coursera utilities in local computer

In trying to implement Deep Learning models in my computer environment, I often find that utilities/modules/libraries are not recognized, not found. They are, evidently, written by Coursera staff for the assignments. Say, tf_utils.

How do I incorporate them and make them work in my environment? Or, can I at least see the code and reproduce it or take it as example? Right now I need tf_utils because I want to encode my training labels as one-hot. But the question is quite general.

Hi, @EduardoChicago.

Select FileOpen from the notebook menu:

You will be taken to the file browser:

files

Have fun! :slight_smile:

1 Like

Thanks Ramón, will do.
As for the one-hot conversion issue, I found this script, which works very well, by StackOverflowUser2010.

import numpy as np

def convertToOneHot(vector, num_classes=None):
    """
    Converts an input 1-D vector of integers into an output
    2-D array of one-hot vectors, where an i'th input value
    of j will set a '1' in the i'th row, j'th column of the
    output array.

    Example:
        v = np.array((1, 0, 4))
        one_hot_v = convertToOneHot(v)
        print one_hot_v

        [[0 1 0 0 0]
         [1 0 0 0 0]
         [0 0 0 0 1]]
    """

    assert isinstance(vector, np.ndarray)
    assert len(vector) > 0

    if num_classes is None:
        num_classes = np.max(vector)+1
    else:
        assert num_classes > 0
        assert num_classes >= np.max(vector)

    result = np.zeros(shape=(len(vector), num_classes))
    result[np.arange(len(vector)), vector] = 1
    return result.astype(int)

Thanks Ramón. But where is the (presumably Coursera-written) tf_utils?
If I attempt to import it

from tensorflow import tf_utils

in the local environment, it can’t be found.

If you try to run code from a Coursera notebook locally, that code may require custom Python modules written by the course staff, like tf_utils, that are not present in your machine. You can use the file browser from the Coursera notebook to look for them and download them.

From the name of the module, I assumed you were referring to this notebook, but you can use the same procedure if it’s a different one.

Let me know if you get it to work. And thanks for sharing the script! :slight_smile:

1 Like

Hello Ramón, I have a related question that belongs in this Discussion item.
When testing the Identity_block function in the ResNets50 Lab, the lab test for Exercise 1 (Identity block) includes these lines

A3np = A3.numpy()
print(np.around(A3.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))
resume = A3np[:,(0,-1),:,:].mean(axis = 3)
print(resume[1, 1, 0] , resume[1, 1, 0])

This causes the creation of A3np, which we are told is not a tensor (A3 is a tensor).
When I try to execute these lines in my (tf) environment, I get an error

----> 1 A3np = A3.numpy()
AttributeError: ‘Tensor’ object has no attribute ‘numpy’

Evidently, the rules in my environment are different. How do I change the local environment to match Coursera’s? Or, how do I reformulate

A3np = A3.numpy()

in a way that is more universally interpretable?
(Note. I asked the same to Rainaud, under a different topic. I want to make sure you see it too. Sorry for the duplication).

You’re probably running an old version of TensorFlow, as @paulinpaloalto suspects :slight_smile:

Try updating it.

1 Like

Just did it. Quite an ordeal!