简体   繁体   English

Tensorflow Keras:运行model.fit时出现尺寸/形状错误

[英]Tensorflow Keras: Dimension/Shape Error when running model.fit

I am trying to use Tensorflow and Keras for a prediction model.我正在尝试将 Tensorflow 和 Keras 用于预测模型。

I first read in my dataset that has shape (7709, 58), then normalize it:我首先读取具有形状 (7709, 58) 的数据集,然后对其进行规范化:

normalizer = tf.keras.layers.Normalization(axis=-1)
normalizer.adapt(np.array(dataset))

Then I split the data into training and testing data:然后我将数据拆分为训练和测试数据:

train_dataset = dataset[:5000]
test_dataset = dataset[5000:]

I prepare those datasets:我准备了这些数据集:

train_dataset.describe().transpose()
test_dataset.describe().transpose()

train_features = train_dataset.copy()
test_features = test_dataset.copy()

train_labels = train_features.pop('outcome')
test_labels = test_features.pop('outcome')

Then I build the model:然后我建立模型:

def build_and_compile_model(norm):
  model = keras.Sequential([
      norm,
      layers.Dense(64, activation='relu'),
      layers.Dense(64, activation='relu'),
      layers.Dense(1)
  ])

  model.compile(loss='mean_squared_error', metrics=['mean_squared_error'],
                optimizer=tf.keras.optimizers.Adam(0.001))
  return model

dnn_model = build_and_compile_model(normalizer)

When I then try to fit the model, it fails:然后当我尝试拟合模型时,它失败了:

history = dnn_model.fit(
    test_features,
    test_labels, 
    validation_split=0.2, epochs=50)

Gives the following error:给出以下错误:

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, 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 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, 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 "normalization_7" (type Normalization).
    
    Dimensions must be equal, but are 57 and 58 for '{{node sequential_7/normalization_7/sub}} = Sub[T=DT_FLOAT](sequential_7/Cast, sequential_7/normalization_7/sub/y)' with input shapes: [?,57], [1,58].

Does anyone know what the issue is and how I can address it?有谁知道问题是什么以及我该如何解决? Thanks!谢谢!

You lost the outcome column in the dataframe because of pop .由于pop ,您丢失了数据框中的outcome列。 Try extracting that column using尝试使用提取该列

train_labels = train_features['outcome']
test_labels = test_features['outcome']

bui is correct that pop is the problem . bui 是正确的,pop 是问题所在 However, I would keep pop but move the "normalizer.adapt" method behind pop.但是,我会保留 pop,但将“normalizer.adapt”方法移到 pop 后面。 This way you don't fit the normalizer to your labels (which does not make sense) and you don't use the labels as a feature (which could be terrible).这样,您就不会将规范化器与标签相匹配(这没有意义),并且您不会将标签用作特征(这可能很糟糕)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM