【问题标题】:Documentation on how to use tf.estimator in TensorFlow关于如何在 TensorFlow 中使用 tf.estimator 的文档
【发布时间】:2017-09-27 03:23:32
【问题描述】:

我知道我们可以编写自定义模型并使用 tf.estimator 对其进行封装。但我似乎找不到任何带有示例的文档。

我知道你必须在“model_fn”中定义你的模型,但我应该从这个函数返回什么。我也应该将损失和训练步骤放在“model_fn”或只是网络中。我应该如何修改下面给出的代码以使其与 tf.estimator 一起使用。非常感谢您的帮助。

def test_model(features,labels):
    X = tf.placeholder(tf.float32,shape=(None,1),name="Data_Input")
    #Output
    Y = tf.placeholder(tf.float32,shape=(None,1),name="Target_Labels")
    W =  tf.Variable(tf.random_normal([0],stddev=stddev0)) 
    b = tf.Variable(tf.random_normal([0],stddev=stddev0))

    Ypredict = W*X + b
    return Ypredict

 estimator = tf.estimator.Estimator(model_fn = test_model)

【问题讨论】:

    标签: model tensorflow


    【解决方案1】:

    您应该返回一个tf.estimator.EstimatorSpec 对象。大意是:

    def model_fn(features, labels, mode, params):
        /*
        Your marvelous model
        */
        loss = tf.losses.softmax_cross_entropy(onehot_labels=labels_onehot, logits=logits)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
        train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())  
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
    

    还有更多内容,如需更好的演练,请参阅here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多