如何将一些变量保存到检查点以正确还原?

时间:2019-05-06 12:55:33

标签: tensorflow

我是Tensorflow的新手,并在保存和恢复方面感到挣扎。我有下面的代码,并尝试从一个点恢复它。我的模型中有一个检查点文件,但由于检查点文件中缺少某些键,因此恢复失败:

Key RMSprop/decay not found in checkpoint
     [[node save/RestoreV2 (defined at C:\Users\components\docs.py:199)  = 
RestoreV2[dtypes=[DT_FLOAT, DT_INT64, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., 
DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], 
_device="/job:localhost/replica:0/task:0/device:CPU:0"] 
(_arg_save/Const_0_0, save/RestoreV2/tensor_names, 
save/RestoreV2/shape_and_slices)]]

我还想知道如何将我的预测变量保存到chekpoint?我为此奋斗。我的预测变量是我的模型中的self.predictions=...

self.x1 = tf.placeholder(dtype=tf.int32, shape=[None, max_sequence_len],name="x1")
self.x2 = tf.placeholder(dtype=tf.int32, shape=[None, max_sequence_len],name="x2")
self.is_training = tf.placeholder(dtype=tf.bool)
self.labels = tf.placeholder(dtype=tf.int32, shape=[None, 1])
self.sentences_lengths = tf.placeholder(dtype=tf.int32, shape=[None])

self.debug = None

self.embedding_size = main_cfg['PARAMS'].getint('embedding_size')
self.learning_rate = main_cfg['TRAINING'].getfloat('learning_rate')

self.embedding_mat=embedding_mat

with tf.variable_scope('embeddings'):
    word_embeddings = tf.constant(self.embedding_mat, dtype=tf.float32, name="embedding")
    self.embedded_x1 = tf.nn.embedding_lookup(word_embeddings, self.x1)
    self.embedded_x2 = tf.nn.embedding_lookup(word_embeddings, self.x2)

   # word_embeddings = tf.get_variable('word_embeddings', [vocabulary_size, self.embedding_size])
    #self.embedded_x1 = tf.gather(word_embeddings, self.x1)
    #self.embedded_x2 = tf.gather(word_embeddings, self.x2)

with tf.variable_scope('siamese'):
    self.predictions = self.siamese_layer(max_sequence_len, model_cfg)

with tf.variable_scope('loss'):
    self.loss = loss_function(self.labels, self.predictions)
    self.opt = optimize(self.loss, self.learning_rate)

with tf.variable_scope('metrics'):
    self.temp_sim = tf.rint(self.predictions)
    self.correct_predictions = tf.equal(self.temp_sim, tf.to_float(self.labels))
    self.accuracy = tf.reduce_mean(tf.to_float(self.correct_predictions))

with tf.variable_scope('summary'):
    tf.summary.scalar("loss", self.loss)
    tf.summary.scalar("accuracy", self.accuracy)
    self.summary_op = tf.summary.merge_all()



def siamese_layer(self, sequence_len, model_cfg):
    num_filters = parse_list(model_cfg['PARAMS']['num_filters'])
    filter_sizes = parse_list(model_cfg['PARAMS']['filter_sizes'])

    embedded_x1=self.embedded_x1
    out1 = cnn_layers(self.embedded_x1,
                  sequence_len,
                  num_filters=num_filters,
                  filter_sizes=filter_sizes)

    out2 = cnn_layers(self.embedded_x2,
                  sequence_len,
                  num_filters=num_filters,
                  filter_sizes=filter_sizes,
                  reuse=True)

    out1 = dropout(out1, self.is_training)
    out2 = dropout(out2, self.is_training)

    return manhattan_similarity(out1, out2)

在范围损失下,优化功能为:

def optimize(loss, learning_rate=0.001):
   return tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)

作用域名称和张量/操作名称之间的检查点文件实际上有什么区别?我该如何命名我的var和ops,以便从检查点恢复我的预测变量?

0 个答案:

没有答案