<p>使用<a href="https://keras.io/getting-started/functional-api-guide/" rel="nofollow noreferrer">Keras functional API</a>可以很容易地实现这个自动编码器。这将允许有一个额外的输入占位符<code>y_prec_input</code>,它将与“precision”向量一起输入。完整的源代码可以找到<a href="https://gist.github.com/rvinas/9e81ae0f17e61cc2c54b63f45fb07a28" rel="nofollow noreferrer">here</a>。在</p>
<hr/>
<p><strong>数据生成器</strong></p>
<p>首先,让我们重新实现数据生成器,如下所示:</p>
<pre><code>class DataGenerator(Sequence):
def __init__(self, batch_size, y, prec, shuffle=True):
self.batch_size = batch_size
self.y = y
self.shuffle = shuffle
self.prec = prec
self.on_epoch_end()
def on_epoch_end(self):
self.indexes = np.arange(len(self.y))
if self.shuffle:
np.random.shuffle(self.indexes)
def __len__(self):
return int(np.floor(len(self.y) / self.batch_size))
def __getitem__(self, index):
indexes = self.indexes[index * self.batch_size: (index + 1) * self.batch_size]
y = self.y[indexes]
y_prec = self.prec[indexes]
return [y, y_prec], y
</code></pre>
<p>注意,我去掉了全局变量。现在,精度向量<code>P</code>作为输入参数(<code>prec</code>)提供,生成器生成一个额外的输入,该输入将馈送给精度占位符<code>y_prec_input</code>(请参见模型定义)。在</p>
<hr/>
<p><strong>型号</strong></p>
<p>最后,您的模型可以定义和训练如下:</p>
^{pr2}$
<p>其中<code>input_dim = digits.data.shape[1]</code>。请注意,我还将解码器的输出维度更改为<code>input_dim</code>,因为它必须与输入维度匹配。在</p>