2017-02-14 91 views
3

我正在做一個通過Tensorflow增強(4層DNN到5層DNN)的例子。我正在保存會話並在TF中恢復,因爲TF tute中有一個簡短的段落: '例如,您可能已經訓練了一個4層的神經網絡,現在您想要訓練5層的新模型,將來自先前訓練模型的4層的參數恢復到新模型的前4層。',其中張量流通啓動於https://www.tensorflow.org/how_tos/variables/恢復Tensorflow中新模型子集的變量?

但是,我發現當檢查點保存4層參數時,沒有人詢問如何使用「恢復」,但我們需要將它放入5層,引發紅旗。

使這在實際的代碼,我做了

with tf.name_scope('fcl1'): 
    hidden_1 = fully_connected_layer(inputs, train_data.inputs.shape[1], num_hidden)    
with tf.name_scope('fcl2'): 
    hidden_2 = fully_connected_layer(hidden_1, num_hidden, num_hidden)     
with tf.name_scope('fclf'): 
    hidden_final = fully_connected_layer(hidden_2, num_hidden, num_hidden)  
with tf.name_scope('outputl'): 
    outputs = fully_connected_layer(hidden_final, num_hidden, train_data.num_classes, tf.identity) 
    outputs = tf.nn.softmax(outputs) 
with tf.name_scope('boosting'): 
    boosts = fully_connected_layer(outputs, train_data.num_classes, train_data.num_classes, tf.identity) 

其中內部變量(或稱爲)「FCL1」 - 這樣我有「FCL1 /變量」和「FCL1/Variable_1」的重量和偏見 - 'fcl2','fclf'和'outputl'由saver.save()存儲在腳本中,沒有'boosting'圖層。然而,正如我們現在已經「助推」層,saver.restore(SESS,「saved_models/model_list.ckpt」)不工作作爲

NotFoundError: Key boosting/Variable_1 not found in checkpoint 

我真的很希望聽到這個問題。謝謝。下面的代碼是我陷入困境的代碼的主要部分。

def fully_connected_layer(inputs, input_dim, output_dim, nonlinearity=tf.nn.relu): 
    weights = tf.Variable(
     tf.truncated_normal(
      [input_dim, output_dim], stddev=2./(input_dim + output_dim)**0.5), 
     'weights') 
    biases = tf.Variable(tf.zeros([output_dim]), 'biases') 
    outputs = nonlinearity(tf.matmul(inputs, weights) + biases)  

    return outputs 

inputs = tf.placeholder(tf.float32, [None, train_data.inputs.shape[1]], 'inputs') 
targets = tf.placeholder(tf.float32, [None, train_data.num_classes], 'targets') 

with tf.name_scope('fcl1'): 
    hidden_1 = fully_connected_layer(inputs, train_data.inputs.shape[1], num_hidden)    
with tf.name_scope('fcl2'): 
    hidden_2 = fully_connected_layer(hidden_1, num_hidden, num_hidden)     
with tf.name_scope('fclf'): 
    hidden_final = fully_connected_layer(hidden_2, num_hidden, num_hidden)  
with tf.name_scope('outputl'): 
    outputs = fully_connected_layer(hidden_final, num_hidden, train_data.num_classes, tf.identity) 

with tf.name_scope('error'):  
    error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(outputs, targets)) 
with tf.name_scope('accuracy'): 
    accuracy = tf.reduce_mean(tf.cast(
     tf.equal(tf.argmax(outputs, 1), tf.argmax(targets, 1)), 
     tf.float32)) 
with tf.name_scope('train'): 
    train_step = tf.train.AdamOptimizer().minimize(error) 

init = tf.global_variables_initializer() 
saver = tf.train.Saver() 

with tf.Session() as sess: 
    sess.run(init) 
    saver.restore(sess, "saved_models/model.ckpt") 
    print("Model restored") 

    print("Optimization Starts!") 
    for e in range(training_epochs): 
     ... 

    #Save model - save session   
    save_path = saver.save(sess, "saved_models/model.ckpt") 
    ### I once saved the variables using var_list, but didn't work as well... 
    print("Model saved in file: %s" % save_path) 

爲了清楚起見,檢查點文件具有

fcl1/Variable:0 

fcl1/Variable_1:0 

fcl2/Variable:0 

fcl2/Variable_1:0 

fclf/Variable:0 

fclf/Variable_1:0 

outputl/Variable:0 

outputl/Variable_1:0 

由於原來的4層模型不具有 '升壓' 層。

+0

可以恢復使用'tf.Saver' [構造]的'var_list'參數(https://www.tensorflow.org/api_docs/python模型/ state_ops/saving_and_restoring_variables)。 之後您將負責正確初始化第5層。 – drpng

回答

5

在這種情況下,從檢查點讀取提升值並不正確,我認爲這不是您想要做的。很明顯,你會遇到錯誤,因爲在恢復變量時,首先要捕捉模型中所有變量的列表,然後在檢查點中查找相應的變量,但沒有變量。

您可以通過定義模型變量的子集來僅恢復部分模型。例如,你可以使用tf.slim庫來完成。獲取模型中的變量列表:

variables = slim.get_variables_to_restore() 

現在變量是張量列表,但是對於每個元素,您可以訪問其名稱屬性。使用它你可以指定你只想恢復增強以外的圖層,例如:

variables_to_restore = [v for v in variables if v.name.split('/')[0]!='boosting'] 
model_path = 'your/model/path' 

saver = tf.train.Saver(variables_to_restore) 

with tf.Session() as sess: 
    saver.restore(sess, model_path) 

這樣你就可以恢復4層。理論上,你可以嘗試通過創建另一個服務器來捕獲變量中某個變量的值,這些服務器只會在變量列表中提升並從檢查點重命名所選變量,但我真的不認爲這是您需要的。

由於這是您的模型的自定義層,並且您沒有任何位置的此變量,只需在工作流中初始化它,而不是嘗試導入它。你可以通過這個參數,例如做,而調用函數fully_connected:

weights_initializer = slim.variance_scaling_initializer() 

你需要檢查自己的細節,雖然,因爲我不知道你的進口是什麼,您使用的這些在這裏的功能。

一般來說,我建議你看看苗條庫,這將使你更容易爲圖層定義模型和範圍(而不是通過定義它,而可以調用範圍參數功能)。它看起來像與苗條:

boost = slim.fully_connected(input, number_of_outputs, activation_fn=None, scope='boosting', weights_initializer=slim.variance_scaling_initializer()) 
+0

謝謝。有用!其實,我的情況並不需要「苗條」。 – sdr2002