首页 > 解决方案 > 将网络的输出与它在 keras 自定义模型中接收到的输入合并(连接)会产生错误

问题描述

我试图用一些 Dense 层实现一个 jordan 网络,我想将网络的输出与输入合并(我希望网络看到它的 k 个最后预测值,但现在让它只使用最后一个值就足够了)。我在 keras 2 中使用自定义模型。我尝试了所有方法,但无论我使用 tf.concat 还是 tf.keras.layers.concatenation,merge(layer) 都会出错。该模型可以编译,但不会训练或预测。

错误是---> AttributeError:“连接”对象没有属性“形状”

class Jordan (tf.keras.Model):

    def __init__(self,num_feedback=1):
        super().__init__()
        self.feedback = np.zeros((1,num_feedback))
        self.l1 = tf.keras.layers.Dense(32, activation='relu')
        self.l2 = tf.keras.layers.Dense(16, activation='relu')
        self.outp = tf.keras.layers.Dense(1)

    def call(self, inputss):
        _concat = tf.concat ( [  self.feedback , inputss ] )
        _1 = self.l1(_concat)
        _2 = self.l2(_1)
        _output = self.outp(_2)
        self.feedback = _output
        return _output

a = np.ones((100,6))
b = np.ones((100,1))
model = jordan(10)
model.compile(optimizer='adam',loss='MSE',metrics=['accuracy'])
model.fit(a,b,epochs=10)

标签: pythontensorflowkeras

解决方案


好的,这是问题所在,在 model.fit() 中,您需要将 bach_size 调整为 self.feedback 的第一个维度 --------> 在此代码中它为 1,因此批量大小应为 1。


推荐阅读