【问题标题】:How to modify this code to make it compatible with tensorflow2?如何修改此代码以使其与 tensorflow2 兼容?
【发布时间】:2021-07-01 01:15:49
【问题描述】:

代码在 tensorflow==1.14 和 keras==2.2.4 中运行良好:

def data_generator(images, descriptions, VGG16) :
    while True :
        for i in range(0, len(images)) :
            feature = VGG16.predict(images[i]);
            #variable description includes what the people are doing in this image : ["a man is cooking"]
            description = descriptions[i];
            #sequence includes things like ["a"ID, "man"ID, "is"ID, "cooking"ID], "xxx"ID is a digit of a word "xxx"
            sequence = make_sequence(description);
            yield [[feature, sequence], description];

//...
vgg_model = VGG16();
model = my_model();
generator = data_generator(images, descriptions, vgg_model);
model.fit_generator(generator, epochs = 1, verbose = 1, steps_per_epoch = 1);

模型的输入是带有描述的图像,模型的输出是词汇的概率分布。我想使用更高级的 CNN ResNet 152,它仅在 tensorflow==2.2 和最新版本的 keras 中可用。升级 tensorflow 和 keras 后,出现错误:

ValueError: Layer model expects 2 input(s), but it received 3 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, None) dtype=int32>, <tf.Tensor 'IteratorGetNext:2' shape=(None, None) dtype=float32>]

因为代码在老版本的tensorflow和keras中运行良好,所以我认为这个问题的根源来自data_generator。如何修改这段代码,使其兼容最新版本的tensorflow和keras?

【问题讨论】:

  • 我认为您应该使用生成器返回的内容。比如把[[feature, sequence], description];改成这个([feature, sequence], description);就可以解决了。
  • @Kaveh 问题解决了!

标签: python tensorflow keras deep-learning


【解决方案1】:

我认为您应该使用生成器返回的内容。

[[feature, sequence], description]; 更改为此([feature, sequence], description);,因为它需要xy 作为一个元组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2013-10-30
    • 2015-03-20
    • 2023-03-19
    • 1970-01-01
    • 2011-06-24
    相关资源
    最近更新 更多