python - How to save a specific variable in TensorFlow? -
i make network test save model. code:
import tensorflow tf import numpy np import time dimensions=100 batch_size=128 def add_layer(inputs, in_size, out_size, activation_function=none): weights = tf.variable(tf.random_normal([in_size, out_size])) biases = tf.variable(tf.zeros([1, out_size]) + 0.1) wx_plus_b = tf.matmul(inputs, weights) + biases if activation_function none: outputs = wx_plus_b else: outputs = activation_function(wx_plus_b) return outputs def f(batch_size,val,dims): = np.zeros(batch_size,dtype=np.int32)+val b = np.zeros((batch_size, dims)) b[np.arange(batch_size), a] = 1 return b xs = tf.placeholder(tf.float32, [none, dimensions]) ys = tf.placeholder(tf.float32, [none, 43]) l1 = add_layer(xs, dimensions, 64, activation_function=none) l2 = add_layer(l1, 64, 64, activation_function=tf.nn.sigmoid) prediction = add_layer(l2, 64, 43, activation_function=none) loss = tf.reduce_mean(tf.square(ys - prediction)) train_step = tf.train.adamoptimizer(0.003).minimize(loss) sess = tf.session() sess.run(tf.global_variables_initializer()) step in range(100): start_time = time.time() x = f(batch_size=batch_size,val=step,dims=dimensions) y = np.random.rand(batch_size,43) sess.run(train_step, feed_dict={xs:x, ys:y}) duration = time.time()-start_time if step%10 == 0: loss_value = sess.run(loss, feed_dict={xs: x, ys: y}) format_str = ('step %d,loss=%5.2f (%.1f examples/sec;%.3f sec/batch)') print(format_str %(step,loss_value,batch_size/duration,float(duration))) saver = tf.train.saver() save_path = saver.save(sess, "./save_net.ckpt") sess.close()
it save variables "./save_net.ckpt".
but want save weight , bias of l1 layer. how it?
and how extract these variables in tensorflow?
you should take @ tensorflow documentation. variables
especially part choosing variables save , restore
in case
you should pass name functions creates weights , biases declaration be
weights = tf.variable(tf.random_normal([in_size, out_size]), name=weights_name) biases = tf.variable(tf.zeros([1, out_size]) + 0.1, name = biases_name)
and then
saver = tf.train.saver({"l1_wieghts": "l1_weights_name", "l1_biases": "l1_biases_name", "l2_weights":"l2_weights_names", "l2_biases":"l2_biases_name"})
Comments
Post a Comment