我正在处理极其嘈杂的数据偶尔会充满异常值,所以我主要依靠相关性来衡量我的NN的准确性.
是否有可能明确地使用诸如秩相关(Spearman相关系数)之类的东西作为我的成本函数?到目前为止,我主要依靠MSE作为相关代理.
我现在有三个主要的绊脚石:
1)对于小批量,排名的概念变得更加模糊.
2)你如何动态执行排名?TensorFlow会不会出现梯度误差/无法跟踪重量/偏差的变化如何影响成本?
3)如何确定运行期间正在查看的张量的大小?
例如,如果我只是使用相关性,下面的代码就是我想要大致做的事情.实际上,长度需要传递而不是在运行时确定.
length = tf.shape(x)[1] ## Example code. This line not meant to work.
original_loss = -1 * length * tf.reduce_sum(tf.mul(x, y)) - (tf.reduce_sum(x) * tf.reduce_sum(y))
divisor = tf.sqrt(
(length * tf.reduce_sum(tf.square(x)) - tf.square(tf.reduce_sum(x))) *
(length * tf.reduce_sum(tf.square(y)) - tf.square(tf.reduce_sum(y)))
)
original_loss = tf.truediv(original_loss, divisor)
Run Code Online (Sandbox Code Playgroud)
Here is the code for the Spearman correlation:
\n\npredictions_rank = tf.nn.top_k(predictions_batch, k=samples, sorted=True, name=\'prediction_rank\').indices\nreal_rank = tf.nn.top_k(real_outputs_batch, k=samples, sorted=True, name=\'real_rank\').indices\nrank_diffs = predictions_rank - real_rank\nrank_diffs_squared_sum = tf.reduce_sum(rank_diffs * rank_diffs)\nsix = tf.constant(6)\none = tf.constant(1.0)\nnumerator = tf.cast(six * rank_diffs_squared_sum, dtype=tf.float32)\ndivider = tf.cast(samples * samples * samples - samples, dtype=tf.float32)\nspearman_batch = one - numerator / divider\n
Run Code Online (Sandbox Code Playgroud)\n\nThe problem with the Spearman correlation is that you need to use a sorting algorithm (top_k
in my code). And there is no way to translate it to a loss value. There is no derivade of a sorting algorithm. You can use a normal correlation but I think there is no mathematically difference to use the mean squared error.
I am working on this right now for images. What I have read in papers that they use to add the ranking into the loss function is to compare 2 or 3 images (where I say images you can say anything you want to rank).
\n\nComparing two elements:
\n\n\n\n\n\nWhere N is the total number of elements and \xce\xb1 a margin value. I got this equation from Photo Aesthetics Ranking Network with Attributes and Content Adaptation
\n\nYou can also use losses with 3 elemens where you compare two of them with similar ranking with another one with a different one:
\n\n\n\nBut in this equation you also need to add the direction of the ranking, more details in Will People Like Your Image?. In the case of this paper they use a vector encodig instead of a real value but you can do it for just a number too.
\n\nIn the case of images, the comparison between images makes more sense when those images are related. So it is a good idea to run a clustering algorithm to create (maybe?) 10 clusters, so you can use elements of the same cluster to make comparisons instead of very different things. This will help the network as the inputs are related somehow and not completely different.
\n\n作为旁注,您应该知道什么对您来说更重要,是最终排名顺序还是排名值。如果它是你应该使用均方误差的值,如果它是排名顺序,你可以使用我之前写的损失。或者你甚至可以将它们结合起来。
\n\n\n\n\n如何确定在运行时查看的张量的大小?
\n
tf.shape(tensor)
返回具有形状的张量。然后你就可以使用它tf.gather(tensor,index)
来获取你想要的值。