如何将固定权重矩阵乘以角膜层输出

问题描述 投票:0回答:1

当我打算用恒定张量乘以keras输出时遇到问题?这是我的代码

a = tf.constant(ws, "float32")
output4 = AveragePooling3D(pool_size=(X_shape[0], 1, 1), strides = None, padding='valid')(output3)
output5 = Conv3D(filters=X_shape[3], kernel_size=(1, 1, 1),padding='same',data_format='channels_last')(output4)
output6 = Lambda( tf.multiply(output5, a))(output5)

这是我的错误“

ValueError:Tensor(“ Const:0”,shape =(1,1,21,27,3),dtype = float32)必须与Tensor(“ conv3d_1 / add:0”,shape =(无,1、21、27、3),dtype = float32)。

谢谢您的帮助!

python tensorflow keras multiplying
1个回答
0
投票

由于正在使用功能性API,因此需要将常数张量提供给模型或作为继承给Layer的对象的构造函数的输入。

方法1(首选)-作为自定义层的输入:

class ConstMul(tf.keras.layers.Layer):
    def __init__(self, const_val, *args, **kwargs):
        ....
        self.const = const_val

    def call(self, inputs, **kwargs):
        return inputs * self.const

方法2-作为单独的输入层:

const_inp = Input(shape)
....
output6 = Lambda( tf.multiply(output5, const_inp))

然后将const_inp添加到模型的输入列表中。不过,这不是最好的方法。

© www.soinside.com 2019 - 2024. All rights reserved.