首页 文章

在TensorFlow中修改已恢复的CNN模型的权重和偏差

提问于
浏览
2

我最近开始使用TensorFlow(TF),我遇到了一个需要帮助的问题 . 基本上,我已经恢复了预先训练的模型,在重新测试其准确性之前,我需要修改其中一个层的权重和偏差 . 现在,我的问题如下:如何使用TF中的 assign 方法更改权重和偏差?是否可以在TF中修改已恢复建模的权重?

这是我的代码:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data # Imports the MINST dataset

# Data Set:
# ---------
mnist = input_data.read_data_sets("/home/frr/MNIST_data", one_hot=True)# An object where data is stored

ImVecDim = 784# The number of elements in a an image vector (flattening a 28x28 2D image)
NumOfClasses = 10

g = tf.get_default_graph()

with tf.Session() as sess:
  LoadMod = tf.train.import_meta_graph('simple_mnist.ckpt.meta')  # This object loads the model
  LoadMod.restore(sess, tf.train.latest_checkpoint('./'))# Loading weights and biases and other stuff to the model

  # ( 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 ) #

  # Testing the acuracy of the model:
  X = g.get_tensor_by_name('ImageIn:0')
  Y = g.get_tensor_by_name('LabelIn:0')
  KP = g.get_tensor_by_name('KeepProb:0')
  Accuracy = g.get_tensor_by_name('NetAccuracy:0')
  feed_dict = { X: mnist.test.images[:256], Y: mnist.test.labels[:256], KP: 1.0 }
  print( 'Model Accuracy = ' )
  print( sess.run( Accuracy, feed_dict ) )

3 回答

  • 0

    除了现有答案之外,还可以通过tf.assign函数执行张量更新 .

    v1 = sess.graph.get_tensor_by_name('v1:0')
    print(sess.run(v1))   # 1.0
    sess.run(tf.assign(v1, v1 + 1))
    print(sess.run(v1))   # 2.0
    
  • 1

    感谢所有回复的人 . 我只想把各个部分放在一起 . 这是帮助我完成我想要的代码:

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data # Imports the MINST dataset
    
    # Data Set:
    # ---------
    mnist = input_data.read_data_sets("/home/frr/MNIST_data", one_hot=True)# An object where data is stored
    
    ImVecDim = 784# The number of elements in a an image vector (flattening a 28x28 2D image)
    NumOfClasses = 10
    
    g = tf.get_default_graph()
    
    with tf.Session() as sess:
       LoadMod = tf.train.import_meta_graph('simple_mnist.ckpt.meta')  # This object loads the model
       LoadMod.restore(sess, tf.train.latest_checkpoint('./'))# Loading weights and biases and other stuff to the model
    
       wc1 = g.get_tensor_by_name('wc1:0')
       sess.run( tf.assign( wc1,tf.multiply(wc1,0) ) )# Setting the values of the variable 'wc1' in the model to zero.
    
       # Testing the acuracy of the model:
       X = g.get_tensor_by_name('ImageIn:0')
       Y = g.get_tensor_by_name('LabelIn:0')
       KP = g.get_tensor_by_name('KeepProb:0')
       Accuracy = g.get_tensor_by_name('NetAccuracy:0')
       feed_dict = { X: mnist.test.images[:256], Y: mnist.test.labels[:256], KP: 1.0 }
       print( 'Model Accuracy = ' )
       print( sess.run( Accuracy, feed_dict ) )
    
  • 0

    对的,这是可能的 . 加载元图后,您的权重和偏差已经加载 . 您需要找到它们的名称(请参阅list_variables函数),然后将它们分配给Python变量 .

    为此,请使用带有变量名称的 tf.get_variable . 您可能必须在变量范围上设置 reuse=True . 有关重用变量的更多详细信息,请参见this answer .

    将它们作为 weights 变量后,您可以调用 sess.run(weights.assign(...)) .

相关问题