トレーニングで取得した値をTensorflowモデルから読み取ろうとしていますが、私は重みの初期値のみを取得しています。 https://gist.github.com/saitodev/8532cf9e94a9490f75a9bce678751aecのサンプルコードを使って、Wとbの値を出力するコードを追加しています。 私はバイアス(b)の値を取得しますが、ウェイト(W)はすべて、学習モデルが実際に働いているため(92%の精度)、真ではないゼロになります。私は体重についてはtrainable = Falseを設定するように練習しましたが、Wは実際に働くために更新する必要があるという意味を学ぶために停止しました。学習した体重の価値をどのように読み取るべきですか?私のアプローチの問題は何ですか?学習されたテンソルフローモデルで訓練された荷重を読み取ることができません
コード:
from __future__ import print_function
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
max_steps = 1000
for step in range(max_steps):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
if (step % 100) == 0:
print(step, sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
print(max_steps, sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
print("W=")
print(sess.run(W))
print("b=")
print(sess.run(b))
print(max_steps, sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
結果:あなたのコードで
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
0 0.3348
100 0.8918
200 0.9037
300 0.906
400 0.9098
500 0.9137
600 0.9168
700 0.9147
800 0.9134
900 0.9193
1000 0.9193
W=
[[ 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.]]
b=
[-0.38804549 0.35967571 0.09746896 -0.28238639 0.03597458 1.31636047
-0.11613782 0.64165515 -1.42244864 -0.24211763]
1000 0.9193
モデルを保存または復元する必要はありません。私は、モデルが訓練された後、変数の値をチェックしたいだけです。 – Glubin
申し訳ありませんが、私はあなたを誤解しています。あなたのコードのエラーは 'W = tf.Variable(tf.zeros([784、10]))'です。 'W'はゼロで初期化すべきではありません。グラデーションディセントメソッドは 'W'を更新できません。この 'W = tf.Variable(tf.truncated_normal([784、10]、stddev = 1e-4)、name =" weights ")' – dxf