Я создаю собственный убыток в Keras с бэкэндом Tensorflow.
Внутри Lossfunction не работает извлечение части массива
(это мой первый пост, я пытаюсь соответствовать стандартам :))
Мой ввод - это массив матриц 80x80x1 (как x_train)
Функция потерь должна выполнить некоторые расчеты по прогнозируемым параметрам и вводу
Результатом этого расчета является минимизация потерь.
Проблема:
y_true
внутри таможенной потери:
y_true
форма: [партия 80 80 1]
Вопрос в том, как мне вернуть 80х80 с y_true
. Следующий код не работает:
extracted_data_80x80 = y_true[ 0, :, :, 0]
Соответствующее сообщение об ошибке следующее:
ValueError: Index out of range using input dim 2; input has only 2 dims for 'loss/dense_3_loss/strided_slice_7' (op: 'StridedSlice') with input shapes: [?,?], [4], [4], [4] and with computed input tensors: input[3] = <1 1 1 1>.
Пример кода:
# -------
# Import
# -------
import tensorflow as tf
import numpy as np
# -----------------------
# Generating random input
# 100 pices of 8x8 array
# -----------------------
X_train = np.random.rand(10000,8,8)
y_train = np.random.rand(10000,8,8)
# Here we add an additional Dimension to be able to work with
# Keras Conv2D (maybe this step is the origin of the problem)
X_train = np.expand_dims(X_train, axis=4)
y_train = np.expand_dims(y_train, axis=4)
# -----------------
# The Loss Function
# -----------------
def CustomLoss(y_true, y_pred):
#y_true[batch, 8, 8, 1]
# Extracting the 8x8 part from y_true
extracted_data_80x80 = y_true[0, :, :, 0] # This is NOT working
return tf.reduce_sum(extracted_data_80x80 )
# -----------
# Keras Model
# -----------
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
model = Sequential()
# add model layers
model.add(Conv2D(64, kernel_size=3, activation="relu", input_shape=(8, 8, 1),data_format="channels_last"))
model.add(Flatten())
model.add(Dense(3, activation="relu"))
# compile model using *CustomLoss* as loss
model.compile(optimizer="adam", loss = CustomLoss)
#train the model
model.fit(X_train, y_train, epochs=100, batch_size=2, verbose=1)
Итак, я действительно задаюсь вопросом, что было бы здесь правильным способом извлечь часть Тензор 80x80?
Спасибо за ваше время, вашу помощь и спасение моей жизни :) 1031 *