这将是一个漫长而难以描述的事先道歉。
我有一个常规的CNN网络,其上面有标准的MLP图层。在MLP之上,我也有一个softmax层,但是,与传统网络不同,它与下面的MLP没有完全连接,它由子组组成。
为了进一步描述softmax,它看起来像这样:
Neur1A Neur2A ... NeurNA Neur1B Neur2B ... NeurNB Neur1C Neur2C ...NeurNC
Group A Group B Group C
还有更多的团体。每个组都有一个独立于其他组的softmax。所以它在某种程度上是几个独立的分类(尽管实际上并非如此)。
我需要的是激活的神经元的指数在组间单调增加。例如,如果我在A组中激活了Neuron5,我希望B组中激活的神经元>≥5。与B组和C组相同,等等。
这个包含所有组的所有神经元的softmax图层实际上不是我的最后一层,有趣的是它是中间层。
为了实现这种单调性,我在我的损失函数中增加了另一个术语,它惩罚了非单调激活的神经元指数。以下是一些代码:
softmax图层及其输出的代码:
def compute_image_estimate(layer2_input):
estimated_yps= tf.zeros([FLAGS.batch_size,0],dtype=tf.int64)
for pix in xrange(NUM_CLASSES):
pixrow= int( pix/width)
rowdata= image_pixels[:, pixrow*width:(pixrow+1)*width]
with tf.variable_scope('layer2_'+'_'+str(pix)) as scope:
weights = _variable_with_weight_decay('weights', shape=[layer2_input.get_shape()[1], width], stddev=0.04, wd=0.0000000)
biases = _variable_on_cpu('biases', [width], tf.constant_initializer(0.1))
y = tf.nn.softmax(tf.matmul(layer2_input,weights) + biases)
argyp=width-1-tf.argmax(y,1)
argyp= tf.reshape(argyp,[FLAGS.batch_size,1])
estimated_yps=tf.concat(1,[estimated_yps,argyp])
return estimated_yps
estimated_yps传递给量化单调性的函数:
def compute_monotonicity(yp):
sm= tf.zeros([FLAGS.batch_size])
for curr_row in xrange(height):
for curr_col in xrange(width-1):
pix= curr_row *width + curr_col
sm=sm+alpha * tf.to_float(tf.square(tf.minimum(0,tf.to_int32(yp[:,pix]-yp[:,pix+1]))))
return sm
和损失函数是:
def loss(estimated_yp, SOME_OTHER_THINGS):
tf.add_to_collection('losses', SOME_OTHER_THINGS)
monotonicity_metric= tf.reduce_mean( compute_monotonocity(estimated_yp) )
tf.add_to_collection('losses', monotonicity_metric)
return tf.add_n(tf.get_collection('losses'), name='total_loss')
现在我的问题是,当我不使用传统指标的SOME_OTHER_THINGS时,我得到ValueError: No gradients provided for any variable
的单调性指标。
当像这样使用softmax层输出时,似乎没有定义渐变。
我做错了吗?任何帮助将不胜感激。
答案 0 :(得分:1)
道歉..我意识到问题是tf.argmax函数显然没有定义渐变。