gpt4 book ai didi

python - 尝试使用未初始化的值: Tensorflow

转载 作者:行者123 更新时间:2023-11-30 09:44:23 26 4
gpt4 key购买 nike

我正在从经过训练的模型恢复权重,并尝试使用 Tensorflow 中预训练模型的权重初始化另一个级别的某些层。我使用 session.runget_tensor_by_name 从预训练模型中获取权重值。我正在使用这些权重初始化 tf.Variable。这是我的代码:

checkpoint_dir = "check_point_"   #directory that contains .meta, .index, checkpoint files
checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)

graph = tf.Graph()
with graph.as_default():
sess = tf.Session()
with sess.as_default():
# Load the saved meta graph and restore variables
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
saver.restore(sess, checkpoint_file)

# load and save data from model
#f = open('weights.txt', 'ab')
var = tf.global_variables()
tf.initialize_all_variables().run()

for v in var:
print(v.name, end="\t")
print(v.shape)
gn = graph.get_tensor_by_name
sr = sess.run
v1 = sr(gn('Variable:0'))
v2 = sr(gn('Variable_1:0'))
v3 = sr(gn('Variable_2:0'))
v4 = sr(gn('Variable_3:0'))
v5 = sr(gn('Variable_4:0'))
v6 = sr(gn('Variable_5:0'))
v7 = sr(gn('Variable_6:0'))
v8 = sr(gn('Variable_7:0'))
print(type(v8))
conv1 = sess.run(gn('Variable:0'))

train_data_node = tf.placeholder(tf.float32, shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))

conv1_weights = tf.Variable(v1, dtype=tf.float32)
conv1_biases = tf.Variable(v2, dtype=tf.float32)
# conv2_weights = tf.Variable(gn('Variable_2:0'))
conv2_weights = tf.Variable(tf.truncated_normal(
[5, 5, 32, 64], stddev=0.1,
seed=SEED, dtype=tf.float32))
conv2_biases = tf.Variable(v4, dtype=tf.float32)
fc1_weights = tf.Variable(v5, dtype=tf.float32)
fc1_biases = tf.Variable(v6, dtype=tf.float32)
fc2_weights = tf.Variable(v7, dtype=tf.float32)
fc2_biases = tf.Variable(v8, dtype=tf.float32)
# fc2_biases = tf.Variable(tf.constant(
# 0.1, shape=[NUM_LABELS], dtype=tf.float32))

conv1 = tf.nn.conv2d(train_data_node, conv1_weights,
strides=[1, 1, 1, 1],
padding='SAME')
# Bias and rectified linear non-linearity.
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))

pool1 = tf.nn.max_pool(relu1,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME')
conv2 = tf.nn.conv2d(pool1,
conv2_weights,
strides=[1, 1, 1, 1],
padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
pool2 = tf.nn.max_pool(relu2,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME')
pool_shape = pool2.get_shape().as_list()
reshape = tf.reshape(
pool2,
[pool_shape[0], pool_shape[1] * pool_shape[2] * pool_shape[3]])
# Fully connected layer. Note that the '+' operation automatically
# broadcasts the biases.
hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)
print(sr(conv1_weights))
out = tf.matmul(hidden, fc2_weights) + fc2_biases

print(sess.run(out, feed_dict={train_data_node: numpy.random.randn(200, 28, 28, 1)}))

我收到此错误:'尝试使用未初始化的值 Variable_9
[[{{node _retval_Variable_9_0_0}} = _Retval[T=DT_FLOAT,index=0,_device="/job:localhost/replica:0/task:0/device:CPU:0"](Variable_9)]]'
我做错了什么?

最佳答案

1)如果我不得不猜测那是因为你在做

tf.initialize_all_variables().run()

如果我没记错的话,如果您从保护程序实例加载,则不需要执行initialize_all_variables

2) 如果您正在执行 initialize_all_variables 并定义自己的自定义变量,则需要在定义所有变量后调用 initialize_all_variables

关于python - 尝试使用未初始化的值: Tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54406098/

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