【问题标题】:Tensorflow restoring named variablesTensorFlow 恢复命名变量
【发布时间】:2016-08-09 08:29:09
【问题描述】:

我正在使用 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])

在此之后我进行一些计算和训练并保存所有变量。

现在我在另一个程序中重新创建相同的图形结构

程序 2 sn-p

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 中的变量,当我尝试获取它们的形状时,它们仍然没有显示, 但正在运行:

vars_list[i].eval(session = session) 

给出了正确的答案,因此恢复工作正常。

我的问题是为什么 vars_list[i].get_shape() 没有给出错误的答案 tf.shape(vars_list[i]) 似乎也不起作用。

这是一个问题,因为当我使用

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

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案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])
    

    ...等等。这些变量的初始化函数将永远不会运行,因此以这种方式编写它不会产生额外的成本。

    【讨论】:

    • 是的,这行得通,但是这种恢复模型的方法确实很麻烦,希望他们能想出更好的方法。
    • MetaGraphDef 被设计为整个模型的序列化格式,包括模型结构和检查点,它是一种不那么繁琐的方式来做你想做的事情。查看tutorial了解更多详情。
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2016-04-16
    • 2017-09-28
    • 2017-06-17
    • 2016-06-14
    • 2023-03-19
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多