【发布时间】:2017-11-02 14:52:15
【问题描述】:
这是我在这里的第一篇文章,所以我希望它符合准则,并且除了我自己之外的其他人也很有趣。
我正在构建一个 CNN 自动编码器,它将固定大小的输入矩阵作为输入矩阵,目的是获得它们的低维表示(我在这里称它们为哈希)。当矩阵相似时,我想让这些哈希相似。由于只有少数数据被标记,我想让损失函数成为两个独立函数的组合。一部分将是自动编码器的重建误差(这部分工作正常)。另一部分,我希望它用于标记数据。由于我将拥有三个不同的类,因此我希望在每批中计算属于同一类的哈希值之间的距离(我无法实现这一点)。
到目前为止我的努力:
X = tf.placeholder(shape=[None, 512, 128, 1], dtype=tf.float32)
class1_indices = tf.placeholder(shape=[None], dtype=tf.int32)
class2_indices = tf.placeholder(shape=[None], dtype=tf.int32)
hashes, reconstructed_output = self.conv_net(X, weights, biases_enc, biases_dec, keep_prob)
class1_hashes = tf.gather(hashes, class1_indices)
class1_cost = self.calculate_within_class_loss(class1_hashes)
class2_hashes = tf.gather(hashes, class2_indices)
class2_cost = self.calculate_within_class_loss(class2_hashes)
loss_all = tf.reduce_sum(tf.square(reconstructed_output - X))
loss_labeled = class1_cost + class2_cost
loss_op = loss_all + loss_labeled
optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
calclulate_within_class_loss 是我创建的一个单独的函数。我目前仅针对同一批次中该类的第一个哈希与该类的其他哈希的区别来实现它,但是,我对当前的实现不满意,并且看起来它不起作用。
def calculate_within_class_loss(self, hash_values):
first_hash = tf.slice(hash_values, [0, 0], [1, 256])
total_loss = tf.foldl(lambda d, e: d + tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(e, first_hash)))), hash_values, initializer=0.0)
return total_loss
所以,我有两个问题/问题:
- 是否有任何简单的方法可以计算张量中每个原始数据与所有其他原始数据的距离?
- 我当前在类距离内计算的实现,即使它只是针对具有其他元素的第一个元素,当我尝试优化它时会给我一个“nan”。
感谢您的时间和帮助:)
【问题讨论】:
-
class?_hashes的形状是什么? -
对不起。我在这里只发布了我认为与问题相关的部分代码。由于哈希的形状为 [None, 256],因此类?_hashes 的形状也为 [?, 256],具体取决于我在当前批次中有多少个该类的索引。
标签: python tensorflow autoencoder