[英]Attempting to use uninitialized value: Tensorflow
我正在從訓練有素的模型中恢復權重,並嘗試使用Tensorflow中預先訓練的模型的權重來初始化另一個級別的某些層。 我正在使用session.run
和get_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)}))
我收到此錯誤: 'Attempting to use uninitialized value 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.