在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/人工智能  Python/ 如何解決tensorflow反復(fù)調(diào)用預(yù)測函數(shù)導(dǎo)致內(nèi)存爆炸的問題?

如何解決tensorflow反復(fù)調(diào)用預(yù)測函數(shù)導(dǎo)致內(nèi)存爆炸的問題?

各位大神,這是我的預(yù)測函數(shù),輸入的是batch_size50030的array,輸出的是batch_size*2的包含預(yù)測分類和概率的array。
這個函數(shù)極其消耗內(nèi)存,監(jiān)測發(fā)現(xiàn)內(nèi)存總是突然暴漲然后迅速下降,迅速下降應(yīng)該是釋放函數(shù)內(nèi)的臨時變量,但是整體來看的話還是迅速在占用內(nèi)存(下降的沒有暴漲的多),導(dǎo)致這個函數(shù)循環(huán)調(diào)用30多次,我16G的內(nèi)存就爆掉了,請問該如何解決?
我猜測是由于with tf.Graph().as_default():導(dǎo)致什么東西沒有被當(dāng)做臨時變量釋放掉,然后不用這種寫法又會導(dǎo)致ckpt無法重復(fù)調(diào)用,報Key <variable_name_#n> not found in checkpoint,請問這個又該如何解決?

def evaluate_all_oneday_data(data,batch_size):
    with tf.Graph().as_default():
        BATCH_SIZE = batch_size
        N_CLASSES = 7
        n_inputs=30
        n_steps=500
        n_hidden_units=100
        
        tf_data=tf.reshape(data,[BATCH_SIZE,n_steps,n_inputs])
        
        #inference是個RNN的網(wǎng)絡(luò),最后的全連接層沒有加act_fun,所以這里加了softmax。
        logit = tf.nn.softmax(model_1206.inference((tf_data), BATCH_SIZE, N_CLASSES,n_inputs,n_steps,n_hidden_units))        
        x = tf.placeholder(tf.float32, shape=[BATCH_SIZE,500, 30])        
        logs_train_dir = 'G:\\first\\train501\\'
        
        saver = tf.train.Saver()
        
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(logs_train_dir)
            if ckpt and ckpt.model_checkpoint_path:
                #global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                pass
            prediction = sess.run(logit, feed_dict={x: data})
            pred = np.column_stack((np.argmax(prediction,axis=1),np.max(prediction,axis=1)))
    return pred
回答
編輯回答
妖妖

你可以在外面加載一下模型,預(yù)測的時候只是調(diào)用模型好了,不要每次都加載,應(yīng)該就可以避免這個問題。我一般是加載一次模型,然后預(yù)測所用數(shù)據(jù),完全不用再加載。

2018年6月5日 03:54