在 Keras 中组合模型(输出)
Posted
技术标签:
【中文标题】在 Keras 中组合模型(输出)【英文标题】:Combine Models (outputs) in Keras 【发布时间】:2021-12-22 06:58:40 【问题描述】:我正在尝试构建以下论文中介绍的网络:link
基本上,自动编码器是其他两个模型的组合,嵌入器和恢复器如下所述:
X = Input(shape=[TIMESTEPS, FEAT], batch_size=BATCH_SIZE, name='RealData')
def recovery(self, H):
L1 = LSTM(HIDDEN_NODES, return_sequences=True)(H)
L2 = LSTM(HIDDEN_NODES, return_sequences=True)(L1)
L3 = LSTM(HIDDEN_NODES, return_sequences=True)(L2)
O = Dense(OUTPUT_NODES, activation='sigmoid', name='OUTPUT')(L3)
return O
def embedder(self, X):
L1 = LSTM(HIDDEN_NODES, return_sequences=True)(X)
L2 = LSTM(HIDDEN_NODES, return_sequences=True)(L1)
L3 = LSTM(HIDDEN_NODES, return_sequences=True)(L2)
O = Dense(HIDDEN_NODES, activation='sigmoid')(L3)
return O
最后,将它们与以下几行结合起来:
H = self.embedder(X)
X_tilde = self.recovery(H)
self.autoencoder = Model(inputs=X, outputs=X_tilde)
显示自动编码器的.summary
我有以下内容:
然后出现以下错误:
var_list = self.embedder.trainable_variables + self.recovery.trainable_variables
AttributeError: 'function' object has no attribute 'trainable_variables'
我做错了什么?
我复制的基线代码可以在here找到
【问题讨论】:
【参考方案1】:问题是embedder
和recovery
不是带有trainable_variables
的模型。这两个函数只是返回最后一层的输出。也许尝试这样的事情:
import tensorflow as tf
X = tf.keras.layers.Input(shape=[10, 10], batch_size=2, name='RealData')
def recovery():
model = tf.keras.Sequential([
tf.keras.layers.LSTM(10, return_sequences=True),
tf.keras.layers.LSTM(10, return_sequences=True),
tf.keras.layers.LSTM(10, return_sequences=True),
tf.keras.layers.Dense(10, activation='sigmoid', name='OUTPUT')
])
return model
def embedder():
model = tf.keras.Sequential([
tf.keras.layers.LSTM(10, return_sequences=True),
tf.keras.layers.LSTM(10, return_sequences=True),
tf.keras.layers.LSTM(10, return_sequences=True),
tf.keras.layers.Dense(10, activation='sigmoid')
])
return model
embedder_model = embedder()
H = embedder_model(X)
recovery_model = recovery()
X_tilde = recovery_model(H)
autoencoder = tf.keras.Model(inputs=X, outputs=X_tilde)
var_list = embedder_model.trainable_variables + embedder_model.trainable_variables
tf.print(var_list[:2])
[[[0.343916416 0.310338378 0.34440577 ... 0.0633761585 0.0405358076 0.276733816]
[0.245998859 0.197870493 0.0333348215 ... -0.136249736 0.271893084 -0.0605607331]
[-0.290359527 0.240957797 0.117871583 ... 0.172593892 0.113803834 0.0506341457]
...
[0.15672195 -0.161336392 -0.13484776 ... 0.306486845 -0.0707859397 0.245753765]
[0.00567743182 0.181330919 0.206510961 ... 0.0141542256 0.205756843 -0.074064374]
[0.299010575 -0.236641362 0.272176802 ... 0.0658480823 0.04648754 -0.342863292]], [[0.224076748 -0.112819761 -0.114276126 ... -0.190908 -0.282466382 -0.0711786151]
[-0.0689174235 0.203702673 -0.248280779 ... -0.0145524191 0.202952 0.0797807127]
[0.0919017 0.108805738 -0.124872617 ... 0.26839748 0.21041657 0.251440644]
...
[-0.117122218 -0.0974424109 -0.17138055 ... 0.150875479 0.0454813093 0.0753096]
[-0.115990438 -0.360190183 -0.0988362879 ... -0.0655761734 0.11425022 0.0291871373]
[-0.00164104556 -0.0442082509 0.135109842 ... -0.182655513 -0.0121813752 0.0497299805]]]
【讨论】:
成功了。谢谢!以上是关于在 Keras 中组合模型(输出)的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Keras 中组合两个具有不同输入大小的 LSTM 层?
如何在Tensorflow中组合feature_columns,model_to_estimator和dataset API