簡體   English   中英

Tensorflow model.fit ValueError

[英]Tensorflow model.fit ValueError

我正在嘗試使用 Tensorflow 訓練 model。 我正在使用tf.data.experimental.make_csv_dataset讀取一個巨大的 csv 文件這是我的代碼:

進口

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing

LABEL_COLUMN = 'venda_qtde'

將 csv 讀入 tf.data.Dataset

def get_dataset(file_path, **kwargs):
  dataset = tf.data.experimental.make_csv_dataset(
      file_path,
      batch_size=4096, 
      na_value="?",
      label_name=LABEL_COLUMN,
      num_epochs=1,
      ignore_errors=False,
      shuffle=False,
      **kwargs)
  return dataset

構建 model 實例

def build_model():
  model = None
  model = keras.Sequential([
    layers.Dense(520, activation='relu'),
    layers.Dense(520, activation='relu'),
    layers.Dense(520, activation='relu'),
    layers.Dense(1)
  ])

  model.compile(loss='mean_squared_error',
                optimizer='adam',
                metrics=['mae'])
  return model

執行功能:

ds_treino = get_dataset('data/processed/curva_a/curva_a_train.csv')
nn_model = build_model()
nn_model.fit(ds_treino, epochs=10)

但是當調用 fit function 時,我得到錯誤:

ValueError: Layer sequential_5 expects 1 inputs, but it received 520 input tensors. Inputs received: ...

我的數據集有 519 個特征和 1 個 label 和大約 17M 行誰能幫我做錯了什么?

Function make_csv_dataset將返回一個特征是字典的tf.data.Dataset

train_dataset
<PrefetchDataset shapes: (OrderedDict([... features ... ])

您需要將它們配對成特征和標簽。 您可以使用:

def features_and_labels(features, labels):
  features = tf.stack(list(features.values()), axis=1)
  return features, labels

train_dataset = train_dataset.map(features_and_labels)

train_dataset
<MapDataset shapes: ((None, 10), (None,)), types: (tf.float32, tf.int32)>

之后,您應該可以將其傳遞給fit() function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM