Import NengoDL in Google Colab

Hello, I’m trying to follow spiking MNIST tutorial using Colab. [Colab code]

I added a code cell at the beginning to install Nengo&NengoDL,

!pip install nengo
!pip install nengo-dl

then tried to import some libraries.

%matplotlib inline

from urllib.request import urlretrieve

import nengo
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

import nengo_dl

But an error occurred at import nengo_dl.

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-8-01afe5e72f23> in <module>()
      8 import matplotlib.pyplot as plt
      9 
---> 10 import nengo_dl

/usr/local/lib/python3.6/dist-packages/nengo_dl/__init__.py in <module>()
     56 
     57 # import into top-level namespace
---> 58 from nengo_dl import callbacks, compat, converter, dists, losses
     59 from nengo_dl.config import configure_settings
     60 from nengo_dl.converter import Converter

/usr/local/lib/python3.6/dist-packages/nengo_dl/callbacks.py in <module>()
     18 from tensorflow.python.eager import context
     19 
---> 20 from nengo_dl import compat, utils
     21 
     22 

/usr/local/lib/python3.6/dist-packages/nengo_dl/compat.py in <module>()
     12 import tensorflow as tf
     13 from tensorflow.python.keras import backend
---> 14 from tensorflow.python.keras.engine import network
     15 
     16 # TensorFlow compatibility

ImportError: cannot import name 'network'

Is there anything I have to know when running NengoDL project in Colab?
It seems to be okay when NengoDL is imported in local environment.
(using Anaconda, Python 3.7.7, TensorFlow 2.1.0, Nengo 3.0.0, NengoDL 3.2.0)

Colab probably uses the latest TensorFlow 2.3.0 as default and NengoDL has not yet been updated to support the changes that were introduced (see https://github.com/nengo/nengo-dl/issues/167#issuecomment-668575023).

Until NengoDL is updated, a workaround is to:

  • add this cell to your imports:

!git clone https://github.com/nengo/nengo-dl.git
!pip install -e ./nengo-dl

  • Restart the runtime

  • Run this cell:

import nengo_dl

I tested it and it works.

3 Likes

Thank you for your kind answer!
As another workaround, I downgraded TensorFlow to 2.1.0 version, and it worked well :slightly_smiling_face:

1 Like

Just be aware that installing TensorFlow using pip on colab is discouraged as the version pulled doesn’t have all the optimisations that the default colab build has.

1 Like

This should be resolved now, as NengoDL 3.3.0 has just been released and is compatible with TF 2.3. :sunny:

2 Likes