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

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

checkpoint_dir = "check_point_"   #directory that contains .meta, .index, checkpoint filescheckpoint_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()

如果我没记错的话,如果你从一个saver实例加载,你不需要执行 initialize_all_variables

2) 如果你执行了 initialize_all_variables 并且你定义了自己的自定义变量,你需要在定义所有变量之后调用 initialize_all_variables

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注