将feed_dict tf.session和tf.graph重写为估算器

时间:2018-09-14 03:23:22

标签: google-cloud-tpu

我有一些与feed_dict一起写到tf.Session和tf.Graph低级API的代码,由于我想在TPU上使用它,因此我试图将其重写为tf.Estimator API

下面是代码的当前版本。 (为简便起见,删除了一些片段并标记为...)

class my_tpu_class(object):

def __init__(self, ):
    // ...code to initialize class members
    self.g = tf.Graph()
    self._buildGraph()
    self.session = tf.Session(graph = self.g)

def _buildGraph(self):
    with self.g.as_default():
        XPH = tf.placeholder(tf.float32, [None, self.inputShape[0], self.inputShape[1], self.inputShape[2]], name='XPH')
        self.XPH = XPH
        YPH = tf.placeholder(tf.float32, [None, self.outputShape1[0] + self.outputShape2[0] + self.outputShape3[0] + self.outputShape4[0]], name='YPH')
        self.YPH = YPH

        conv1 = tf.layers.conv2d(inputs=XPH,
                                 filters=self.numFeature1,
                                 activation=selu.selu,
                                 name='conv1')
        self.conv1 = conv1

       // ...rest of code to build the network and get the loss.

        loss1 = tf.reduce_sum(tf.pow(YBaseChangeSigmoid - tf.slice(YPH,[0,0],[-1,self.outputShape1[0]], name='YBaseChangeGetTruth'), 2, name='YBaseChangeMSE'), name='YBaseChangeReduceSum')

        loss = loss1 + other losses...
        self.loss = loss

        tf.summary.scalar("loss", loss)
        self.merged_summary_op = tf.summary.merge_all()

        self.training_op = tf.train.AdamOptimizer(learning_rate=learningRatePH).minimize(loss)
        self.init_op = tf.global_variables_initializer()

def init(self):
    self.session.run( self.init_op )

def close(self):
    self.session.close()

def train(self, batchX, batchY):
    loss, _, summary = self.session.run( (self.loss, self.training_op, self.merged_summary_op),
                                          feed_dict={self.XPH:batchX, self.YPH:batchY, self.learningRatePH:self.learningRateVal,
                                          self.phasePH:True, self.dropoutRatePH:self.dropoutRateVal})
    return loss, summary

我通读了大部分estimator和tensorflow文档,并能够使用estimator界面提出以下版本。

class my_tpu_class(object):

def __init__(self, ):
    //...code to initialize class members

def my_model_fn(self, XPH, YPH, mode, params): 
        conv1 = tf.layers.conv2d(inputs=XPH,
                                 filters=self.numFeature1,
                                 activation=selu.selu,
                                 name='conv1')
        self.conv1 = conv1

        // rest of code to build the network and get the loss....

        loss1 = tf.reduce_sum(tf.pow(YBaseChangeSigmoid - tf.slice(YPH,[0,0],[-1,self.outputShape1[0]], name='YBaseChangeGetTruth'), 2, name='YBaseChangeMSE'), name='YBaseChangeReduceSum')

        loss = loss1 + other losses....
        self.loss = loss

        tf.summary.scalar("loss", loss)
        self.merged_summary_op = tf.summary.merge_all()

        self.training_op = tf.train.AdamOptimizer(learning_rate=params['learningRatePH']).minimize(loss)
        return tf.estimator.EstimatorSpec(mode=mode, loss=self.loss, train_op=self.training_op, eval_metric_ops=self.merged_summary_op)

def init(self):
    print ("No op")

def close(self):
    self.session.close()

def train_input_fn(self, features, labels):
    dataset = tf.data.Dataset.from_tensor_slices((features, labels))
    return dataset.make_one_shot_iterator().get_next()

def train(self, batchX, batchY):
    my_tpu_estimator = tf.estimator.Estimator( model_fn=self.my_model_fn, 
                                               params= {'learningRatePH':self.learningRateVal, 'phasePH':True, 'dropoutRatePH':self.dropoutRateVal })
    my_tpu_estimator.train(input_fn=self.train_input_fn(batchX, batchY))

这是执行此操作的正确方法,还是错误地理解了估算器概念?目前,该应用程序在训练函数调用中崩溃。所以我想我出了点问题。

1 个答案:

答案 0 :(得分:0)

您编写的代码不是TPU代码。它只是使用Estimator API,它是高级API。 根据我的看法,它应该可以在CPU或GPU上运行,但不能在TPU上运行。 对于TPU,您应该使用TPUEstimator API。

为了弄清楚崩溃的确切原因,我想知道如果您在TPU或CPU上运行它。还可以请您复制并粘贴崩溃时出现的控制台错误。