一.保存模型
所谓的模型保存,也就是冻结(freeze)模型,将该模型的图结构和该模型的权重固化到一起。
二.加载模型
在恢复模型的时候,通过get_tensor_by_name获得模型中的变量,然后对变量进行赋值。
三.代码实例
Demo1:单纯地训练,不生成模型
#-*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
with tf.variable_scope('Placeholder'):
inputs_placeholder = tf.placeholder(tf.float32,name = 'inputs_placeholder',shape = [None,10])
labels_placeholder = tf.placeholder(tf.float32,name = 'labels_placeholder',shape = [None,1])
with tf.variable_scope('NN'):
W1 = tf.get_variable('W1',shape = [10,1],initializer = tf.random_normal_initializer(stddev = 1e-1))
b1 = tf.get_variable('b1',shape = [1],initializer = tf.constant_initializer(0.1))
W2 = tf.get_variable('W2',shape = [10,1],initializer = tf.random_normal_initializer(stddev = 1e-1))
b2 = tf.get_variable('b2',shape = [1],initializer = tf.constant_initializer(0.1))
a1 = tf.nn.relu(tf.matmul(inputs_placeholder,W1) + b1)
a2 = tf.nn.relu(tf.matmul(inputs_placeholder,W2) + b2)
y = tf.div(tf.add(a1,a2),2)
with tf.variable_scope('Loss'):
loss = tf.red