小编Kh4*_*tiK的帖子

张量流中的排列优化

问题设置

我有一批4x4矩阵,包含实值条目。

L = tf.placeholder('float32', shape=[None, 4, 4], name='pairwise-loss')
Run Code Online (Sandbox Code Playgroud)

我想找到一批 4-permutations,使得每个 4-permutation 最小化将排列作为单热掩码应用的总和。

def get_best_permutation(L_):
    '''
    Returns a batch of permutations `P` such that `tf.reduce_sum(tf.one_hot(P) * L_, axis=(1,2))`
      is minimized for each batch element.

    Args:
        L_: real valued tensor of shape [None, 4, 4]

    Returns:
        P: tf.int64 tensor of shape [None, 4]

    '''
    raise NotImpelmentedError()
Run Code Online (Sandbox Code Playgroud)

排列大小小且恒定,通常为 3-4。然而,批量大小预计会非常大。理想情况下,一切都应该在图形内完成,并在 GPU 上完成,因此数据传输更少。

编辑假设所有矩阵条目都是正数是安全的。

背景

这是为了实现类似于Permutation Invariant Training 的东西。

快速而肮脏的解决方案

可以预先计算所有可能的排列,因为它们很小,然后并行应用所有排列。最后申请tf.argmin找到最好的。但是我想要一个更有效的解决方案。

algorithm permutation tensorflow

5
推荐指数
0
解决办法
373
查看次数

用keras计算梯度范数权重

我正在尝试计算相对于具有keras的神经网络(作为诊断工具)的权重的梯度范数。最终,我想为此创建一个回调,但是在此过程中,我一直在努力创建一个可以计算梯度并以numpy数组/标量值形式(而不只是tensorflow)形式返回实际值的函数。张量)。代码如下:

import numpy as np
import keras.backend as K
from keras.layers import Dense
from keras.models import Sequential


def get_gradient_norm_func(model):
    grads = K.gradients(model.total_loss, model.trainable_weights)
    summed_squares = [K.sum(K.square(g)) for g in grads]
    norm = K.sqrt(sum(summed_squares))
    func = K.function([model.input], [norm])
    return func


def main():
    x = np.random.random((128,)).reshape((-1, 1))
    y = 2 * x
    model = Sequential(layers=[Dense(2, input_shape=(1,)),
                               Dense(1)])
    model.compile(loss='mse', optimizer='RMSprop')
    get_gradient = get_gradient_norm_func(model)
    history = model.fit(x, y, epochs=1)
    print(get_gradient([x]))

if  __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

调用时代码失败get_gradient()。追溯很长,涉及很多形状,但是关于什么是正确形状的信息很少。我该如何纠正?

理想情况下,我想要一个与后端无关的解决方案,但是基于tensorflow的解决方案也是一种选择。

2017-08-15 15:39:14.914388: W tensorflow/core/framework/op_kernel.cc:1148] Invalid …
Run Code Online (Sandbox Code Playgroud)

python neural-network keras tensorflow

3
推荐指数
1
解决办法
2705
查看次数