我将尝试将神经网络模型和设置随机权重为零的restore()函数分开。以下是模型代码:http://pastebin.com/TqN6kkeb(其正常工作)。
以下是该功能的代码:
问题如下: 1) 在这行代码中,它一直给我写 FailedPreconditionError (详见上面的 traceback):尝试使用未初始化的变量“Variable”。
无论如何。我已经阅读了这些主题:Prettytensor: Attempting to use uninitialized value和Update a subset of weights in TensorFlow(以及许多其他主题),但那里的建议并没有帮助我解决问题。只要我运行tf.global_variables_initializer(),我就不明白初始化有什么问题。
2)所有的权重似乎都被设置为零而不是一半,我不知道为什么。
请帮忙,我真的卡住了。
以下是该功能的代码:
from __future__ import print_function
import tensorflow as tf
tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES
import random
from LogReg import accuracy
from LogReg import W
from LogReg import x,y
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
def restore(model_file):
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph(model_file + ".meta")
new_saver.restore(sess, model_file)
with tf.variable_scope("foo", reuse=True):
temp_var = tf.get_variable("W")
size_2a = tf.get_variable("b")
size_2 = tf.shape(size_2a).eval()[0]
size_1 = tf.shape(temp_var).eval()[0]
ones_mask = tf.Variable(tf.ones([size_1, size_2]))
arg = random.sample(xrange(size_1), size_1/2)
index_num=tf.convert_to_tensor(arg, dtype=tf.int32)
print("om", ones_mask)
print("index", index_num)
print(W)
zeroes = tf.zeros([size_1/2, size_2])
update = tf.scatter_update(ones_mask, index_num, zeroes)
print(update)
assign_op = W.assign(tf.mul(W, update))
sess.run(update)
sess.run(assign_op)
init_op = tf.global_variables_initializer()
sess.run(init_op)
new_saver.save(sess, model_file)
print("Accuracy_new:", accuracy.eval({x: mnist.test.images, y:mnist.test.labels}))
restore('./MyModel2')
问题如下: 1) 在这行代码中,它一直给我写 FailedPreconditionError (详见上面的 traceback):尝试使用未初始化的变量“Variable”。
update = tf.scatter_update(ones_mask, index_num, zeroes)
无论如何。我已经阅读了这些主题:Prettytensor: Attempting to use uninitialized value和Update a subset of weights in TensorFlow(以及许多其他主题),但那里的建议并没有帮助我解决问题。只要我运行tf.global_variables_initializer(),我就不明白初始化有什么问题。
2)所有的权重似乎都被设置为零而不是一半,我不知道为什么。
请帮忙,我真的卡住了。