【问题标题】:AssertionError in Functional Model with Multiple Inputs when moving from TF1 to TF2从 TF1 移动到 TF2 时具有多个输入的功能模型中的 AssertionError
【发布时间】:2021-01-04 10:06:52
【问题描述】:

您好,我正在尝试将在 TF1 上运行的旧模型转换为 TF2,但遇到了一些问题。一直在使用 google colab 在 TF1 和 TF2 之间切换,使用 TF1 似乎一切正常,但使用 TF2 却不行。我已经用下面的一小段代码复制了这个问题。


from keras.layers import *
from keras import Model
from keras.backend import squeeze

def create_model():

    inputA = Input(shape=(1,))
    x = Dense(1)(inputA)
    x = Model(inputs=inputA, outputs=x)

    print(x.predict([0.1]))
    
    inputB = Input(shape=(1,))
    y = Dense(1)(inputB)
    y = Model(inputs=inputB, outputs=y)
    
    print(y.predict([0.1]))
    
    combined  = concatenate(inputs = [x.output,y.output])
    model = Model(inputs=[x.input, y.input], outputs=combined)
    
    return model


if (__name__  == "__main__") :
    model = create_model()
    model.compile(loss='mse',optimizer='RMSprop')
    model.summary()

    print(model.predict([[0.1],[0.1]]))


这是使用 TF2 的错误:

AssertionError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1462 predict_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1452 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1445 run_step  **
        outputs = model.predict_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1418 predict_step
        return self(x, training=False)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386 call
        inputs, training=training, mask=mask)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:517 _run_internal_graph
        assert x_id in tensor_dict, 'Could not compute output ' + str(x)

    AssertionError: Could not compute output Tensor("concatenate/concat:0", shape=(None, 2), dtype=float32)

我们将不胜感激。

谢谢, V_W

【问题讨论】:

    标签: tensorflow keras neural-network functional-programming artificial-intelligence


    【解决方案1】:

    您可以修改您的代码,例如,

    from tf.keras.layers import *
    from tf.keras import Model
    
    def create_model():
    
        inputA = Input(shape=(1,))
        x = Dense(1)(inputA)
        modelA = Model(inputs=inputA, outputs=x)
    
        print(modelA.predict([0.1]))
        
        inputB = Input(shape=(1,))
        y = Dense(1)(inputB)
        modelB = Model(inputs=inputB, outputs=y)
        
        print(modelB.predict([0.1]))
        
        concat = Concatenate()( [ x , y ] )
        model = Model(inputs=[ inputA, inputB ], outputs=concat )
        
        return model
    

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 2020-12-18
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 2017-09-14
      相关资源
      最近更新 更多