Hardware issues (M1)

Any thoughts on how to move forward if “correct” code happens not to run on your hardware. For example, when completing C2_W3_Assignment, 5.1 - Exercise 3 would NOT fit using my M1 chip (same code ran fine on the virtual server). Error message:

InvalidArgumentError: Cannot assign a device for operation Complex/L1/MatMul/ReadVariableOp: Could not satisfy explicit device specification ‘’ because the node {{colocation_node Complex/L1/MatMul/ReadVariableOp}} was colocated with a group of nodes that required incompatible device ‘/job:localhost/replica:0/task:0/device:GPU:0’. All available devices [/job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:GPU:0].

When I searched Google, it is apparently a known issue:


Is there a common way to test code on different machines just to rule out hardware issues?

– Sam


It seems that tensorflow doesn’t have official support for GPU on mac.
See: Install TensorFlow with pip

Apple has released a plugin Tensorflow Plugin - Metal - Apple Developer
Try it and see if it fixes the problem.

Otherwise, I suggest turning off GPU support so all operations run on CPU with this command in the first cell of the notebook,

# Hide GPU from visible devices
tf.config.set_visible_devices([], 'GPU')

I uninstalled tensorflow-metal, i.e. package to run tensorflow on M1 chip and code ran.

– Sam

I tried both the commands below to force the CPU to run.

# Hide GPU from visible devices
# tf.device("/cpu:0")
tf.config.set_visible_devices([], 'GPU')

No luck. Uninstalling the tensorflow-metal package as stated above was the only work around. Thanks for researching in parallel!

– Sam

Instead of uninstalling TensorFlow-metal completely, I think it would be wiser if we create a new virtual environment(like conda env) to install normal TensorFlow.

(For C2_W3_Assignment Specifically)
Edit : It works well in google colab. Little work has to be done to take care for path accessing in certain files and in mounting drive

For Mounting run the below command in colab cell:

from google.colab import drive 

Create a folder say Graded-Assignment-3 in drive
In order to import files as modules within directory run the below command :

import sys
sys.path.insert(0,’/content/drive/My Drive/Graded-Assignment-3’)

With which files within this directory can also be imported into the notebook.

Finally to display plots, ie to enable third party Jupiter widgets run the command below :

from google.colab import output

One more edit have to be done, in line 25 of assignment_utis.py file

plt.style.use('/content/drive/My Drive/Graded-Assignment-3/deeplearning.mplstyle')

Agreed. I wasn’t happy with uninstalling metal either. I finally got SamReiswig’s solution to work. You have to call set_visible_devices BEFORE you create the model, not BEFORE the fit() call. Everything works leveraging just the CPU!

– Sam

# Hide GPU from visible devices
# tf.device("/cpu:0")
print('Devices: ', tf.config.get_visible_devices())
tf.config.set_visible_devices([], 'GPU')
print('Deactivate GPU: ', tf.config.get_visible_devices())

model = Sequential(
        ### START CODE HERE ### 
        Dense(120, activation='relu', name="L1"),
        Dense(40, activation='relu', name="L2"),
        Dense(6, activation='linear', name="L3")
        ### END CODE HERE ### 
    ], name="Complex"