Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not compute output" error using tf.keras merge layers in Tensorflow 2

I'm trying to use a merge layer in tf.keras but getting AssertionError: Could not compute output Tensor("concatenate_3/Identity:0", shape=(None, 10, 8), dtype=float32). Minimal (not)working example:

import tensorflow as tf
import numpy as np

context_length = 10 

input_a = tf.keras.layers.Input((context_length, 4))
input_b = tf.keras.layers.Input((context_length, 4))

#output = tf.keras.layers.concatenate([input_a, input_b]) # same error
output = tf.keras.layers.Concatenate()([input_a, input_b])

model = tf.keras.Model(inputs = (input_a, input_b), outputs = output)

a = np.random.rand(3, context_length, 4).astype(np.float32)
b = np.random.rand(3, context_length, 4).astype(np.float32)

pred = model(a, b)

I get the same error with other merge layers (e.g. add). I'm on TF2.0.0-alpha0 but get the same with 2.0.0-beta1 on colab.

like image 988
daknowles Avatar asked Mar 04 '23 14:03

daknowles


2 Answers

Ok well the error message was not helpful but I eventually stumbled upon the solution: the input to model needs to be an iterable of tensors, i.e.

pred = model((a, b))

works just fine.

like image 187
daknowles Avatar answered May 04 '23 21:05

daknowles


It fails because of the tf.keras.layers.Input. Tensorflow can't validate the shape of the layer thus it fails. This will work:

class MyModel(tf.keras.Model):

    def __init__(self):
        super(MyModel, self).__init__()
        self.concat = tf.keras.layers.Concatenate()
        # You can also add the other layers
        self.dense_1 = tf.keras.layers.Dense(10)

    def call(self, a, b):
        out_concat = self.concat([a, b])
        out_dense = self.dense_1(out_concat)

model = MyModel()

a = np.random.rand(3, 5, 4).astype(np.float32)
b = np.random.rand(3, 5, 4).astype(np.float32)

output = model(a, b)
like image 22
gorjan Avatar answered May 04 '23 22:05

gorjan