Up categorical_columns の作成 作成: 2021-04-23
更新: 2021-04-23


    categorical_columns の生成:
      >>> CATEGORIES = { ... 'sex': ['male', 'female'], ... 'class' : ['First', 'Second', 'Third'], ... 'deck' : ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], ... 'embark_town' : ['Cherbourg', 'Southhampton', 'Queenstown'], ... 'alone' : ['y', 'n'] ... } >>> categorical_columns = [] >>> for feature, vocab in CATEGORIES.items(): ... cat_col = tf.feature_column.categorical_column_with_vocabulary_list( ... key=feature, vocabulary_list=vocab) ... categorical_columns.append(tf.feature_column.indicator_column(cat_col)) ... >>>

    categorical_columns の内容チェック:
      >>> categorical_columns [IndicatorColumn(categorical_column=VocabularyListCategoricalColumn( key='sex', vocabulary_list=('male', 'female'), dtype=tf.string, default_value=-1, num_oov_buckets=0)), IndicatorColumn(categorical_column=VocabularyListCategoricalColumn( key='class', vocabulary_list=('First', 'Second', 'Third'), dtype=tf.string, default_value=-1, num_oov_buckets=0)), IndicatorColumn(categorical_column=VocabularyListCategoricalColumn( key='deck', vocabulary_list=('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'), dtype=tf.string, default_value=-1, num_oov_buckets=0)), IndicatorColumn(categorical_column=VocabularyListCategoricalColumn( key='embark_town', vocabulary_list=('Cherbourg', 'Southhampton', 'Queenstown'), dtype=tf.string, default_value=-1, num_oov_buckets=0)), IndicatorColumn(categorical_column=VocabularyListCategoricalColumn( key='alone', vocabulary_list=('y', 'n'), dtype=tf.string, default_value=-1, num_oov_buckets=0))]


    categorical_columns の機能テスト
     categorical_layer を作成:
      >>> categorical_layer = tf.keras.layers.DenseFeatures(categorical_columns) >>> categorical_layer <tensorflow.python.feature_column.dense_features.DenseFeatures object at 0x6414dbb0>


     packed_train_data からバッチを1つとって,categorical_layer に入力:
      >>> for batch in packed_train_data.take(1): ... print( categorical_layer(batch).numpy()[0] ) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/home/pi/venv/lib/python3.7/site-packages/ tensorflow_core/python/keras/engine/base_layer.py", line 913, in __call__outputs = self.call(cast_inputs, *args, **kwargs) File "/home/pi/venv/lib/python3.7/site-packages/ tensorflow_core/python/feature_column/dense_features.py", line 129, in call features) ValueError: ('We expected a dictionary here. Instead we got: ', (OrderedDict([ ('sex', <tf.Tensor: shape=(5,), dtype=string, numpy=array([b'male', b'female', b'male', b'female', b'male'], dtype=object)>), ('class', <tf.Tensor: shape=(5,), dtype=string, numpy=array([b'Third', b'First', b'Third', b'Second', b'First'], dtype=object)>), ('deck', <tf.Tensor: shape=(5,), dtype=string, numpy=array([b'unknown', b'E', b'unknown', b'unknown', b'B'], dtype=object)>), ('embark_town', <tf.Tensor: shape=(5,), dtype=string, numpy=array([b'Queenstown', b'Southampton', b'Southampton', b'Southampton', b'Southampton'], dtype=object)>), ('alone', <tf.Tensor: shape=(5,), dtype=string, numpy=array([b'y', b'n', b'y', b'y', b'n'], dtype=object)>), ('numeric', <tf.Tensor: shape=(5, 4), dtype=float32, numpy= array([[28. , 0. , 0. , 7.75], [33. , 1. , 0. , 53.1 ], [20. , 0. , 0. , 8.05], [35. , 0. , 0. , 21. ], [31. , 1. , 0. , 57. ]], dtype=float32)>) ]), <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 0, 1, 1])>)