Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give multiple arguments in tensorflow Model call function?

I'm trying to build a model in tensorflow by extending the 'Model' class in tensorflow.keras. I need to pass two arguments in the 'call' function of this class, input images x (224,224,3) and output label y. But I get the following error while building the model:

ValueError: Currently, you cannot build your model if it has positional or keyword arguments that are not input to the model, but are required for its 'call' method.

class myCNN(Model):
  def __init__(self):
    super(myCNN, self).__init__()

    base_model = tf.keras.applications.VGG16(input_shape=(224,224,3), weights='imagenet')
    layer_name = 'block5_conv3'
    self.conv_1 = Model(inputs=base_model.input, outputs=base_model.get_layer(layer_name).output)
    self.flatten = L.Flatten(name='flatten')
    self.fc1 = L.Dense(1000, activation='relu', name='fc1')
    self.final = L.Activation('softmax')

  # The problem is because I need y
  def call(self, x, y):
    x = self.conv_1(x)
    x = self.flatten(x)
    x = self.fc1(x)
    return self.final(x)

model = myCNN()
model.build((None, 224, 224, 3, 1))
like image 557
Prerna Garg Avatar asked May 31 '26 15:05

Prerna Garg


1 Answers

Inputs parameter of call method can be an input tensor or list/tuple of input tensors.

You can pass two arguments like this:

def call(self, inputs):
    x = inputs[0]
    y = inputs[1]
    x = self.conv_1(x)
    x = self.flatten(x)
    x = self.fc1(x)
    return self.final(x)  
like image 159
Shuhana Azmin Avatar answered Jun 02 '26 04:06

Shuhana Azmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!