查看以下源代码:
import pandas as pd
from tensorflow.keras import layers, models
colors_df = pd.DataFrame(data=[[5,'yellow'],[1,'red'],[2,'blue'],[3,'green'],[4,'blue'],[7,'purple']], columns=['id', 'color'])
categorical_input = layers.Input(shape=(1,), dtype=tf.string)
one_hot_layer = OneHotEncodingLayer()
one_hot_layer.adapt(colors_df['color'].values)
encoded = one_hot_layer(categorical_input)
numeric_input = layers.Input(shape=(1,), dtype=tf.float32)
concat = layers.concatenate([numeric_input, encoded])
model = models.Model(inputs=[numeric_input, categorical_input], outputs=[concat])
predicted = model.predict([colors_df['id'], colors_df['color']])
print(predicted)
# [[5. 0. 1. 0. 0. 0.]
# [1. 0. 0. 1. 0. 0.]
# [2. 1. 0. 0. 0. 0.]
# [3. 0. 0. 0. 0. 1.]
# [4. 1. 0. 0. 0. 0.]
# [7. 0. 0. 0. 1. 0.]]
我正在尝试删除 id
字段并写入以下内容:
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import tensorflow as tf
from tensorflow.keras import layers
import pandas as pd
from tensorflow.keras import models
from OneHotEncodingLayer import OneHotEncodingLayer
colors_df = pd.DataFrame(
data=['yellow', 'red','blue','green','blue','purple'],
columns=['color']
)
categorical_input_tensor = layers.Input(shape=(1,), dtype=tf.string) # the expected input element will be 1-D vector of string type
one_hot_layer = OneHotEncodingLayer()
print(categorical_input_tensor)
one_hot_layer.adapt(colors_df['color'].values) # pass the vector of 'color' elements
encoded_input_tensor = one_hot_layer(categorical_input_tensor) # caregorical_input is a tensor
concat_tensor = layers.concatenate([encoded_input_tensor])
model = models.Model(inputs=[categorical_input_tensor], outputs=[concat_tensor])
predicted = model.predict([colors_df['color']])
但是,这显示了以下错误:
Traceback (most recent call last):
File "C:/Users/pc/source/repos/OneHotEncodingLayer__test/main.py", line 49, in <module>
concat_tensor = layers.concatenate([encoded_input_tensor])
File "C:\ProgramData\Miniconda3\envs\the_neural_network\lib\site-packages\tensorflow\python\keras\layers\merge.py", line 931, in concatenate
return Concatenate(axis=axis, **kwargs)(inputs)
File "C:\ProgramData\Miniconda3\envs\the_neural_network\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 925, in __call__
return self._functional_construction_call(inputs, args, kwargs,
File "C:\ProgramData\Miniconda3\envs\the_neural_network\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1098, in _functional_construction_call
self._maybe_build(inputs)
File "C:\ProgramData\Miniconda3\envs\the_neural_network\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2643, in _maybe_build
self.build(input_shapes) # pylint:disable=not-callable
File "C:\ProgramData\Miniconda3\envs\the_neural_network\lib\site-packages\tensorflow\python\keras\utils\tf_utils.py", line 323, in wrapper
output_shape = fn(instance, input_shape)
File "C:\ProgramData\Miniconda3\envs\the_neural_network\lib\site-packages\tensorflow\python\keras\layers\merge.py", line 493, in build
raise ValueError('A `Concatenate` layer should be called '
ValueError: A `Concatenate` layer should be called on a list of at least 2 inputs
我该如何解决这个问题?