Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 有没有一种方法可以让KERA在无需额外数据处理措施的情况下读取TFRecord数据集?_Tensorflow_Tfrecord - Fatal编程技术网

Tensorflow 有没有一种方法可以让KERA在无需额外数据处理措施的情况下读取TFRecord数据集?

Tensorflow 有没有一种方法可以让KERA在无需额外数据处理措施的情况下读取TFRecord数据集?,tensorflow,tfrecord,Tensorflow,Tfrecord,我是一名高中生,正在努力学习TensorFlow的基础知识。我目前正在使用TFRecords输入文件构建一个模型,TFRecords输入文件是TensorFlow中的默认数据集文件类型,它是从原始数据压缩而来的。我目前正在使用一种复杂的方法将数据解析为numpy数组,以便Keras对其进行解释。虽然Keras是TF的一部分,但它应该能够轻松读取TFRecord数据集。Keras是否有其他方法来理解TFRecord文件 我使用decodeExampleHelper方法为培训准备数据 def _de

我是一名高中生,正在努力学习TensorFlow的基础知识。我目前正在使用TFRecords输入文件构建一个模型,TFRecords输入文件是TensorFlow中的默认数据集文件类型,它是从原始数据压缩而来的。我目前正在使用一种复杂的方法将数据解析为numpy数组,以便Keras对其进行解释。虽然Keras是TF的一部分,但它应该能够轻松读取TFRecord数据集。Keras是否有其他方法来理解TFRecord文件

我使用decodeExampleHelper方法为培训准备数据

def _decodeExampleHelper(example) :
  dataDictionary = {
    'xValues' : tf.io.FixedLenFeature([7], tf.float32),
    'yValues' : tf.io.FixedLenFeature([3], tf.float32)
  }
  # Parse the input tf.Example proto using the data dictionary
  example = tf.io.parse_single_example(example, dataDictionary)
  xValues = example['xValues']
  yValues = example['yValues']
  # The Keras Sequential network will have "dense" as the name of the first layer; dense_input is the input to this layer
  return dict(zip(['dense_input'], [xValues])), yValues

data = tf.data.TFRecordDataset(workingDirectory + 'training.tfrecords')

parsedData = data.map(_decodeExampleHelper)
我们可以看到
parsedData
在下面的代码块中具有正确的维度

tmp = next(iter(parsedData))
print(tmp)
这将输出Keras能够解释的第一组正确尺寸的数据

({'dense_input': <tf.Tensor: id=273, shape=(7,), dtype=float32, numpy=
array([-0.6065675 , -0.610906  , -0.65771157, -0.41417238,  0.89691925,
        0.7122903 ,  0.27881026], dtype=float32)>}, <tf.Tensor: id=274, shape=(3,), dtype=float32, numpy=array([ 0.        , -0.65868723, -0.27960175], dtype=float32)>)
model.fit(parsedData,epochs=1)
给出了一个错误
ValueError:error当检查输入时:期望密集输入具有形状(7),但得到了具有形状(1,)的数组,尽管密集输入为7


在这种情况下会有什么问题?为什么Keras no能正确解释文件中的张量?

在将数据传递给Keras并使用输入层之前,需要对数据进行批处理。以下内容对我来说很好:

import tensorflow as tf

ds = tf.data.Dataset.from_tensors((
    {'dense_input': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]}, [ 0.0, 0.1, -0.1]))
ds = ds.repeat(32).batch(32)

model = tf.keras.models.Sequential(
    [
      tf.keras.Input(shape=(7,), name='dense_input'),
      tf.keras.layers.Dense(20, activation = 'relu'),
      tf.keras.layers.Dense(3, activation = 'linear'),
    ]
)

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

model.fit(ds, epochs = 1)

在将数据传递给Keras和使用输入层之前,需要对数据进行批处理。以下内容对我来说很好:

import tensorflow as tf

ds = tf.data.Dataset.from_tensors((
    {'dense_input': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]}, [ 0.0, 0.1, -0.1]))
ds = ds.repeat(32).batch(32)

model = tf.keras.models.Sequential(
    [
      tf.keras.Input(shape=(7,), name='dense_input'),
      tf.keras.layers.Dense(20, activation = 'relu'),
      tf.keras.layers.Dense(3, activation = 'linear'),
    ]
)

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

model.fit(ds, epochs = 1)