Tensorflow:估计器中 mean_squared_error 未提供梯度

Tensorflow: No gradients provided with mean_squared_error in estimator

我正在使用自定义估计器 api 开发二元分类器,代码如下。

我想尝试使用不同的损失函数,下面的代码通过 sigmoid_cross_entropy 或 sparse_softmax_cross_entropy 调用运行。但是当我尝试 mean_squared_error 时,我得到了堆栈跟踪

ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'dense/kernel:0' shape=(350, 18) dtype=float32_ref>", "<tf.Variable 'dense/bias:0' shape=(18,) dtype=float32_ref>", "<tf.Variable 'OUTPUT/kernel:0' shape=(18, 2) dtype=float32_ref>", "<tf.Variable 'OUTPUT/bias:0' shape=(2,) dtype=float32_ref>"] and loss Tensor("mean_squared_error/value:0", shape=(), dtype=float32).

这是代码,我怀疑是一些新手错误。 任何见解将不胜感激。 谢谢

# input layer                                                                                                                                                                                                                                                               
net = tf.feature_column.input_layer( features, params['feature_columns'] )

# hidden layer 1                                                                                                                                                                                                                                                            
net = tf.layers.dense(net, units=18, activation=tf.nn.relu)

# output layer computes logits                                                                                                                                                                                                                                              
logits = tf.layers.dense(net, params['n_classes'], activation=None, name='OUTPUT')

# sigmoid cross entropy                                                                                                                                                                                                                                                     
#multi_class_labels = tf.one_hot( labels, 2 )                                                                                                                                                                                                                               
#loss = tf.losses.sigmoid_cross_entropy(multi_class_labels=multi_class_labels, logits=logits)                                                                                                                                                                               

# sparse softmax cross entropy                                                                                                                                                                                                                                              
# loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

# mean squared error                                                                                                                                                                                                                                                        
predicted_classes = tf.argmax(logits, 1)                                                                                                                                                                                                                                   
loss = tf.losses.mean_squared_error(labels=labels, predictions=predicted_classes)                                                                                                                                                                                          

# TRAINING MODE                                                                                                                                                                                                                                                             
assert mode == tf.estimator.ModeKeys.TRAIN
optimizer = tf.train.AdagradOptimizer(learning_rate=0.1)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

这个 demo_model 自定义估算器是这样调用的

    classifier = tf.estimator.Estimator(
    model_fn=demo_model,
    model_dir=cur_model_dir,
    params={
        'feature_columns': feature_columns,
        # The model must choose between 2 classes.                                                                                                                                                                                                                          
        'n_classes': 2
    })

问题是 tf.argmax 没有定义梯度。您仍然可以使用均方误差将 logits 与单热编码标签进行比较:

loss = tf.losses.mean_squared_error(labels=tf.one_hot(labels, 2), predictions=logits)