2016-08-09 8 views
0

私はTensorflowで畳み込みニューラルネットワークを構築しています。名前付き変数を復元するTensorflow

def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    return tf.Variable(initial,name = 'weights') 

def bias_variable(shape): 
    initial = tf.constant(0.1, shape=shape) 
    return tf.Variable(initial, name = 'biases') 

def conv2d(x, W): 
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 

def max_pool_2x2(x): 
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 
         strides=[1, 2, 2, 1], padding='SAME') 

with tf.Graph().as_default(): 
    with tf.name_scope('convolution1'): 
     W_conv1 = weight_variable([5, 5, 1, 32]) 
     b_conv1 = bias_variable([32]) 

    x = tf.placeholder(tf.float32, shape=[None, 96*96]) 
    y_ = tf.placeholder(tf.float32, shape=[None, 30]) 
    x_image = tf.reshape(x, [-1,96,96,1]) 

    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 
    h_pool1 = max_pool_2x2(h_conv1) 

    with tf.name_scope('convolution2'): 
     W_conv2 = weight_variable([5, 5, 32, 64]) 
     b_conv2 = bias_variable([64]) 

    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 
    h_pool2 = max_pool_2x2(h_conv2) 

    with tf.name_scope('connected'): 
     W_fc1 = weight_variable([24 * 24 * 64, 1024]) 
     b_fc1 = bias_variable([1024]) 

    h_pool2_flat = tf.reshape(h_pool2, [-1, 24*24*64]) 
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 

    keep_prob = tf.placeholder(tf.float32) 
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 

    with tf.name_scope('output'): 
     W_fc2 = weight_variable([1024, 30]) 
     b_fc2 = bias_variable([30]) 

これ以降、いくつかの計算とトレーニングを行い、すべての変数を保存します。

今、私はこの問題は、私はその形状を取得しようとすると、彼らはまだ何を示していないvars_listの変数である 別のプログラム

PROGRAM 2スニペット

tf.reset_default_graph() 

x = tf.placeholder(tf.float32, shape=[None, 96*96]) 
x_image = tf.reshape(x, [-1,96,96,1]) 
y_ = tf.placeholder(tf.float32, shape=[None, 30]) 

with tf.name_scope('convolution1'): 
    W_conv1 = tf.Variable(-1.0, validate_shape = False, name = 'weights') 
    b_conv1 = tf.Variable(-1.0, validate_shape = False, name = 'biases') 

with tf.name_scope('convolution2'): 
    W_conv2 = tf.Variable(-1.0, validate_shape = False, name = 'weights') 
    b_conv2 = tf.Variable(-1.0, validate_shape = False, name = 'biases') 

with tf.name_scope('connected'): 
    W_fc1 = tf.Variable(-1.0, validate_shape = False, name = 'weights') 
    b_fc1 = tf.Variable(-1.0, validate_shape = False, name = 'biases') 

with tf.name_scope('output'): 
    W_fc2 = tf.Variable(-1.0, validate_shape = False, name = 'weights') 
    b_fc2 = tf.Variable(-1.0, validate_shape = False, name = 'biases') 

session = tf.Session() 
saver = tf.train.Saver() 
saver.restore(session, 'my-model-2000') 
vars_list = tf.get_collection(tf.GraphKeys.VARIABLES) 

h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 
h_pool1 = max_pool_2x2(h_conv1) 
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 
h_pool2 = max_pool_2x2(h_conv2) 
h_pool2_flat = tf.reshape(h_pool2, [-1, 24*24*64]) 
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 
y_convtry = tf.matmul(h_fc1, W_fc2) + b_fc2 
y_conv_alternate = 95.99*tf.ones_like(y_convtry) 
y_conv = tf.select(tf.greater(y_convtry, y_conv_alternate), y_conv_alternate, y_convtry) 

cost = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.select(tf.is_nan(y_), y_conv, y_) - y_conv), reduction_indices=[1]))) 

train_step = tf.train.AdamOptimizer(1e-4).minimize(cost,var_list = vars_list) 

で同じグラフ構造を再作成します実行中:

vars_list[i].eval(session = session) 

復元はw orking。

私の質問は、vars_list [i] .get_shape()が間違った答えを返すtf.shape(vars_list [i])が動作しないように見えない理由です。

私は

tf.AdamOptimizer.minimize(cost) //This internally call var.get_shape() and throws error 

答えて

1

を使用するときにtf.Variableを作成する上でvalidate_shape=Falseを設定すると、これが変数のデータの任意の形状を含むことができることTensorFlow伝え、あなたが(たとえば)ことができますので、これは復元する問題であり、任意形状のチェックポイントデータを変数に追加します。しかし、これはTensorFlowに、変数の形状に関する静的な情報を提供しません。たとえば、AdamOptimizer.minimize()は適切な形状のアキュムレータスロットを構築するために使用します。

最善の解決策は、ように、あなたが最初のプログラムで使用する変数を作成するために同じコードを再利用すなわち

with tf.name_scope('convolution1'): 
    W_conv1 = weight_variable([5, 5, 1, 32]) 
    b_conv1 = bias_variable([32]) 

...とすることです。これらの変数の初期化関数は決して実行されないので、このように書くための追加コストはかかりません。

+0

ええ、これはうまくいきますが、これは実際にモデルを復元するための厄介な方法です。私は彼らがより良い方法を思い付くことを願っています。 –

+0

'MetaGraphDef'は、モデル構造とチェックポイントを含むモデル全体のシリアライズフォーマットとして設計されています。これは、実行しようとすることをやりにくくする方法です。詳細については、[チュートリアル](https://www.tensorflow.org/versions/r0.10/how_tos/meta_graph/index.html)をご覧ください。 – mrry

関連する問題