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
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 "", line 2, in
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', ),
('class', ),
('deck', ),
('embark_town', ),
('alone', ),
('numeric', )
]),
)
|