繁体   English   中英

Tensorflow API、Tensorflow hub 和 Keras 输入形状

[英]Tensorflow API, Tensorflow hub and Keras input shape

我正在使用 tf=2.0.0 和 tensorflow hub。 目前我正在使用 Tensorflow 数据 API 来加载存储在 tfrecords 文件中的数据。

我已成功加载数据集并编译模型,但是当我尝试将数据放入模型时,出现错误:

检查输入时出错:预期的输入输入有 1 维,但得到形状为 (64, 1) 的数组

这是我加载数据的方式:

def _dataset_parser(value):
    """Parse a record from value."""
    featdef={
        'id':  tf.io.FixedLenFeature([1], tf.int64),
        'question': tf.io.FixedLenFeature([1], tf.string),
        'label': tf.io.FixedLenFeature([1], tf.int64)
    }

    example = tf.io.parse_single_example(value, featdef)

    label = tf.cast(example['label'], tf.int32)
    question = tf.cast(example['question'], tf.string)
    return example['question'], example['label']
def _input(epochs, batch_size, filenames):
    dataset = tf.data.TFRecordDataset(filenames)

    dataset = dataset.repeat(epochs)
    dataset = dataset.prefetch(1)

    # Parse records.
    dataset = dataset.map(_dataset_parser)
    dataset = dataset.shuffle(100)

    # Batch it up.
    dataset = dataset.batch(batch_size)
    iterator = tf.compat.v1.data.make_one_shot_iterator(dataset)

    question_batch, label_batch = iterator.get_next()
    label_batch = tf.one_hot(label_batch, NUM_CLASSES)

    return question_batch, label_batch



train_ds = _input(20, 64, ['train_xs.tfrecords'])

这是我的模型:

model = tf.keras.Sequential([
        hub.KerasLayer(HUB_URL, dtype=tf.string, input_shape=[], output_shape=[WIDTH], name='inputs'),
        tf.keras.layers.Dense(256, 'relu', name ='layer_1'),
        tf.keras.layers.Dense(128, 'relu', name = 'layer_2'),
        tf.keras.layers.Dense(NUM_CLASSES, activation='softmax', name='output')
    ])

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

我已经尝试将入口层的输入形状设置为 (None, 1) 但它一直失败,不确定问题是否是由于 TensorFlow 集线器造成的,但是我尝试从 Hands-on-ml 书中运行这个示例:

model = tf.keras.Sequential([
    hub.KerasLayer("https://tfhub.dev/google/tf2-preview/nnlm-en-dim50/1",
                   dtype=tf.string, input_shape=[], output_shape=[50]),
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dense(1, activation="sigmoid")
])
model.compile(loss="binary_crossentropy", optimizer="adam",
              metrics=["accuracy"])
datasets, info = tfds.load("imdb_reviews", as_supervised=True, with_info=True)
train_size = info.splits["train"].num_examples
batch_size = 32
train_set = datasets["train"].repeat().batch(batch_size).prefetch(1)
history = model.fit(train_set, steps_per_epoch=5, epochs=5)#steps_per_epoch=train_size // batch_size, epochs=5)

它工作正常,但我发现的一个区别是,如果我在书中的示例上打印 train_set ,我会得到:

<PrefetchDataset shapes: ((None,), (None,)), types: (tf.string, tf.int64)>

而使用我的代码,当我打印我提供给模型的数据集时,我得到了这个:

(<tf.Tensor: id=11409, shape=(64, 1), dtype=string, numpy=  array([[b'Restroom score out of 9'],
        [b'Name'],
        [b'Lastname'],
        [b'Type of house'],
        [b'Inspection date'],
        [b'Pet'],
        [b'Phone'], dtype=object)>,  <tf.Tensor: id=11414, shape=(64, 1, 80), dtype=float32, numpy=  array([[[0., 0., 0., ...,
0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.]],

        ...,

        [[0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.]],

        [[0., 0., 0., ..., 0., 0., 0.]]], dtype=float32)>)

有人知道为什么数据形状不同吗?

如果有人感兴趣,这是对我有用的最终代码:

    model = tf.keras.Sequential([
        hub.KerasLayer(HUB_URL, dtype=tf.string, input_shape=[], output_shape=[WIDTH], name='inputs'),
name=INPUT_TENSOR_NAME),
        tf.keras.layers.Dense(256, 'relu', name ='layer_1'),
        tf.keras.layers.Dense(128, 'relu', name = 'layer_2'),
        tf.keras.layers.Dense(NUM_CLASSES, activation='softmax', name='output')
    ])

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

    def _dataset_parser(value):
        """Parse a record from value."""
        featdef={
            'id':  tf.io.FixedLenFeature([], tf.int64),
            'question': tf.io.FixedLenFeature([], tf.string),
            'label': tf.io.FixedLenFeature([], tf.int64)
        }

        example = tf.io.parse_single_example(value, featdef)

        label = tf.cast(example['label'], tf.int64)
        question = tf.cast(example['question'], tf.string)
        return question, tf.one_hot(label, NUM_CLASSES)

    def _input(epochs, batch_size, filenames):
        dataset = tf.data.TFRecordDataset(filenames)

        dataset = dataset.repeat(epochs)
        dataset = dataset.prefetch(1)

        # Parse records.
        dataset = dataset.map(_dataset_parser)
        dataset = dataset.shuffle(100)

        # Batch it up.
        dataset = dataset.batch(batch_size)


        return dataset

    train_ds = _input(1, 10, ['train_xs.tfrecords'])

    model.fit(train_ds,epochs=1)

不知道为什么,但通过将特征定义更改为

 featdef={
        'id':  tf.io.FixedLenFeature([], tf.int64),
        'question': tf.io.FixedLenFeature([], tf.string),
        'label': tf.io.FixedLenFeature([], tf.int64)
    }

有效

暂无
暂无

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

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