使用add的Tensorflow内存问题

时间:2018-03-05 17:51:22

标签: python tensorflow deep-learning

我基本上试图在tensorflow脚本中的for循环中递归更新dimension(1,batch_size)的张量对象。例如:

def __cost( self, trueX, trueY, reuse ):

    x = tf.expand_dims( trueX, 0 )
    X = tf.tile( x, [self.config['L'],1,1] )

    # X -> A
    A, mu_phi1, ls_phi1 = self.build_nn_gauss( x, self.config['sizeq_x2a'],
                          self.config['dim_a'], self.config['L'], 'phi1', reuse )
    _cost = -tf.reduce_sum( ls_phi1, -1 )

    # AX -> G (this is the semiclassifier)
    AX = tf.concat( [ A, X ], -1 )
    pre_G  = self.build_nn( AX, self.config['sizeq_ax2g'], '0phig2', reuse )
    log_pred_G = tf.layers.dense( pre_G, self.config['dim_g'], name='0phi2g', reuse=reuse )
    log_pred_G = log_pred_G - tf.reduce_logsumexp( log_pred_G, -1, keep_dims=True )
    pred_G = tf.exp( log_pred_G )


    if trueY is not None:
        # y = tf.expand_dims( trueY, 0 )
        # Y = tf.tile( y, [self.config['L'],1,1] )
        g = tf.expand_dims( trueG, 0 )
        G = tf.tile( g, [self.config['L'],1,1] )
        depth = 2

        _cost_supervised_bi_label = 0
        _cost_supervised_multi_label = tf.reduce_sum( tf.nn.sigmoid_cross_entropy_with_logits( logits=log_pred_G, labels= G), -1 )


        for i in range(self.config['dim_y']):

            y = tf.expand_dims( tf.one_hot(tf.to_int32(trueY[:,i], name='ToInt32'), depth, dtype=self.dtype ), 0 )
            Y = tf.tile( y, [self.config['L'],1,1] )

            # AGX -> Y (this is the classifier)
            AGX = tf.concat( [ A, G, X ], -1 )
            pre_Y  = self.build_nn( AGX, self.config['sizeq_agx2y'], '{}phi2'.format(i), reuse )
            log_pred_Y = tf.layers.dense( pre_Y, 2, name='{}phi2y'.format(i), reuse=reuse )
            log_pred_Y = log_pred_Y - tf.reduce_logsumexp( log_pred_Y, -1, keep_dims=True )
            pred_Y = tf.exp( log_pred_Y )

            # reuse layers if created or else create a new layer
            try:
                _cost += self.__add(_cost, self.__eval_cost_given_axgy( A, X, Y, G, True ))
            except:
                _cost += self.__add(_cost, self.__eval_cost_given_axgy( A, X, Y, G, False ))

            b = _cost 



            _cost_supervised_bi_label += -tf.reduce_sum( tf.multiply( Y, log_pred_Y ), -1 )

像这样运行它会消耗大量的gpu内存。我进一步做了一个测试,运行基本相同的东西,但删除了添加操作(例如):

_cost = self.__add(_cost, self.__eval_cost_given_axgy( A, X, Y, G, False ))

并停止大量内存消耗(Gbs减少)。我不知道出了什么问题。在tensorflow中添加和更新是否有不同的方式而不仅仅是更新张量?有没有办法我需要更新张量以避免内存泄漏?

 def __eval_cost_given_axgy( self, A, X, Y, G, reuse ):
    '''
    A, X, Y are 3D tensors of shape
    n_samples x batch_size x dim

    reuse -- whether to reuse NNs

    return 2D tensor of cost function
    n_samples x batch_size
    '''
    # A,G,X,Y -> Z
    AGXY = tf.concat( [A, G, X, Y], -1 )
    Z, mu_phi3, ls_phi3 = self.build_nn_gauss( AGXY, self.config['sizeq_agxy2z'],
                          self.config['dim_z'], self.config['L'], 'phi3', reuse )

    _cost = -tf.reduce_sum( ls_phi3, -1 )
    _cost += .5 * tf.reduce_sum( tf.pow( mu_phi3, 2 ), -1 )
    _cost += .5 * tf.reduce_sum( tf.exp( 2*ls_phi3 ), -1 )

    ...................

    return _cost

0 个答案:

没有答案