Windows10, Anaconda上でtensorflow1.4.0を用いて学習済みモデルの読み込みをしたいと考えています。そこでMNISTのコードを改変しましたが、現状、変数がrestoreされず困っています。
###変数をsaveするコード
このコードではrestoreができているようです。
python:savemodel.py
1 2import tensorflow as tf 3from tensorflow.examples.tutorials.mnist import input_data 4 5# データ読み込み 6mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 7 8# placeholder用意 xは学習用画像 9x = tf.placeholder(tf.float32, [None, 784]) 10# y_は学習用ラベル 11y_ = tf.placeholder(tf.float32, [None, 10]) 12 13# weightとbias 14# さっきの例ではw * xだったけど、今回はw * x + b 15W = tf.Variable(tf.zeros([784, 10]),name='W') 16b = tf.Variable(tf.zeros([10]),name='b') 17 18# Softmax Regressionを使う yはモデル 19y = tf.nn.softmax(tf.matmul(x, W) + b) 20 21# 交差エントロピー 22cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) 23 24# 先ほど使ったGradientDescentOptimizerで、今回はcross_entropyを利用 25train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 26 27model_path = './model/model.ckpt' 28 # 初期化 29init = tf.global_variables_initializer() 30sess = tf.Session() 31sess.run(init) 32 33# 学習 34for i in range(1000): 35 batch_xs, batch_ys = mnist.train.next_batch(100) 36 sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 37 if i == 999: 38 saver = tf.train.Saver() 39 saver.save(sess,model_path) 40 print("model saved" + model_path) 41 print(sess.run(W)) 42 print(sess.run(b)) 43 44# テストデータで予測 45correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 46accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 47acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}) 48print('accuracy : '+str(acc)); 49sess.close() 50 51sess = tf.InteractiveSession() 52init = tf.global_variables_initializer() 53sess.run(init) 54 55saver.restore(sess,model_path) 56print(sess.run(W)) 57print(sess.run(b)) 58correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 59accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 60acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}) 61print('accuracy : '+str(acc)) 62
実行時コンソール出力
この時Wとbの値を表示させておりどちらも同じ値が確認できます。
Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz model saved./model/model.ckpt [[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]] [-0.61506635 0.45124692 0.21486145 -0.35527256 -0.03915499 1.95576119 -0.17007764 0.89766943 -1.96336353 -0.37660047] accuracy : 0.9153 INFO:tensorflow:Restoring parameters from ./model/model.ckpt [[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]] [-0.61506635 0.45124692 0.21486145 -0.35527256 -0.03915499 1.95576119 -0.17007764 0.89766943 -1.96336353 -0.37660047] accuracy : 0.9153
###変数をrestoreするコード
このコードでは変数のrestoreが出来ていないようです。
python:restoremodel.py
1import tensorflow as tf 2from tensorflow.examples.tutorials.mnist import input_data 3mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 4 5x = tf.placeholder(tf.float32, [None, 784]) 6y_ = tf.placeholder(tf.float32, [None, 10]) 7 8W = tf.Variable(tf.zeros([784, 10])) 9b = tf.Variable(tf.zeros([10])) 10 11y = tf.nn.softmax(tf.matmul(x, W) + b) 12 13init = tf.global_variables_initializer() 14sess = tf.Session() 15sess.run(init) 16 17saver = tf.train.import_meta_graph('./model/model.ckpt.meta') 18model_path = './model/model.ckpt' 19 20ckpt = tf.train.get_checkpoint_state('./model/') 21print(ckpt) 22 23saver.restore(sess,model_path) 24print(sess.run(W)) 25print(sess.run(b)) 26correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 27accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 28acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}) 29print('accuracy : '+str(acc)); 30
実行時コンソール出力
こちらでもWとbの値を表示させていますがbの値が正しく読み込まれていないようです。
Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz model_checkpoint_path: "./model/model.ckpt" all_model_checkpoint_paths: "./model/model.ckpt" INFO:tensorflow:Restoring parameters from ./model/model.ckpt accuracy : 0.098 [[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
学習済みモデルの読み込みをし、正解率を表示させたいのですがsaver.restore(sess,model_path)の命令で変数が読み込まれると解釈しましたが、コンソールの出力を見るとbの値が読み込めていないように見受けられます。
そこでなんらかの方法で学習済みモデルを復元する方法を教えていただきたいです。
まだ勉強を始めて日が浅く、分からないことが多いため初歩的な質問で申し訳ないですが回答をいただけると幸いです。
回答1件
あなたの回答
tips
プレビュー