gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:36:45 26 4
gpt4 key购买 nike

我最近开始使用 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 ) )

最佳答案

除了现有答案外,张量更新还可以通过 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

关于python - 在 TensorFlow 中修改恢复的 CNN 模型的权重和偏差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47723873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com