Tensorflow:估计器中没有随mean_squared_error提供的渐变

时间:2018-04-26 13:12:55

标签: tensorflow tensorflow-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).

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

# 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
    })

1 个答案:

答案 0 :(得分:4)

问题是tf.argmax没有定义的渐变。您仍然可以使用平均误差来比较logits与one-hot编码标签:

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