image similarity model，The input sequence is inconsistent, resulting in inconsistent results
Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63132">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63132">No</a>
 new issue Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63132">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63132">No</a>

### Issue type

Support

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

source

### TensorFlow version

2.11.0

### Custom code

Yes

### OS platform and distribution

_No response_

### Mobile device

_No response_

### Python version

_No response_

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

### model definition
import tensorflow as tf
from keras import backend as K
from keras.layers import Lambda
from tensorflow.keras import layers, models
from tensorflow.keras.applications import ResNet50

tf.random.set_seed(2)


def euclidean_distance(vects):
    x, y = vects
    euclidean_dist = K.sqrt(K.sum(K.square(x - y), axis=1, keepdims=True))
    return 1 / (1 + euclidean_dist)

def build_similarity_model(base_model):
    base_model.trainable = False 

    input1 = layers.Input(shape=(224, 224, 3))
    input2 = layers.Input(shape=(224, 224, 3))

    x1 = base_model(input1)
    f1 = layers.Flatten()(x1)
    v1 = layers.Dense(256, activation='relu')(f1)
    d1 = layers.Dropout(0.5)(v1)

    x2 = base_model(input2)
    f2 = layers.Flatten()(x2)
    v2 = layers.Dense(256, activation='relu')(f2)
    d2 = layers.Dropout(0.5)(v2)

    distance = Lambda(euclidean_distance, output_shape=(1,))([d1, d2])

    model = models.Model(inputs=[input1, input2], outputs=distance)

    return model


base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

similarity_model = build_similarity_model(base_model)

### model train
from tensorflow.keras import optimizers
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image

from test import similarity_model


# 读取图像并进行预处理
def preprocess_image(image_path):
    img = image.load_img(image_path, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array = preprocess_input(img_array)
    return img_array

# 编译模型
similarity_model.compile(optimizer=optimizers.Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
print(similarity_model.summary())
# 准备数据集，这里假设有一个包含正样本和负样本的数据集
# 数据集结构：两个文件夹，分别存放相似和不相似的图像对

# 构造训练数据
X = []
y = []

image_path1 = '/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/1574820198658.jpg'
image_path2 = '/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/jietu-1709643042777.jpg'
image_path3 = '/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/1574821172023.jpg'

img1 = preprocess_image(image_path1)
img2 = preprocess_image(image_path2)
img3 = preprocess_image(image_path3)

X.append([img1, img2])
y.append(1)
X.append([img1, img3])
y.append(0)
X.append([img2, img3])
y.append(0)


X = np.array(X)
y = np.array(y)

# 训练模型
similarity_model.fit([X[:, 0], X[:, 1]], y, validation_data=([X[:, 0], X[:, 1]], y), epochs=100, batch_size=3)
similarity_model.save_weights('/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/weights.h5')

### Standalone code to reproduce the issue

```shell
import numpy as np
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras import backend as K

from test import similarity_model


def preprocess_image(image_path):
    img = image.load_img(image_path, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = preprocess_input(img_array)
    return img_array

def extract_features(model, img_array):
    features = model.predict(img_array)
    return features.flatten()


# 读取两个图像
# image_path1 = '/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/jietu-1709715631130.jpg'
# image_path1 = '/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/jietu-1709715631130.jpg'
image_path1 = '/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/jietu-1709643042777.jpg'
# image_path2 = '/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/1574821172023.jpg'
image_path2 = '/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/1574820198658.jpg'
K.set_learning_phase(0)
# 加载预训练的VGG16模型（包括权重）
similarity_model.load_weights('/home/hr/projects/baidu/bpit-ateam/aib-service-paddlenlp/weights.h5')
similarity_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

print(similarity_model.summary())

# 预处理图像并提取特征
img_array1 = preprocess_image(image_path1)
img_array2 = preprocess_image(image_path2)

features1 = extract_features(similarity_model, [img_array1, img_array2])
features2 = extract_features(similarity_model, [img_array2, img_array1])
print(features1)
print(features2)
```


### Relevant log output

```shell
2024-03-06 20:13:55.955842: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F AVX512_VNNI FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-03-06 20:13:56.121561: I tensorflow/core/util/port.cc:104] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-03-06 20:13:56.864236: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/hr/python3/lib:/usr/local/cuda-11.2/lib64:/home/hr/python3/lib:/usr/local/cuda-11.2/lib64:
2024-03-06 20:13:56.864303: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/hr/python3/lib:/usr/local/cuda-11.2/lib64:/home/hr/python3/lib:/usr/local/cuda-11.2/lib64:
2024-03-06 20:13:56.864313: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
2024-03-06 20:13:58.368233: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F AVX512_VNNI FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-03-06 20:13:59.159186: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1613] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 18022 MB memory:  -> device: 0, name: Tesla V100-SXM2-32GB, pci bus id: 0000:41:00.0, compute capability: 7.0
/home/hr/python3/lib/python3.7/site-packages/keras/backend.py:452: UserWarning: `tf.keras.backend.set_learning_phase` is deprecated and will be removed after 2020-10-11. To update it, simply pass a True/False value to the `training` argument of the `__call__` method of your layer or model.
  "`tf.keras.backend.set_learning_phase` is deprecated and "
Model: "model"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to                     
==================================================================================================
 input_2 (InputLayer)           [(None, 224, 224, 3  0           []                               
                                )]                                                                
                                                                                                  
 input_3 (InputLayer)           [(None, 224, 224, 3  0           []                               
                                )]                                                                
                                                                                                  
 resnet50 (Functional)          (None, 7, 7, 2048)   23587712    ['input_2[0][0]',                
                                                                  'input_3[0][0]']                
                                                                                                  
 flatten (Flatten)              (None, 100352)       0           ['resnet50[0][0]']               
                                                                                                  
 flatten_1 (Flatten)            (None, 100352)       0           ['resnet50[1][0]']               
                                                                                                  
 concatenate (Concatenate)      (None, 200704)       0           ['flatten[0][0]',                
                                                                  'flatten_1[0][0]']              
                                                                                                  
 dense (Dense)                  (None, 256)          51380480    ['concatenate[0][0]']            
                                                                                                  
 dense_1 (Dense)                (None, 1)            257         ['dense[0][0]']                  
                                                                                                  
==================================================================================================
Total params: 74,968,449
Trainable params: 51,380,737
Non-trainable params: 23,587,712
__________________________________________________________________________________________________
None
2024-03-06 20:14:05.296236: I tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:428] Loaded cuDNN version 8101
1/1 [==============================] - 4s 4s/step
1/1 [==============================] - 0s 36ms/step
[0.]
[1.]
```
