DachuanZhao
DachuanZhao

Reputation: 1339

How to use `model.predict` when the target is one of inputs in tensorflow?

I notice the layer LogisticEndpoint in https://www.tensorflow.org/guide/keras/train_and_evaluate#automatically_setting_apart_a_validation_holdout_set . The document build a model like this :

import numpy as np

inputs = keras.Input(shape=(3,), name="inputs")
targets = keras.Input(shape=(10,), name="targets")
logits = keras.layers.Dense(10)(inputs)
predictions = LogisticEndpoint(name="predictions")(logits, targets)

model = keras.Model(inputs=[inputs, targets], outputs=predictions)
model.compile(optimizer="adam")  # No loss argument!

data = {
    "inputs": np.random.random((3, 3)),
    "targets": np.random.random((3, 10)),
}
model.fit(data)

My question is that how to use this model when inference , since we don't know the target when we use model.predict

Upvotes: 0

Views: 804

Answers (1)

Frank
Frank

Reputation: 1249

The "LogisticEndpoint" is actually a layer. It takes the prediction and target as input, it can calculate the loss tracked by add_loss(), and calculate the precision scalar tracked by add_metric().

The target, if I am not wrong, is actually the ground truth data. When you make the inference (testing stage, neither training nor validating), you do not need the ground truth data. Just pass the input to the model, then take the output as the prediction.

To make the prediction of the multi-input:

First, convert the multi-input into one array (maybe a big one).

Then make sure the shape of the array (or precisely, tensor) matches the size of the input layer.

There is a part of the code in TF2 I am using for multi-input.

...
 for view_id in range(1,9):
   ...
                img = self.transform(img).float()
                pose_2d = self.transform(pose_2d).float()
                img_con.append(img)
                pose_2d_con.append(pose_2d)
# then make the img_con and pose_2d_con two tensor.

Upvotes: 1

Related Questions