我在重新整理模型时遇到问题。我训练了模型并使用此代码保存了模型。我不太确定这是否是正确的方法,我将不胜感激。当我尝试还原模型时会发生问题。我只需要预测,就不会再接受过培训了。从模型中恢复参数需要花费很多时间。在我仅需要预测的前提下,如何改进模型保护程序或模型恢复程序以使其快速完成。
X = tf.placeholder(tf.float32, [None, 56, 56, 1]) Y_ = tf.placeholder(tf.float32, [None, 36]) L1 = 432 L2 = 72 L3 = 36 W1 = tf.Variable(tf.truncated_normal([3136, L1], stddev=0.1)) b1 = tf.Variable(tf.zeros([L1])) W2 = tf.Variable(tf.truncated_normal([L1, L2], stddev=0.1)) b2 = tf.Variable(tf.zeros([L2])) W3 = tf.Variable(tf.truncated_normal([L2, L3], stddev=0.1)) b3 = tf.Variable(tf.zeros([L3])) XX = tf.reshape(X, [-1, 3136]) Y1 = tf.nn.sigmoid(tf.matmul(XX, W1) + b1) Y1 = tf.nn.dropout(Y1, keep_prob=0.8) Y2 = tf.nn.sigmoid(tf.matmul(Y1, W2) + b2) Y2 = tf.nn.dropout(Y2, keep_prob=0.8) Ylogits = tf.matmul(Y2, W3) + b3 Y = tf.nn.softmax(Ylogits) cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=Ylogits, labels=Y_) cross_entropy = tf.reduce_mean(cross_entropy) * 100 correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cross_entropy) allweights = tf.concat([tf.reshape(W1, [-1]), tf.reshape(W2, [-1]), tf.reshape(W3, [-1])], 0) allbiases = tf.concat([tf.reshape(b1, [-1]), tf.reshape(b2, [-1]), tf.reshape(b3, [-1])], 0) init = tf.global_variables_initializer() saver = tf.train.Saver() def next_batch(x, y, batch, step): x_temp = x[cur_step:(step+batch)] y_temp = np.squeeze(y[step:(step + batch)]) return x_temp, y_temp with tf.Session() as sess: sess.run(init) cur_step = 0 for i in range(NUM_ITERS + 1): batch_X, batch_Y = next_batch(train_xx, train_yy, BATCH, cur_step) if i % DISPLAY_STEP == 0: acc_trn, loss_trn, w, b = sess.run([accuracy, cross_entropy, allweights, allbiases], feed_dict={X: batch_X, Y_: batch_Y}) acc_tst, loss_tst = sess.run([accuracy, cross_entropy], feed_dict={X: test_xx, Y_: test_yy}) sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y}) save_path = saver.save(sess, "abc/model")
恢复:
X = tf.placeholder(tf.float32, [None, 56, 56, 1]) Y_ = tf.placeholder(tf.float32, [None, 36]) L1 = 432 L2 = 72 L3 = 36 W1 = tf.Variable(tf.truncated_normal([3136, L1], stddev=0.1)) b1 = tf.Variable(tf.zeros([L1])) W2 = tf.Variable(tf.truncated_normal([L1, L2], stddev=0.1)) b2 = tf.Variable(tf.zeros([L2])) W3 = tf.Variable(tf.truncated_normal([L2, L3], stddev=0.1)) b3 = tf.Variable(tf.zeros([L3])) XX = tf.reshape(X, [-1, 3136]) Y1 = tf.nn.sigmoid(tf.matmul(XX, W1) + b1) Y1 = tf.nn.dropout(Y1, keep_prob=0.8) Y2 = tf.nn.sigmoid(tf.matmul(Y1, W2) + b2) Y2 = tf.nn.dropout(Y2, keep_prob=0.8) Ylogits = tf.matmul(Y2, W3) + b3 Y = tf.nn.softmax(Ylogits) with tf.Session() as sess: saver = tf.train.Saver() saver = tf.train.import_meta_graph('model.meta') saver.restore(sess, 'model')
编辑:也许使用Google Colab的GPU训练模型,然后将其还原到我的PC上这一事实很重要。
它的重复项:Tensorflow:如何保存/恢复模型?。
您对模型的保存是正确的,但不正确。您正在做的是尝试创建一个与保存的模型具有相同节点的新图,而不是从保存的图中还原它。以下步骤应解决有关如何还原模型的问题:
#Start with resetting the default graph tf.reset_default_graph() with tf.Session() as sess: # Nodes:Before loading the graph print([n.name for n in tf.get_default_graph().as_graph_def().node]) # Output is [] as no graph is loaded yet. # First let's load meta graph saver = tf.train.import_meta_graph("abc/model.meta") # Nodes:after loading the graph' print([n.name for n in tf.get_default_graph().as_graph_def().node]) # Output is [save/RestoreV2/shape_and_slices', 'save/RestoreV2/tensor_ ...] # The above step doesnt load the weights, can be checked by print(sess.run('Variable_1:0')) # Error: attempting to use uninitialized graph. #load the weights saver.restore(sess,tf.train.latest_checkpoint('./abc/')) print(sess.run('Variable_1:0')) # Output: [-2.80421402e-04 3.53254407e-04 ...]
现在我们已经加载并准备好节点,您需要访问其中的一些以进行推断。但是,由于节点的命名不正确,因此很难确定哪个节点是输入和输出。为了避免这种情况,name在使用以下name参数正确保存模型时,需要张量/运算:
name
X = tf.placeholder(tf.float32, [None, 56, 56, 1], name='X') Y = tf.identity(f.nn.softmax(Ylogits), name='logits').
加载图和权重后,可以在推理图中使用以下张量获得这些张量get_tensor_by_name:
get_tensor_by_name
with tf.Session() as sess: #Load the graph and weights as above .... graph = tf.get_default_graph() X_infer = graph.get_tensor_by_name('X:0') Y_infer = graph.get_tensor_by_name('logits:0') sess.run(Y_infer,{X_infer:new_input}