Tensorflow Keras如何用TFRECORDS数据集拟合模型?

Tensorflow Keras如何用TFRECORDS数据集拟合模型?,tensorflow,keras,Tensorflow,Keras,我编写了一个简单的TFRECORDS文件,其中包含三个特性和一个标签。 在我学习教程的过程中,似乎要使用这些TFRECORDS,我需要创建一个数据集,解析示例,并通过map()执行其他操作,如规范化。如果这不是正确的工作流程,我将不胜感激 dataset = tf.data.TFRecordDataset("dataset.tfrecords") #parse the protobuffer def _parse_function(

我编写了一个简单的TFRECORDS文件,其中包含三个特性和一个标签。 在我学习教程的过程中,似乎要使用这些TFRECORDS,我需要创建一个数据集,解析示例,并通过map()执行其他操作,如规范化。如果这不是正确的工作流程,我将不胜感激

    dataset = tf.data.TFRecordDataset("dataset.tfrecords")
    
    #parse the protobuffer
    
    def _parse_function(proto):
        # define your tfrecord again. 
        keys_to_features = {'weight_pounds': tf.io.FixedLenFeature([], tf.float32),
                            'gestation_weeks': tf.io.FixedLenFeature([], tf.float32),
                            'plurality': tf.io.FixedLenFeature([], tf.float32),
                            'isMale': tf.io.FixedLenFeature([], tf.float32),
                          
                           
                           }
        
        # Load one example
        parsed_features = tf.io.parse_example(proto, keys_to_features)
        
        # Turn your saved image string into an array
        #parsed_features['image'] = tf.decode_raw(
        #    parsed_features['image'], tf.uint8)
        
        return parsed_features
    
    hold_meanstd={
        'weight_pounds':[7.234738,1.330294],
        'gestation_weeks':[38.346464,4.153269],
        'plurality':[1.035285,0.196870]
    }
    
    def normalize(example):
        example['weight_pounds']=(example['weight_pounds']-hold_meanstd['weight_pounds'][0])/hold_meanstd['weight_pounds'][1]
        example['gestation_weeks']=(example['gestation_weeks']-hold_meanstd['gestation_weeks'][0])/hold_meanstd['gestation_weeks'][1]
        example['plurality']=(example['plurality']-hold_meanstd['plurality'][0])/hold_meanstd['plurality'][1]
        label=example.pop('isMale')
        return(example,label)

dataset = tf.data.TFRecordDataset(["dataset.tfrecords"]).map(_parse_function)
dataset =dataset.map(normalize)
dataset =dataset.batch(64)
一旦我有了这个数据集,我想我可以输入Keras模型:

Dense = keras.layers.Dense
model = keras.Sequential(
        [
            Dense(500, activation="relu", kernel_initializer='uniform',
                  input_shape=(3,)),
            Dense(200, activation="relu"),
            Dense(100, activation="relu"),
            Dense(25, activation="relu"),
            Dense(1, activation="sigmoid")
        ])

optimizer = keras.optimizers.RMSprop(lr=0.01)

    # Compile Keras model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=[tf.keras.metrics.AUC()])
    
model.fit(dataset)
这会引发一个错误:

 ValueError: Layer sequential_1 expects 1 inputs, but it received 3 input tensors. Inputs received: [<tf.Tensor 'ExpandDims:0' shape=(None, 1) dtype=float32>, <tf.Tensor 'ExpandDims_1:0' shape=(None, 1) dtype=float32>, <tf.Tensor 'ExpandDims_2:0' shape=(None, 1) dtype=float32>]
ValueError:Layer sequential_1需要1个输入,但收到3个输入张量。收到的投入:[,]

问题似乎是输入数据集看起来像三个输入,而不是一个?如何允许Keras在TF记录数据集上进行训练?

在第一个密集层中指定
输入形状=(3,)
后,Keras模型期望输入形状为
(无,3)
(其中
无定义批量大小)的张量。我们可以举以下为例:

[
[0.0,0.0,0.0]
]
如果我们查看您的
tf.data.Dataset
,我们可以看到它正在返回一个字典。每个输入将如下所示:

{
"weight_pounds":[0.0],
"gestation_weeks":[0.0],
"plurality":[0.0]
}
这与上面指定的
输入形状
有点不同

要解决此问题,您有两种解决方案:

  • 删除字典数据结构。为此,您可以稍微更改一下规格化函数:
  • 使用a作为输入层::

在第一个密集层中指定
input\u shape=(3,)
后,keras模型期望输入一个张量,其形状
(None,3)
(其中
None
定义批量大小)。我们可以举以下为例:

[
[0.0,0.0,0.0]
]
如果我们查看您的
tf.data.Dataset
,我们可以看到它正在返回一个字典。每个输入将如下所示:

{
"weight_pounds":[0.0],
"gestation_weeks":[0.0],
"plurality":[0.0]
}
这与上面指定的
输入形状
有点不同

要解决此问题,您有两种解决方案:

  • 删除字典数据结构。为此,您可以稍微更改一下规格化函数:
  • 使用a作为输入层::