Tensorflow API,Tensorflow集线器和Keras输入形状

问题描述 投票:0回答:1

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

我已经成功加载了数据集并编译了模型,但是当我尝试将数据拟合到模型中时出现错误:

检查输入时出错:预期inputs_input具有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集线器引起的,但是我尝试从动手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)>)

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

我正在使用tf = 2.0.0和tensorflow hub。目前,我正在使用Tensorflow数据API加载存储在tfrecords文件中的数据。我已成功加载数据集并进行编译...

python tensorflow tensorflow-datasets tensorflow-hub
1个回答
0
投票

不确定原因,但是将特征定义更改为

© www.soinside.com 2019 - 2024. All rights reserved.