python - 如何修改此代码以删除 ID 字段?
问题描述
查看以下源代码:
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
我怎样才能解决这个问题?
解决方案
推荐阅读
- python - 我怎样才能在我的views.py中写这个过滤器
- node.js - 如何更新 Mogodb 文档的架构
- flutter - RangeError(索引):无效值:不在包含范围内 0..1273:1274
- html - 表格行悬停以显示工具栏
- flutter - 容器设计结构——Flutter
- c - 如何获取命令行参数以从同一 c 程序文件中的另一个方法读取输出?
- unity3d - 为什么 Unity 在构建中包含编辑器目录
- html - 如何使用纯 CSS 在一个 div 中订购(两列)长文本?
- javascript - 无法使用 MySQL 在 node.js 中执行 Sql 查询
- powerbi - 日期PowerBI的累计发生次数?