C2_W1_Assignment: error with sigmoid function call (local Jupyter Notebook environment)

Dear all, I can’t call “g(z)” in “def my_dense(a_in, W, b, g)”, Exercise 2 because I get the following error:

“ValueError: setting an array element with a sequence.”.

If I hard code the sigmoid function I receive the right expected output.

If I print the first value g(z) in the for loop I get:

Tensor(“Sigmoid:0”, shape=(), dtype=float64)

Why is g(z) a tensor or a matrix or 2D array with a specific shape?

I have loaded the sigmoid function using “from autils import *”. Restarting the kernel and run all did not help. My environment is setup locally.

Thank you!

HI @Daniel_Blossey

The code of you is correct but There are something note clear to me as the error of setting an array element with a sequence raise because of:

  • trying to create a jagged array
  • providing elements of incompatible types(that’s I think)

so that try to check you didn’t change the type of lists and didn’t change the cells before this cell, make sure you run the cells before it specially the library cell, and if the error is still you can get new fresh assignment by doing these steps:
file → open → select running assignment and doing shutdown and select all file and delete it → after that select help

like this image and choose →


I suspect your error has something to do with your environment - maybe version of TF you’re using, or something you loaded with a different def for sigmoid?

Here’s what I’m seeing when I run in the coursera environment:

  • My TF version is 2.8.0

  • When I added a print(z) as well as the print(g(z)) that you have, this is what the first pair of results looked like when I ran the “Quick Check” cell:

tf.Tensor(0.5473576181430894, shape=(), dtype=float64)
  • When I copy the code for the sigmoid function from autils and paste it in the assignment file (I put it in the same cell as my_dense, and re-executed that cell), the first pair of results look like this:

I don’t understand why I’m seeing a tf.Tensor result when sigmoid is defined in autils, but just a scalar when it’s in the assignment file. I also have no idea why you’re seeing Tensor (no tf., like I see), and “Sigmoid:0” instead of the number I’m seeing.

I know this is not an answer to what’s going on in your environment, but this might give you some more info to help you track it down.
And… if you just want to get things working, it might be a work-around for you to copy the sigmoid function into your assignment file. Just remember to remove it before checking in your assignment, since they grader may be picky about it.

I think the notebook is pickup up an incompatible sigmoid() function.

I investigated a similar issue to this some time ago, and I found that the sigmoid() that’s being used isn’t the one from autils.py. It’s a sigmoid() function provided by TF.

So the error supports the theory that the issue is with the TensorFlow version.

Thanks for your answers! I have upgraded tensorflow from 1.15.5 to 2.8.0. But still the error appears if I would like to call the method “sigmoid” of file “autils.py”.

If I change the method name “sigmoid” to e.g. “sigmoid_123” in file “autils.py” my notebook cell works.

Here is my list of installed packages:

[osboxes@localhost exercise_solutions_self]$ pip list
Package                      Version
---------------------------- -------------------
absl-py                      1.3.0
ale-py                       0.8.0
anyio                        3.6.2
appdirs                      1.4.4
argon2-cffi                  21.3.0
argon2-cffi-bindings         21.2.0
asn1crypto                   0.24.0
astor                        0.8.1
astunparse                   1.6.3
atari-py                     0.2.9
attrs                        22.1.0
backcall                     0.2.0
Beaker                       1.10.0
beautifulsoup4               4.11.1
bleach                       5.0.1
blivet                       3.1.3
blivet-gui                   2.1.10
box2d-py                     2.3.5
Brlapi                       0.6.7
bsddb3                       6.2.6
cachetools                   5.3.0
cffi                         1.11.5
chardet                      3.0.4
chrome-gnome-shell           0.0.0
click                        8.1.3
cloudpickle                  1.6.0
colorama                     0.4.6
coverage                     4.5.1
cryptography                 2.6.1
cupshelpers                  1.0
cycler                       0.11.0
dbus-python                  1.2.8
debugpy                      1.6.3
decorator                    4.3.0
defusedxml                   0.7.1
distro                       1.4.0
EasyProcess                  1.1
entrypoints                  0.4
fastjsonschema               2.16.2
filelock                     3.8.0
flatbuffers                  23.1.21
fros                         1.1
future                       0.18.2
gast                         0.2.2
gitdb                        4.0.9
GitPython                    3.1.29
google-auth                  2.16.0
google-auth-oauthlib         0.4.6
google-pasta                 0.2.0
gpg                          1.12.0
gramps                       5.1.2
graphviz                     0.16
grpcio                       1.50.0
gym                          0.20.0
h5py                         2.10.0
humanize                     0.5.1
idna                         2.8
imageio                      2.9.0
importlib-metadata           5.0.0
importlib-resources          5.10.0
ipykernel                    6.16.2
ipython                      7.34.0
ipython-genutils             0.2.0
ipywidgets                   8.0.2
jedi                         0.18.1
Jinja2                       3.1.2
joblib                       0.14.1
jsonschema                   4.16.0
jupyter                      1.0.0
jupyter_client               7.4.4
jupyter-console              6.4.4
jupyter_core                 4.11.2
jupyter-server               1.21.0
jupyterlab-pygments          0.2.2
jupyterlab-widgets           3.0.3
keras                        2.8.0
Keras-Applications           1.0.8
Keras-Preprocessing          1.1.2
kiwisolver                   1.4.4
langtable                    0.0.41
Mako                         1.0.6.dev0
Markdown                     3.4.1
MarkupSafe                   2.1.1
matplotlib                   3.3.4
matplotlib-inline            0.1.6
menulibre                    2.2.1
mistune                      2.0.4
nbclassic                    0.4.7
nbclient                     0.7.0
nbconvert                    7.2.3
nbdime                       2.1.0
nbformat                     5.7.0
nest-asyncio                 1.5.6
networkx                     2.6.3
nltk                         3.5
notebook                     6.5.2
notebook_shim                0.2.0
ntplib                       0.3.3
numexpr                      2.7.2
numpy                        1.20.0
oauthlib                     3.2.2
olefile                      0.46
opt-einsum                   3.3.0
ordered-set                  2.0.2
packaging                    21.3
pandas                       1.2.2
pandocfilters                1.5.0
parso                        0.8.3
Paste                        2.0.3
pexpect                      4.8.0
pickleshare                  0.7.5
pid                          2.2.3
Pillow                       8.2.0
pip                          22.3
pkgutil_resolve_name         1.3.10
ply                          3.11
productmd                    1.21
prometheus-client            0.15.0
prompt-toolkit               3.0.31
protobuf                     3.20.3
psutil                       5.6.7
ptyprocess                   0.7.0
pwquality                    1.4.0
pyasn1                       0.4.8
pyasn1-modules               0.2.8
pycairo                      1.18.0
pycparser                    2.14
pycrypto                     2.6.1
pycups                       1.9.74
pydbus                       0.6.0
pyenchant                    2.0.0
pyglet                       1.5.0
Pygments                     2.13.0
PyGObject                    3.32.0
PyICU                        2.2
pyinotify                    0.9.6
pykickstart                  3.20
PyOpenGL                     3.1.5
PyOpenGL-accelerate          3.1.5
pyOpenSSL                    19.0.0
pyparsing                    2.4.0
pyparted                     3.11.2
pyrsistent                   0.19.1
PySocks                      1.6.8
python-augeas                0.5.0
python-dateutil              2.8.2
python-meh                   0.47
pytz                         2018.5
pyudev                       0.21.0
PyVirtualDisplay             2.2
PyWavelets                   1.3.0
pyxdg                        0.26
pyzmq                        24.0.1
qtconsole                    5.3.2
QtPy                         2.2.1
regex                        2022.10.31
requests                     2.21.0
requests-file                1.4.3
requests-ftp                 0.3.1
requests-oauthlib            1.3.1
rsa                          4.9
scikit-image                 0.18.1
scikit-learn                 0.24.1
scipy                        1.6.0
Send2Trash                   1.8.0
sepolicy                     1.1
setools                      4.1.1
setuptools                   65.5.0
simpleline                   1.4
six                          1.12.0
slip                         0.6.4
slip.dbus                    0.6.4
smmap                        5.0.0
sniffio                      1.3.0
sos                          3.7
soupsieve                    2.3.2.post1
systemd-python               234
Tempita                      0.5.1
tensorboard                  2.8.0
tensorboard-data-server      0.6.0
tensorboard-plugin-wit       1.8.1
tensorflow                   2.8.0
tensorflow-estimator         2.8.0
tensorflow-io-gcs-filesystem 0.30.0
termcolor                    2.1.0
terminado                    0.17.0
tf-estimator-nightly         2.8.0.dev2021122109
threadpoolctl                3.1.0
tifffile                     2021.11.2
tinycss2                     1.2.1
tornado                      6.2
tqdm                         4.64.1
traitlets                    5.5.0
typing_extensions            4.4.0
uritools                     4.0.0
urlextract                   1.2.0
urllib3                      1.24.1
virtualenv                   16.0.0
wcwidth                      0.2.5
webencodings                 0.5.1
websocket-client             1.4.1
Werkzeug                     2.2.2
wheel                        0.37.1
widgetsnbextension           4.0.3
wrapt                        1.14.1
xgboost                      1.3.3
zipp                         3.10.0

Which other package uses the sigmoid function apart from tensorflow that may be overloading?


This is the photo of packages after I am also upgrade my tensorflow versions:
it’s seems different from you, you can use this command to upgrade the version of tensorflow of your environment

 pip install --ignore-installed --upgrade tensorflow

I am also try to use your code in my assignment and found that there are an error but after I upgrade using the above command the error disappeared,

Also if the error is still, there are two ways help you:

  • you can create an cell with the sigmoid function in you assignment it will remove all overlaps

def sigmoid(z):
    return 1 / (1 + np.exp(-z))
  • you could also help you you can try to use np.matmul(…) command for matrix multiplication and pass your result to g(z)


Even running the notebook in the Coursera Labs environment, I think the TensorFlow package is intercepting the call to the autils.py : sigmoid() function.

@TMosh, that sounds exactly right!
I took a quick look to see if I could find the culprit, and found this line in public_utils.py (which gets imported after autils.py):
from tensorflow.keras.activations import sigmoid

But, regardless, it’s not really a good practice for autils.py to define a function with the same name as a function that could be imported from tensorflow, so I’ll submit a suggestion to the developers to rename the function in autils.py to something else that won’t clash, like, simple_sigmoid().

@Wendy, thanks for sorting out the source of the conflict.

Dear @AbdElRhaman_Fakhry

I can also confirm that the sigmoid @ autils.py call in an external environment is working now after upgrading from tensorflow 2.8.0 to 2.11.0.

Well done. Thank you again!

1 Like


I am happy to know that the problem solved with the help of both mentors @TMosh & @Wendy


1 Like

The course notebook was just updated to resolve the issue.