问题描述
我有一个形状为(64、64、2)的2通道numpy数组,作为我的CNN的输入.我要构建 https://www.tensorflow.org/guide中所述的自定义损失函数/keras/train_and_evaluate :
I have a 2-channel numpy array of shape (64, 64, 2) as input to my CNN.I want to build a customized loss function as described in https://www.tensorflow.org/guide/keras/train_and_evaluate :
def basic_loss_function(y_true, y_pred):
return tf.math.reduce_mean(tf.abs(y_true - y_pred))
model.compile(optimizer=keras.optimizers.Adam(),
loss=basic_loss_function)
model.fit(x_train, y_train, batch_size=64, epochs=3)
但是我想要比这个基本的东西更复杂的东西.我需要做一个逆DFT(ifft2d),并且我的y_pred和y_true期望分别是形状(64,64,2),其中2个通道是fft2的实部和虚部.如何正确访问y_pred和y_true通道(我猜这是某种keras/张量层?)以 RealPart + 1j * ImagPart 的形式重建复数(用numpy表示) y_pred [:,:,0]和y_pred [:,:,1])吗?
But I want to something more complicated that this basic one. What I need is to do an inverse DFT (ifft2d) and my y_pred and y_true are expected to be each of shape (64,64,2), with the 2 channels being the real and imaginary parts of a fft2. How can I access correctly the y_pred and y_true channels (which are some kind of keras/tensor layer I guess?) to rebuild a complex number in the form RealPart+1j*ImagPart (in numpy it would be y_pred[:,:,0] and y_pred[:,:,1] ) ?
->总之,有人知道y_pred和y_true是哪种对象以及如何访问其通道/元素吗? (这不容易调试,因为它需要在已编译的CNN中运行,因此请事先了解一下)
--> In summary, does someone know exactly what kind of object is y_pred and y_true and how to access their channels/elements? (This is not easy to debug since would need to be run in a compiled CNN, so better know it beforehand)
推荐答案
y_true
和y_pred
是形状为(batchsize, ...[output shape]...)
的张量.输入的形状为(64,64,2)
,但我不确定输出的外观,如果输出确实为(64,64,2)
,则y_pred
或y_true
的形状为(64,64,64,2)
.
y_true
and y_pred
are Tensors of shape (batchsize, ...[output shape]...)
. Your input has shape (64,64,2)
but I'm not sure what your output looks like, if your output is indeed (64,64,2)
then y_pred
or y_true
have shape (64,64,64,2)
given your batchsize=64
.
使用张量进行处理非常类似于numpy的语法,因此您可以使用带有张量的切片符号,例如y_true[:,:,:,0]
(请注意添加的批处理维度).
Dealing with Tensors is very much like numpy's syntax so You can use slice notation with tensors e.g., y_true[:,:,:,0]
(note the added batch dimension).
Tensorflow具有用于计算DFT,FFT等的功能.参见 tf.signal 和 tf.signal.rfft2d
Tensorflow has functions for computing DFT,FFT,..etc. See tf.signal and tf.signal.rfft2d
如果损失函数涉及输入操作,而不仅仅是输出y_true
和y_pred
,则可以按以下方式使用model.add_loss
代替model.compile(loss= basic_loss_function)
If your loss function involves operations on the input, not just the outputs y_true
and y_pred
, then you can use model.add_loss
instead of model.compile(loss= basic_loss_function)
as follows
x = Input(shape=(64,64,2))
y_true = Input(shape=...))
# your CNN layers
y_pred = Dense(128)(net)
model = Model(input=[x, y_true], output=y_pred)
model.add_loss(basic_loss_function(x, y_true, y_pred))
请注意,标签(又名y_true
)现在是模型的输入.
note that the labels (aka y_true
) is now an input to the model.
这篇关于自定义损失函数访问张量通道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!