質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

845閲覧

tf.get_variablesが定義されないためtf.train.AdamOptimizerが使えない原因

trafalbad

総合スコア303

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/08/20 05:52

編集2017/08/20 06:41

下記のようにGeneratorとDiscriminatorを定義したあとにtf.train.AdamOptimizerを使おうとすると下記エラーが出て使用できないのは何故なのでしょうか?

ValueError: Variable discriminator/d_h0_conv/w/Adam/ does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

tf.train.GradientDescentOptimizer(1e-3)は使えるようですが、tf.train.AdamOptimizerを使わないと上手くいかないのです。どうしたらこのoptimizerが使えるようになるのでしょうか?
ご教授お願いいたします。

追記
早急に解決の必要があるためマルチポストをしてしましました。複数の人に見てもらった方が早く解決できると思ったからです。
リンク

def discriminator(image, reuse=False): batch_size=64 with tf.variable_scope("discriminator") as scope: if reuse: scope.reuse_variales() h0 = lrelu(conv2d(image, 64, name='d_h0_conv')) h1 = lrelu(batch_norm(conv2d(h0, 128, name='d_h1_conv'),'d_bn1')) h2 = lrelu(batch_norm(conv2d(h1, 256, name='d_h2_conv'),'d_bn2')) h3 = lrelu(batch_norm(conv2d(h2, 512, name='d_h3_conv'),'d_bn3')) # shape=(batch_size, 64, 64, 3) h4 = linear_d(tf.reshape(h3, [batch_size, -1]),2,'d_h4_lin') return h4 # shape=(batch_size, 64, 64, 3) def generator(z_): batch_size=64 with tf.variable_scope("generator") as scope: # project `z` and reshape z, h0_w, h0_b = linear(z_, 64*8*4*4, 'g_h0_lin',with_w=True) h0 = tf.nn.relu(batch_norm(tf.reshape(z, [-1, 4, 4, 64*8]), 'g_bn0')) h1, h1_w, h1_b = deconv2d(h0, [batch_size, 8, 8, 64*4], name='g_h1', with_w=True) h1 = tf.nn.relu(batch_norm(h1, 'g_bn1')) h2, h2_w, h2_b = deconv2d(h1, [batch_size, 16, 16, 64*2], name='g_h2', with_w=True) h2 = tf.nn.relu(batch_norm(h2, 'g_bn2')) h3, h3_w, h3_b = deconv2d(h2, [batch_size, 32, 32, 64*1], name='g_h3', with_w=True) h3 = tf.nn.relu(batch_norm(h3, 'g_bn3')) h4, h4_w, h4_b = deconv2d(h3, [batch_size, 64, 64, 3], name='g_h4', with_w=True) return tf.nn.tanh(h4) #shape=(batch_size, 64, 64, 3) def sampler(z_):# shape=(batch_size, 64, 64, 3) batch_size=64 with tf.variable_scope("generator") as scope: # project `z` and reshape z= linear(z_, 64*8*4*4,'g_h0_lin') h0 = tf.nn.relu(batch_norm(tf.reshape(z, [-1, 4, 4, 64*8]),'g_bn0',train=False)) h1 = deconv2d(h0, [batch_size, 8, 8, 64*4], name='g_h1') h1 = tf.nn.relu(batch_norm(h1,'g_bn1',train=False)) h2 = deconv2d(h1, [batch_size, 16, 16, 64*2], name='g_h2') h2 = tf.nn.relu(batch_norm(h2,'g_bn2',train=False)) h3 = deconv2d(h2, [batch_size, 32, 32, 64*1], name='g_h3') h3 = tf.nn.relu(batch_norm(h3,'g_bn3',train=False)) h4 = deconv2d(h3, [batch_size, 64, 64, 3], name='g_h4') return tf.nn.tanh(h4) #shape=(batch_size, 64, 64, 3) G=generator(z) #G(z) D_logits = discriminator(image) #D(x) tf.get_variable_scope().reuse_variables() sampler = sampler(z) D_logits_ = discriminator(G) #D(G(z)) batch_label=64 d_loss_real = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=D_logits, labels=tf.ones([batch_label], dtype=tf.int64))) d_loss_fake = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=D_logits_, labels=tf.zeros([batch_label], dtype=tf.int64))) g_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=D_logits_, labels=tf.ones([batch_label], dtype=tf.int64))) d_loss = d_loss_real + d_loss_fake d_vars = [var for var in tf.trainable_variables() if 'd_' in var.name] g_vars = [var for var in tf.trainable_variables() if 'g_' in var.name] saver=tf.train.Saver() g_optim = tf.train.AdamOptimizer(learning_rate=0.0002, beta1=0.5).minimize(g_loss, var_list=g_vars) d_optim = tf.train.AdamOptimizer(learning_rate=0.0002, beta1=0.5).minimize(d_loss, var_list=d_vars) ValueError: Variable discriminator/d_h0_conv/w/Adam/ does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

回答2

0

自己解決

tf.get_variable_scope().reuse_variables()
を使わずに関数内でreuse=Trueを指定してあげれば解決しました

python

1def sampler():# shape=(batch_size, 64, 64, 3) 2 reuse = False 3 def model(z_): 4 nonlocal reuse 5 batch_size=64 6 with tf.variable_scope("generator") as scope: 7 # project `z` and reshape 8 z= linear(z_, 64*8*4*4,'g_h0_lin') 9 h0 = tf.nn.relu(batch_norm(tf.reshape(z, [-1, 4, 4, 64*8]),'g_bn0',train=False)) 10 h1 = deconv2d(h0, [batch_size, 8, 8, 64*4], name='g_h1') 11 h1 = tf.nn.relu(batch_norm(h1,'g_bn1',train=False)) 12 h2 = deconv2d(h1, [batch_size, 16, 16, 64*2], name='g_h2') 13 h2 = tf.nn.relu(batch_norm(h2,'g_bn2',train=False)) 14 h3 = deconv2d(h2, [batch_size, 32, 32, 64*1], name='g_h3') 15 h3 = tf.nn.relu(batch_norm(h3,'g_bn3',train=False)) 16 h4 = deconv2d(h3, [batch_size, 64, 64, 3], name='g_h4') 17 reuse = True 18 return tf.nn.tanh(h4) #shape=(batch_size, 64, 64, 3) 19 return model

投稿2017/08/20 11:47

編集2017/08/20 11:49
trafalbad

総合スコア303

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

提示コード内容は把握・確認していませんが、エラー内容からほぼ同様のコードと思われる回答が見つかりました。
TensorFlow ValueError: Variable does not exist, or was not created with tf.get_variable()

以下のように修正することでエラーは発生しなくなるかもしれません

Python

1with tf.variable_scope(tf.get_variable_scope(),reuse=False): 2 g_optim = tf.train.AdamOptimizer(3 d_optim = tf.train.AdamOptimizer(

もしかすると過去の以下の質問と同様の原因かもしれません。
解決もしているので同じ原因かご自身で確認ください。
tensorflowのエラーコマンドの解決方法について

ちなみに上記の回答にてTensorFlowの変数についての参考URLを示しているので、こちらも併せて確認ください。
TensorFlowの計算グラフ内の変数tf.Variableの使い方

投稿2017/08/20 07:06

can110

総合スコア38233

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

trafalbad

2017/08/20 07:08

このサイトはすでに見て、試したのですが、同様のエラーが発生してしまいました。
can110

2017/08/20 07:16

エラーメッセージからは、そのような変数は存在しない あるいは tf.get_variable()で生成されていないのが 原因としか分かりません。 提示したコードにて、あなたはどのような変数を定義し、どのように使おうとしているのかを把握・ご理解されているでしょうか?それを追記すると回答が得られるかもしれません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問