I have recently started using TensorFlow (TF), and I have come across a problem that I need some help with. Basically, I’ve restored a pre-trained model, and I need to modify the weights and biases of one of its layers before I retest its accuracy. Now, my problem is the following:
how can I change the weights and biases using the assign
method in TF? Is modifying the weights of a restored modeled even possible in TF?
Here is my code:
JavaScript
x
27
27
1
import tensorflow as tf
2
from tensorflow.examples.tutorials.mnist import input_data # Imports the MINST dataset
3
4
# Data Set:
5
# ---------
6
mnist = input_data.read_data_sets("/home/frr/MNIST_data", one_hot=True)# An object where data is stored
7
8
ImVecDim = 784# The number of elements in a an image vector (flattening a 28x28 2D image)
9
NumOfClasses = 10
10
11
g = tf.get_default_graph()
12
13
with tf.Session() as sess:
14
LoadMod = tf.train.import_meta_graph('simple_mnist.ckpt.meta') # This object loads the model
15
LoadMod.restore(sess, tf.train.latest_checkpoint('./'))# Loading weights and biases and other stuff to the model
16
17
# ( Here I'd like to modify the weights and biases of layer 1, set them to one for example, before I go ahead and test the accuracy ) #
18
19
# Testing the acuracy of the model:
20
X = g.get_tensor_by_name('ImageIn:0')
21
Y = g.get_tensor_by_name('LabelIn:0')
22
KP = g.get_tensor_by_name('KeepProb:0')
23
Accuracy = g.get_tensor_by_name('NetAccuracy:0')
24
feed_dict = { X: mnist.test.images[:256], Y: mnist.test.labels[:256], KP: 1.0 }
25
print( 'Model Accuracy = ' )
26
print( sess.run( Accuracy, feed_dict ) )
27
Advertisement
Answer
In addition to an existing answer, tensor update can be performed via tf.assign
function.
JavaScript
1
5
1
v1 = sess.graph.get_tensor_by_name('v1:0')
2
print(sess.run(v1)) # 1.0
3
sess.run(tf.assign(v1, v1 + 1))
4
print(sess.run(v1)) # 2.0
5