How to save a tensorflow model (omitting the labels tensor) with no variables defined

时间:2017-12-18 08:26:13

标签: python machine-learning tensorflow neural-network deep-learning

My tensorflow model is defined as follows:

public partial class Form1 : Form
{
    string result;
    string fontInformation;

    private bool scaleFactorKnown = false;
    private SizeF scaleFactor;
    public Form1()
    {
        SizeChanged += Form1_SizeChanged;
        InitializeComponent();

        label1.Location = new Point(12, 36);
        label1.Size = new Size(100, 21);
        label1.Scale(scaleFactor);

        // 
        // textBox1
        // 
        textBox1.Location = new Point(133, 33);
        textBox1.Size = new Size(100, 21);
        textBox1.Scale(scaleFactor);

        // 
        // comboBox1
        // 

        comboBox1.Location = new Point(250, 33);
        comboBox1.Size = new Size(100, 21);
        comboBox1.Scale(scaleFactor);
        // button1
        // 
        button1.Location = new Point(365, 32);
        button1.Size = new Size(100, 21);
        button1.Scale(scaleFactor);
        // 
        // radioButton1
        // 

        radioButton1.Location = new Point(480, 32);
        radioButton1.Size = new Size(100, 21);
        radioButton1.Scale(scaleFactor);
        // 
        // checkBox1
        // 
        checkBox1.Location = new Point(586, 33);
        checkBox1.Size = new Size(100, 21);
        checkBox1.Scale(scaleFactor);
        // 
        // textBox2
        // 

        textBox2.Location = new Point(26, 102);
        textBox2.Size = new Size(660, 250);
        textBox2.Scale(scaleFactor);

    }

   private void Form1_SizeChanged(object sender, EventArgs e)
    {
        if (!scaleFactorKnown)
        {
            scaleFactor = AutoScaleFactor;
            scaleFactorKnown = true;
        }
        Size controlSize = new Size((int)(comboBox1.Width * scaleFactor.Width),
           (int)(comboBox1.Height * scaleFactor.Height)); //use for sizing


        //set bounds
        comboBox1.Bounds = new Rectangle(comboBox1.Location, controlSize);

    }
}

Now I want to save this model omitting tensor X = tf.placeholder(tf.float32, [None,training_set.shape[1]],name = 'X') Y = tf.placeholder(tf.float32,[None,training_labels.shape[1]], name = 'Y') A1 = tf.contrib.layers.fully_connected(X, num_outputs = 50, activation_fn = tf.nn.relu) A1 = tf.nn.dropout(A1, 0.8) A2 = tf.contrib.layers.fully_connected(A1, num_outputs = 2, activation_fn = None) cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = A2, labels = Y)) global_step = tf.Variable(0, trainable=False) start_learning_rate = 0.001 learning_rate = tf.train.exponential_decay(start_learning_rate, global_step, 200, 0.1, True ) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) (Y is the label tensor for training, Y is the actual input). Also while mentioning the output node while using X should I mention freeze_graph.py or is it saved with some other name?

1 个答案:

答案 0 :(得分:3)

虽然您尚未手动定义变量,但上面的代码段实际上包含15个可保存变量。您可以使用此内部张量流函数查看它们:

from tensorflow.python.ops.variables import _all_saveable_objects
for obj in _all_saveable_objects():
  print(obj)

对于上面的代码,它产生以下列表:

<tf.Variable 'fully_connected/weights:0' shape=(100, 50) dtype=float32_ref>
<tf.Variable 'fully_connected/biases:0' shape=(50,) dtype=float32_ref>
<tf.Variable 'fully_connected_1/weights:0' shape=(50, 2) dtype=float32_ref>
<tf.Variable 'fully_connected_1/biases:0' shape=(2,) dtype=float32_ref>
<tf.Variable 'Variable:0' shape=() dtype=int32_ref>
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'fully_connected/weights/Adam:0' shape=(100, 50) dtype=float32_ref>
<tf.Variable 'fully_connected/weights/Adam_1:0' shape=(100, 50) dtype=float32_ref>
<tf.Variable 'fully_connected/biases/Adam:0' shape=(50,) dtype=float32_ref>
<tf.Variable 'fully_connected/biases/Adam_1:0' shape=(50,) dtype=float32_ref>
<tf.Variable 'fully_connected_1/weights/Adam:0' shape=(50, 2) dtype=float32_ref>
<tf.Variable 'fully_connected_1/weights/Adam_1:0' shape=(50, 2) dtype=float32_ref>
<tf.Variable 'fully_connected_1/biases/Adam:0' shape=(2,) dtype=float32_ref>
<tf.Variable 'fully_connected_1/biases/Adam_1:0' shape=(2,) dtype=float32_ref>

来自fully_connected层的变量和来自Adam优化器的更多变量(参见this question)。请注意,此列表中没有XY个占位符,因此无需排除它们。当然,这些张量存在于元图中,但它们没有任何价值,因此无法保存。

_all_saveable_objects()列表是默认情况下tensorflow saver保存的,如果未明确提供变量。因此,您的主要问题的答案很简单:

saver = tf.train.Saver()  # all saveable objects!
with tf.Session() as sess:
  tf.global_variables_initializer().run()
  saver.save(sess, "...")

无法提供tf.contrib.layers.fully_connected功能的名称(因此,它保存为fully_connected_1/...),但我们鼓励您切换到tf.layers.dense,其中包含this name论点。无论如何,要了解为什么这是一个好主意,请查看this discussion和{{3}}。