gpt4 book ai didi

python - 调用 "dense_features_5"层时遇到异常

转载 作者:行者123 更新时间:2023-12-05 04:42:01 24 4
gpt4 key购买 nike

我有多类分类(3 个类),因此输出层有 3 个神经元,所有列都是数字。并犯了一个我无法理解的错误。这是我的代码:

def df_to_dataset(df, shuffle=True, batch_size=32): 
df = df.copy()
labels = df.pop('class')
dicts = {'STAR': 1, 'GALAXY': 2, 'QSO': 3}
converted_labels = np.array([dicts[l] for l in labels])
ds = tf.data.Dataset.from_tensor_slices((dict(df), converted_labels))
if shuffle:
ds = ds.shuffle(buffer_size=len(df))
return ds

batch_size = 32
train_ds = df_to_dataset(train, batch_size=batch_size)
val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)

feature_columns = []
for numeric_col in ['objid', 'ra', 'dec', 'u', 'g', 'r', 'i', 'z', 'run', 'rerun', 'camcol', 'field', 'specobjid', 'redshift', 'plate', 'mjd', 'fiberid']:
feature_columns.append(feature_column.numeric_column(numeric_col))

feature_layer = DenseFeatures(feature_columns) # A layer that produces a dense Tensor
model = Sequential([
feature_layer,
Dense(32, activation='relu'),
Dense(3, activation='softmax')
])

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.fit(train_ds,
validation_data=val_ds,
epochs=10)

这里有一个错误:

ValueError: 在用户代码中:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function  *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None

ValueError: Exception encountered when calling layer "dense_features_5" (type DenseFeatures).

Feature (key: camcol) cannot have rank 0. Given: Tensor("IteratorGetNext:0", shape=(), dtype=int64)

请告诉我它可能是什么?

最佳答案

所以我不小心从 df_to_dataset 函数中删除了这一行:

  def df_to_dataset(df, shuffle=True, batch_size=32): 
df = df.copy()
labels = df.pop('class')
dicts = {'STAR': 1, 'GALAXY': 2, 'QSO': 3}
converted_labels = np.array([dicts[l] for l in labels])
ds = tf.data.Dataset.from_tensor_slices((dict(df), converted_labels))
if shuffle:
ds = ds.shuffle(buffer_size=len(df))
ds = ds.batch(batch_size) # this one
return ds

现在一切正常:)

更新:您可以传递您拥有的所有数据,而不是批处理,但在 tensorflow 中,您应该传递批处理

关于python - 调用 "dense_features_5"层时遇到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69910971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com