我想我尝试运行相同的代码以重新创建模型结构,但出现错误。我认为这可能与以下事实有关:此处描述的代码未使用命名变量:http :
//nasdag.github.io/blog/2016/01/19/classifying-bees-with-google-
tensorflow/
def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial)def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial)
所以我做了这个实验。我编写了两个版本的代码(带有和不带有命名变量)来保存模型,而代码则还原了模型。
tensor_save_named_vars.py :
import tensorflow as tf# Create some variables.v1 = tf.Variable(1, name="v1")v2 = tf.Variable(2, name="v2")# Add an op to initialize the variables.init_op = tf.initialize_all_variables()# Add ops to save and restore all the variables.saver = tf.train.Saver()# Later, launch the model, initialize the variables, do some work, save the# variables to disk.with tf.Session() as sess: sess.run(init_op) print "v1 = ", v1.eval() print "v2 = ", v2.eval() # Save the variables to disk. save_path = saver.save(sess, "/tmp/model.ckpt") print "Model saved in file: ", save_path
tensor_save_not_named_vars.py:
import tensorflow as tf# Create some variables.v1 = tf.Variable(1)v2 = tf.Variable(2)# Add an op to initialize the variables.init_op = tf.initialize_all_variables()# Add ops to save and restore all the variables.saver = tf.train.Saver()# Later, launch the model, initialize the variables, do some work, save the# variables to disk.with tf.Session() as sess: sess.run(init_op) print "v1 = ", v1.eval() print "v2 = ", v2.eval() # Save the variables to disk. save_path = saver.save(sess, "/tmp/model.ckpt") print "Model saved in file: ", save_path
tensor_restore.py:
import tensorflow as tf# Create some variables.v1 = tf.Variable(0, name="v1")v2 = tf.Variable(0, name="v2")# Add ops to save and restore all the variables.saver = tf.train.Saver()# Later, launch the model, use the saver to restore variables from disk, and# do some work with the model.with tf.Session() as sess: # Restore variables from disk. saver.restore(sess, "/tmp/model.ckpt") print "Model restored." print "v1 = ", v1.eval() print "v2 = ", v2.eval()
这是我执行此代码后得到的:
$ python tensor_save_named_vars.pyI tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4v1 = 1v2 = 2Model saved in file: /tmp/model.ckpt$ python tensor_restore.pyI tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4Model restored.v1 = 1v2 = 2$ python tensor_save_not_named_vars.pyI tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4v1 = 1v2 = 2Model saved in file: /tmp/model.ckpt$ python tensor_restore.py I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4W tensorflow/core/common_runtime/executor.cc:1076] 0x7ff953881e40 Compute status: Not found: Tensor name "v2" not found in checkpoint files /tmp/model.ckpt [[Node: save/restore_slice_1 = RestoreSlice[dt=DT_INT32, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_1/tensor_name, save/restore_slice_1/shape_and_slice)]]W tensorflow/core/common_runtime/executor.cc:1076] 0x7ff953881e40 Compute status: Not found: Tensor name "v1" not found in checkpoint files /tmp/model.ckpt [[Node: save/restore_slice = RestoreSlice[dt=DT_INT32, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice/tensor_name, save/restore_slice/shape_and_slice)]]Traceback (most recent call last): File "tensor_restore.py", line 14, in <module> saver.restore(sess, "/tmp/model.ckpt") File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 891, in restore sess.run([self._restore_op_name], {self._filename_tensor_name: save_path}) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 368, in run results = self._do_run(target_list, unique_fetch_targets, feed_dict_string) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 444, in _do_run e.pre)tensorflow.python.framework.errors.NotFoundError: Tensor name "v2" not found in checkpoint files /tmp/model.ckpt [[Node: save/restore_slice_1 = RestoreSlice[dt=DT_INT32, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_1/tensor_name, save/restore_slice_1/shape_and_slice)]]Caused by op u'save/restore_slice_1', defined at: File "tensor_restore.py", line 8, in <module> saver = tf.train.Saver() File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 713, in __init__ restore_sequentially=restore_sequentially) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 432, in build filename_tensor, vars_to_save, restore_sequentially, reshape) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 191, in _AddRestoreOps values = self.restore_op(filename_tensor, vs, preferred_shard) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 106, in restore_op preferred_shard=preferred_shard) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/io_ops.py", line 189, in _restore_slice preferred_shard, name=name) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_io_ops.py", line 271, in _restore_slice preferred_shard=preferred_shard, name=name) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 664, in apply_op op_def=op_def) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1834, in create_op original_op=self._default_original_op, op_def=op_def) File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1043, in __init__ self._traceback = _extract_stack()
因此,也许可以将原始代码(请参见上面的外部链接)修改为以下形式:
def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) weight_var = tf.Variable(initial, name="weight_var") return weight_vardef bias_variable(shape): initial = tf.constant(0.1, shape=shape) bias_var = tf.Variable(initial, name="bias_var") return bias_var
但是,我有一个问题:恢复weight_var和bias_var变量是否足以实现预测?我在具有GPU的功能强大的机器上进行了培训,我想将模型复制到功能不那么强大的没有GPU的计算机上以运行预测。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)