在张量流中恢复模型时发生FailedPreconditionError

时间:2019-04-29 00:44:26

标签: python-3.x tensorflow

成功训练模型后,我无法恢复它。该方法以前可以工作,但是以某种方式却给出了错误“ FailedPreconditionError”。 Tensorflow在保存时应该已经存储了所有变量,所以我不确定错误的原因。

我在训练期间保存了模型,并尝试稍后使用保存和还原选项进行还原。

TextView u1Progress;
Globals g = Globals.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    u1Progress = (TextView) findViewById(R.id.u1Progress);
    counter = 0;
    u1t1 = g.getu1t1();
    u1t2 = g.getu1t2();
    u1t3 = g.getu1t3();
    u1t4 = g.getu1t4();
    if (u1t1 = true) {
        counter += 1;
    } if (u1t2 = true) {
        counter += 1;
    } if (u1t3 = true) {
        counter += 1;
    } if (u1t4 = true) {
        counter += 1;
    }
    if (counter == 4) {
        u1Progress.setText("Completed!");
    } else {
        u1Progress.setText(counter + "/4");
    }
}

这是训练时的输出


validation_accuracy_track = []
train_accuracy_track = []
connection_probability_track = []
number_of_ex = X_train.shape[0]
total_steps_for_one_pass = number_of_ex//BATCH_SIZE + 1

print_every = 10
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    num_examples = len(X_train)
    best_accuracy_valid = 0
    print("Training...")
    print()
    for i in range(EPOCHS):
        X_train, y_train = shuffle(X_train, y_train)
        for step in range(0, total_steps_for_one_pass):        
          if step>=number_of_ex//BATCH_SIZE:
            batch_x, batch_y = X_train[step*BATCH_SIZE:,:],y_train[step*BATCH_SIZE:]
            step = 0
          else:
            start = step*BATCH_SIZE
            finish = (step+1)*BATCH_SIZE
            batch_x, batch_y = X_train[step:finish,:],y_train[step:finish]
          tr_op  = sess.run([training_operation], feed_dict={x: batch_x, y: batch_y, is_testing : False})
        prob = sess.run(new_prob)
        if i%print_every == 0:
          tr_accuracy = sess.run(accuracy*100, feed_dict={x: X_train,y:y_train, is_testing: True})  # evaluate(X_train, y_train)
          print("Train Accuracy = {:.5f}".format(tr_accuracy))
          validation_accuracy = sess.run(accuracy*100, feed_dict={x: validation_data,y:validation_label_one_hot, is_testing: True}) #evaluate(X_validation, y_validation)
          validation_accuracy_track.append(validation_accuracy)
          train_accuracy_track.append(tr_accuracy)
          connection_probability_track.append(prob)
          print("EPOCH {} ...".format(i+1))
          print("Validation Accuracy = {:.5f}".format(validation_accuracy))
          print(prob)
          print()
          if (validation_accuracy >= best_accuracy_valid):
            best_accuracy_valid = validation_accuracy
            saver.save(sess, './PendigitSGDBased')

    saver.save(sess, './lenet')
    print("Model saved")

还原时我会得到这个

Training...

Train Accuracy = 99.94996
EPOCH 1 ...
Validation Accuracy = 99.26617
0.83325

Train Accuracy = 99.93328
EPOCH 11 ...
Validation Accuracy = 99.13275
0.1345745

Train Accuracy = 99.94996
EPOCH 21 ...
Validation Accuracy = 99.53302
0.021734525

Train Accuracy = 99.96664
EPOCH 31 ...
Validation Accuracy = 99.59973
0.0035102463

Train Accuracy = 99.96664
EPOCH 41 ...
Validation Accuracy = 99.59973
0.0005669242

Model saved

这是错误

with tf.Session() as sess:
  saver.restore(sess, './lenet')
  sess.run(accuracy*100, feed_dict={x: validation_data,y:validation_label_one_hot, is_testing: True})

这可能是什么原因-为什么训练和测试的行为发生变化?任何帮助将不胜感激。

0 个答案:

没有答案