def _get_cost(self, outputs):
"""Construct the cost function from the outputs of the last layer. This
will be used through SGD to train the network.
Parameters
----------
outputs: tuple fo tensors (n_out)
a tuple of tensor containing the output from the last layer of the
network
Returns
-------
cost: a tensor computing the cost function of the network.
reg: a tensor for computing regularization of the parameters.
It should be None if no regularization is needed.
"""
Zk, X, lmbd = outputs
with tf.name_scope("reconstruction_zD"):
rec = tf.matmul(Zk, tf.constant(self.D))
with tf.name_scope("norm_2"):
Er = tf.multiply(
tf.constant(.5, dtype=tf.float32),
tf.reduce_mean(tf.reduce_sum(tf.squared_difference(rec, X),
reduction_indices=[1])))
with tf.name_scope("norm_1"):
l1 = lmbd * tf.reduce_mean(tf.reduce_sum(
tf.abs(Zk), reduction_indices=[1]))
return tf.add(Er, l1, name="cost")
评论列表
文章目录