handson-ml2 icon indicating copy to clipboard operation
handson-ml2 copied to clipboard

Chapter 13 : TypeError in cell #102

Open lebaste77 opened this issue 4 years ago • 5 comments

Hi,

Executing the cell 102 of the chapter 13 handbook raises TypeError Exception (see traceback below). The workaround i used is to change value=[x] to value=[*x] (and the same for y) as the values returned are arrays ([15.]) and not simple values. We could also be using directly the filepath_dataset created earlier and parse it "the good way", reshaping X_train, or else.. ?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-232-eeaa137fafe1> in <module>
      2     for x, y in zip(X_train[:, 1:2], y_train):
      3         example = Example(features=Features(feature={
----> 4             "housing_median_age": Feature(float_list=FloatList(value=[x])),
      5             "median_house_value": Feature(float_list=FloatList(value=[y]))
      6         }))

~\anaconda3\lib\site-packages\google\protobuf\internal\python_message.py in init(self, **kwargs)
    551             field_value = [_GetIntegerEnumValue(field.enum_type, val)
    552                            for val in field_value]
--> 553           copy.extend(field_value)
    554         self._fields[field] = copy
    555       elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:

~\anaconda3\lib\site-packages\google\protobuf\internal\containers.py in extend(self, elem_seq)
    280       raise
    281 
--> 282     new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
    283     if new_values:
    284       self._values.extend(new_values)

~\anaconda3\lib\site-packages\google\protobuf\internal\containers.py in <listcomp>(.0)
    280       raise
    281 
--> 282     new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
    283     if new_values:
    284       self._values.extend(new_values)

~\anaconda3\lib\site-packages\google\protobuf\internal\type_checkers.py in CheckValue(self, proposed_value)
    283       message = ('%.1024r has type %s, but expected one of: numbers.Real' %
    284                  (proposed_value, type(proposed_value)))
--> 285       raise TypeError(message)
    286     converted_value = float(proposed_value)
    287     # This inf rounding matches the C++ proto SafeDoubleToFloat logic.

TypeError: array([15.]) has type <class 'numpy.ndarray'>, but expected one of: numbers.Real

lebaste77 avatar Dec 28 '20 18:12 lebaste77

Hi lebaste77, thank you for pointing this out and give a workaround. Peter

peter-pri avatar Jan 23 '21 17:01 peter-pri

Hi @lebaste77 , thanks for your feedback.

I recently updated all the notebooks to the latest version of the libs (including Scikit-Learn 0.24, TensorFlow 2.4, Pandas 1.2, Matplotlib 3.3, TF Agents 0.7, Gym 0.18, TFX 0.27, etc.) and I tested everything and got no errors. Could you please test the updated code with the latest library versions (see environment.yml or requirements.txt) and report whether the issue is fixed or not? Thanks!

ageron avatar Mar 03 '21 20:03 ageron

Hi @lebaste77 , thanks for your feedback.

I recently updated all the notebooks to the latest version of the libs (including Scikit-Learn 0.24, TensorFlow 2.4, Pandas 1.2, Matplotlib 3.3, TF Agents 0.7, Gym 0.18, TFX 0.27, etc.) and I tested everything and got no errors. Could you please test the updated code with the latest library versions (see environment.yml or requirements.txt) and report whether the issue is fixed or not? Thanks!

it seems it is still happening (at least on my pc)

yuvaldavid22 avatar Jun 17 '21 06:06 yuvaldavid22

Hi @lebaste77 ,

Could you please run the following code within the same Jupyter notebook as the one that gives an exception, and paste the output below?

import sys
import google.protobuf
import tensorflow as tf
import numpy as np

print("Protobuf:", google.protobuf.__version__)
print("Python:", sys.version)
print("TensorFlow:", tf.__version__)
print("NumPy:", np.__version__)

Also, please indicate which O.S. you are running? Thanks!

ageron avatar Jul 06 '21 04:07 ageron

Following code fixed the problem in my case.

'housing_median_age': Feature(float_list = FloatList(value = [float(x)])),
'median_house_value': Feature(float_list = FloatList(value = [float(y)]))

My environment : Windows10 Protobuf: 3.19.1 Python: 3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)] TensorFlow: 2.9.1 NumPy: 1.21.5

Kawamura-Masaharu avatar Jun 09 '22 12:06 Kawamura-Masaharu