Как передать 2 выходных данных из модели — логиты и карты внимания в пользовательскую функцию потерьPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как передать 2 выходных данных из модели — логиты и карты внимания в пользовательскую функцию потерь

Сообщение Anonymous »


I am working on a model that has 2 outputs: 1st the predictions and 2nd the attention maps. I have stored my target attention maps on my disk as '.npy' files and have loaded and stored in the runtime as reshaped_y_train_fm. I want to pass the attention maps stored by me along with the labels for every x_train.

How can I do this?

I am using the following code for creating the model:-

def student_model_generator(input_shape, num_classes): inputs = Input(shape=input_shape) x = Conv2D(64, (3, 3), padding='same', activation='relu')(inputs) custom_fm = process_fm(x) x = GlobalAveragePooling2D()(custom_fm) predictions = Dense(num_classes, activation='softmax', name="predictions")(x) return tf.keras.Model(inputs=inputs, outputs=[predictions, custom_fm], name='student') I am using a small subset of the actual data in CIFAR-100 dataset :5 images from each category for trial(accuracy dosen't matter only the code should run).

Following is some information about the data that i am using:-

type of x_train shape of x_train (500, 32, 32, 3) type of y_train shape of y_train (500, 100) type of resized_y_train size of resized_y_train 500 shape of every element in resized_y_train_fm (32, 32) Following is the code that i am using to call the fit method.

student_model.compile(optimizer='adam', loss=total_loss) student_params = student_model.fit(x_train, [y_train, resized_y_train_fm], epochs=10) I am getting the following error:-

ValueError Traceback (most recent call last) Cell In[31], line 2 1 student_model.compile(optimizer='adam', loss=total_loss) ----> 2 student_params = student_model.fit(x_train, [y_train, resized_y_train_fm], epochs=10) File ~\AppData\Roaming\Python\Python311\site-packages\keras\src\utils\traceback_utils.py:70, in filter_traceback..error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.__traceback__) 68 # To get the full stack trace, call: 69 # `tf.debugging.disable_traceback_filtering()` ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb File ~\AppData\Roaming\Python\Python311\site-packages\keras\src\engine\data_adapter.py:1960, in _check_data_cardinality(data) 1953 msg += " {} sizes: {}\n".format( 1954 label, 1955 ", ".join( 1956 str(i.shape[0]) for i in tf.nest.flatten(single_data) 1957 ), 1958 ) 1959 msg += "Make sure all arrays contain the same number of samples." -> 1960 raise ValueError(msg) ValueError: Data cardinality is ambiguous: x sizes: 500 y sizes: 500, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 Make sure all arrays contain the same number of samples How can I call the fit method to successfully train the model.


Источник: https://stackoverflow.com/questions/780 ... ustom-loss
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»