python - 将网络的输出与它在 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)
解决方案
好的,这是问题所在,在 model.fit() 中,您需要将 bach_size 调整为 self.feedback 的第一个维度 --------> 在此代码中它为 1,因此批量大小应为 1。
推荐阅读
- javascript - 如何在反应中更新文本框的默认值
- python - 大写字符串和小写字符串的比较?
- javascript - 电子通知 API“点击”事件不起作用
- google-chrome-extension - 是否可以在安装之间以某种方式保留 Web 扩展设置?
- reactjs - 将 React 组件作为其他数据数组的一部分传递?
- powerbi - Power BI - 基于连续平均销售价格的同比百分比变化
- html - 我似乎无法将我从计算机上选择的图像附加到我网站上的轮播上。我的轮播是三张图片
- android - 如何从 Android-Library 覆盖 BuildConfig?
- python - Python 3 无法识别布尔条件
- php - Laravel:帖子类别未更新