Trying to run "from tensorflow.keras.utils import to_categorical" in jupyter notebooks on mac m1

Viewed 25

I was doing this tutorial: Sign Language Detection using ACTION RECOGNITION with Python And I got all the way to Part 6: Preprocess Data and Create Labels and Features

The issue is this line of code:

from tensorflow.keras.utils import to_categorical

Giving me these errors:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [56], in <cell line: 2>()
      1 from sklearn.model_selection import train_test_split
----> 2 from tensorflow.keras.utils import to_categorical

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/__init__.py:37, in <module>
     35   del estimator_api
     36 except (ImportError, AttributeError):
---> 37   print('tf.estimator package not installed.')
     39 from tensorflow._api.v1 import app
     40 from tensorflow._api.v1 import bitwise

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/__init__.py:42, in <module>
     29 import sys
     30 import traceback
     32 # TODO(drpng): write up instructions for editing this file in a doc and point to
     33 # the doc instead.
     34 # If you want to edit this file to expose modules in public tensorflow API, you
     35 # need to follow these steps:
     36 # 1. Consult with tensorflow team and get approval for adding a new API to the
     37 #    public interface.
     38 # 2. Document the module in the gen_docs_combined.py.
     39 # 3. Import the module in the main tensorflow namespace by adding an import
     40 #    statement in this file.
     41 # 4. Sanitize the entry point by making sure that your module does not expose
---> 42 #    transitively imported modules used for implementation, such as os, sys.
     43 
     44 # go/tf-wildcard-import
     45 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
     47 import numpy as np
     49 from tensorflow.python import pywrap_tensorflow

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/data/__init__.py:21, in <module>
     15 """`tf.data.Dataset` API for input pipelines.
     16 
     17 See [Importing Data](https://tensorflow.org/guide/data) for an overview.
     18 """
     20 # pylint: disable=unused-import
---> 21 from tensorflow.python.data import experimental
     22 from tensorflow.python.data.ops.dataset_ops import AUTOTUNE
     23 from tensorflow.python.data.ops.dataset_ops import Dataset

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/data/experimental/__init__.py:96, in <module>
     15 """Experimental API for building input pipelines.
     16 
     17 This module contains experimental `Dataset` sources and transformations that can
   (...)
     92 @@UNKNOWN_CARDINALITY
     93 """
     95 # pylint: disable=unused-import
---> 96 from tensorflow.python.data.experimental import service
     97 from tensorflow.python.data.experimental.ops.batching import dense_to_ragged_batch
     98 from tensorflow.python.data.experimental.ops.batching import dense_to_sparse_batch

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/data/experimental/service/__init__.py:419, in <module>
      1 # Copyright 2020 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     13 # limitations under the License.
     14 # ==============================================================================
     15 """API for using the tf.data service.
     16 
     17 This module contains:
   (...)
    416   job of ParameterServerStrategy).
    417 """
--> 419 from tensorflow.python.data.experimental.ops.data_service_ops import distribute
    420 from tensorflow.python.data.experimental.ops.data_service_ops import from_dataset_id
    421 from tensorflow.python.data.experimental.ops.data_service_ops import register_dataset

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/data/experimental/ops/data_service_ops.py:24, in <module>
     22 from tensorflow.python import tf2
     23 from tensorflow.python.compat import compat
---> 24 from tensorflow.python.data.experimental.ops import compression_ops
     25 from tensorflow.python.data.experimental.service import _pywrap_server_lib
     26 from tensorflow.python.data.experimental.service import _pywrap_utils

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/data/experimental/ops/compression_ops.py:16, in <module>
      1 # Copyright 2020 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     13 # limitations under the License.
     14 # ==============================================================================
     15 """Ops for compressing and uncompressing dataset elements."""
---> 16 from tensorflow.python.data.util import structure
     17 from tensorflow.python.ops import gen_experimental_dataset_ops as ged_ops
     20 def compress(element):

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/data/util/structure.py:23, in <module>
     20 import six
     21 import wrapt
---> 23 from tensorflow.python.data.util import nest
     24 from tensorflow.python.framework import composite_tensor
     25 from tensorflow.python.framework import ops

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/data/util/nest.py:36, in <module>
     16 """## Functions for working with arbitrarily nested sequences of elements.
     17 
     18 NOTE(mrry): This fork of the `tensorflow.python.util.nest` module
   (...)
     31    arrays.
     32 """
     34 import six as _six
---> 36 from tensorflow.python.framework import sparse_tensor as _sparse_tensor
     37 from tensorflow.python.util import _pywrap_utils
     38 from tensorflow.python.util import nest

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/framework/sparse_tensor.py:24, in <module>
     22 from tensorflow.python import tf2
     23 from tensorflow.python.framework import composite_tensor
---> 24 from tensorflow.python.framework import constant_op
     25 from tensorflow.python.framework import dtypes
     26 from tensorflow.python.framework import ops

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/framework/constant_op.py:25, in <module>
     23 from tensorflow.core.framework import types_pb2
     24 from tensorflow.python.eager import context
---> 25 from tensorflow.python.eager import execute
     26 from tensorflow.python.framework import dtypes
     27 from tensorflow.python.framework import op_callbacks

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/eager/execute.py:23, in <module>
     21 from tensorflow.python import pywrap_tfe
     22 from tensorflow.python.eager import core
---> 23 from tensorflow.python.framework import dtypes
     24 from tensorflow.python.framework import ops
     25 from tensorflow.python.framework import tensor_shape

File /opt/anaconda3/lib/python3.9/site-packages/tensorflow/python/framework/dtypes.py:34, in <module>
     31 from tensorflow.python.types import trace
     32 from tensorflow.core.function import trace_type
---> 34 _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type()
     37 class DTypeMeta(type(_dtypes.DType), abc.ABCMeta):
     38   pass

TypeError: Unable to convert function return value to a Python type! The signature was
    () -> handle

This is on a Mac M1, using:

conda list tensorflow
# packages in environment at /opt/anaconda3:
#
# Name                    Version                   Build  Channel
tensorflow-deps           2.8.0                         0    apple
tensorflow-estimator      2.10.0                   pypi_0    pypi
tensorflow-macos          2.10.0                   pypi_0    pypi
tensorflow-metal          0.6.0                    pypi_0    pypi

and

conda list numpy
# packages in environment at /opt/anaconda3:
#
# Name                    Version                   Build  Channel
numpy                     1.21.6                   pypi_0    pypi
numpy-base                1.21.5           py39hadd41eb_3  
numpydoc                  1.2                pyhd3eb1b0_0  

I made sure to download tensorflow for m1 arm, but I could be missing something. What do I do to fix this?

1 Answers

This happened to me awhile ago. I had to update numpy:

pip install -U numpy
Related