2
巨大なグローバル変数は列車と評価の両方で使用されますが、の異なる形のです。今では、同じプロセスで評価とトレーニングの両方を実行しようとしたので、テンソルフローグラフで定義された変数を実際に削除できないという事実に遭遇しました。インスタンスhereの回避策は、reset_default_graph()
を使用することですが、これはグラフのコンテキストマネージャでは動作しません。中グラフをテンソルフローにリセットする
import numpy as np
import tensorflow as tf
GRAPH = tf.Graph()
def train(examples):
with GRAPH.as_default() as g:
# actually this is huge variable
global_var = tf.get_variable('global_var',
initializer=np.full((examples, 32), 0.0),
trainable=False)
def evaluate(examples):
# tf.reset_default_graph() # ValueError: Variable input_var already exists
with GRAPH.as_default() as g: # initialized to some other size
tf.reset_default_graph()
global_var = tf.get_variable('global_var',
initializer=np.full((examples, 32), 0.0),
trainable=False)
# in fact tensorflow creates a new graph and does not use GRAPH to define global_var
train(32)
evaluate(8)
結果:
Traceback (most recent call last):
File "C:/Users/MrD/.PyCharm2017.1/config/scratches/scratch_44.py", line 22, in <module>
evaluate(8)
File "C:/Users/MrD/.PyCharm2017.1/config/scratches/scratch_44.py", line 19, in evaluate
trainable=False)
File "C:\_\Python35\lib\contextlib.py", line 66, in __exit__
next(self.gen)
File "C:\_\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3616, in get_controller
if self.stack[-1] is not default:
IndexError: list index out of range
のでreset_default_graphを使用する正しい方法は何ですか()?潜在的に巨大な初期化子を破棄する変数を再定義する方法はありませんか?
[これ](https://stackoverflow.com/questions/39352865/resetting-default-graph -does-not-remove-variables)は、それがうまくいかない理由、提案された解決策があなたの問題に合っているかどうかを確認しません。 – user1735003
ありがとう@ user1735003 - どのように私はこの変数(パフォーマンス上の理由でプレースホルダを使用することはできません)を処分することができる任意のアイデア? –
おそらく 'tf.assign'はあなたのために働くことができますか? ( 'validate_shape = False'で) – user1735003