Tensorflow模型负载和预测

时间:2020-01-22 13:37:18

标签: python tensorflow

我正在尝试针对5种不同的拆分数据训练TensorFlow模型。对于每个拆分,我都保存模型,以后再将其还原以进行预测,但是它不会获得应提供的实际预测和准确性(对于以后的还原模型)。 我正在使用一个循环进行训练,每次我从不同的分割中加载TensorFlow并使用不同的名称保存每个模型时。 tf.get_default_graph()是否有问题? 我读了文件。提到它不是线程安全的。

这是我的代码:

将my_cnn_tensorflow导入为cnn

    for k_splits in range(K, K + 1):
        #kf = KFold(k_splits, shuffle=False, random_state=42)
        kf = RepeatedKFold(k_splits, n_repeats=1, random_state=42)
        for train_index, test_index in kf.split(X, Y_P2):
            d_range = int(len(X_train) * TL_percentage)
            k_run += 1
            Dx_train, Dx_test = Dx[train_index], Dx[test_index]
            y_train, y_test = Y_P2[train_index], Y_P2[test_index]
            model = cnn.DLSpMVModel(Dx_train, y_train, Dx_test, y_test, classes, TL_percentage,platform1_name, k_run)
            model.training()
            y_pred = model.testing()
            accuracy = accuracy_score(y_test, y_pred) * 100

my_cnn_tensorflow.py

class DLSpMVModel(object):
  def __init__(self, train_data, y_train, test_data, y_test, classes, TL_percentage, platform,
             ksplit):
    self.RES = 0
    self.mean = 0
    self.std = 1
    self.train = load_data(train_data, y_train, classes)
    self.test = load_data(test_data, y_test, classes)

  def training(self):
    print("Model is in training mode")
    assert self.train is not None and self.test is not None, "data not loaded"

    with tf.name_scope('upper'):
        x, y_, h_fc1_upper = single_net(self.RES, self.classes)

    with tf.name_scope('lower'):
        x2, y2_, h_fc1_lower = single_net(self.RES, self.classes)

    h_fc1 = tf.concat([h_fc1_upper, h_fc1_lower], axis=1)
    # [-1, 512 * 2]

    with tf.name_scope('dropout'):
        keep_prob = tf.placeholder(tf.float32, name='keep_prob')
        h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    with tf.name_scope('out'):
        W_fc2 = weight_variable([512 * 2, self.classes])
        b_fc2 = bias_variable([self.classes])
        y_conv = tf.add(tf.matmul(h_fc1_drop, W_fc2), b_fc2, name='y_conv_restore')

    with tf.name_scope('cross_entropy'):
        cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=y_,
                                                    logits=y_conv))  # takes unnormalized output

    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
        correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32),
                                  name='acc_to_restore')
        tf.summary.scalar('accuracy', accuracy)

    merged = tf.summary.merge_all()

    saver = tf.train.Saver()  # traditional saving api

    # train the model
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(self.STEPS):
            batch = self.train.next_batch(50)
            if i % 100 == 0:
                train_accuracy = sess.run(accuracy,
                                          feed_dict={
                                              x: batch[0][:, 0, :, :],
                                              y_: batch[1],
                                              x2: batch[0][:, 1, :, :],
                                              y2_: batch[1],
                                              keep_prob: 1.0
                                          })
                print('step %d, training accuracy %g' % (i, train_accuracy))
            else:
                _ = sess.run(train_step,
                             feed_dict={
                                 x: batch[0][:, 0, :, :],
                                 y_: batch[1],
                                 x2: batch[0][:, 1, :, :],
                                 y2_: batch[1],
                                 keep_prob: 0.5
                             })
        # test
        print('test accuracy %g' % accuracy.eval(
            feed_dict={
                x: self.test.images[:, 0, :, :],
                y_: self.test.labels,
                x2: self.test.images[:, 1, :, :],
                y2_: self.test.labels,
                keep_prob: 1.0
            }))

        # save model and checkpoint
        save_path = saver.save(
            sess,
            os.path.join(
                ROOTDIR,
                "spmv_models/model-itr-{}-{}-{}.ckpt".format(self.ksplit, self.platform,
                                                             self.STEPS)))
        print("Model saved in file %s" % save_path)


  def testing(self):
    """ restore model and checkpoint
    [description]
    """
    print("Model is in testing mode")
    assert self.test is not None, "data not loaded"
    tf.reset_default_graph()  # the graph is empty now, must build graph before restore value
    with tf.Session() as sess:
        # retore graph
        saver = tf.train.import_meta_graph(
            os.path.join(
                ROOTDIR,
                "spmv_models/model-itr-{}-{}-{}.ckpt.meta".format(self.ksplit, self.platform,
                                                                  self.STEPS)))
        # the current graph can be explored by
        graph = tf.get_default_graph()
        # restore value
        saver.restore(sess, tf.train.latest_checkpoint(os.path.join(ROOTDIR, 'spmv_models')))
        print("Model restored")
        x = graph.get_tensor_by_name("upper/input/x:0")
        y = graph.get_tensor_by_name("upper/input/y:0")
        x2 = graph.get_tensor_by_name("lower/input/x:0")
        y2_ = graph.get_tensor_by_name("lower/input/y:0")
        y_conv = graph.get_tensor_by_name('out/y_conv_restore:0')
        keep_prob = graph.get_tensor_by_name("dropout/keep_prob:0")
        # for tensor, use get_tensor_by_name()
        # for operation, use get_operation_by_name()
        # NOTE: Tensor names must be of the form "<op_name>:<output_index>"
        acc = graph.get_tensor_by_name('train/acc_to_restore:0')
        # test
        print("-------------------------------------------------------")
        print('Test accuracy %g' % sess.run(acc,
                                            feed_dict={
                                                x: self.test.images[:, 0, :, :],
                                                y: self.test.labels,
                                                x2: self.test.images[:, 1, :, :],
                                                y2_: self.test.labels,
                                                keep_prob: 1.0
                                            }))

        pred = sess.run(tf.argmax(y_conv, axis=1),
                        feed_dict={
                            x: self.test.images[:, 0, :, :],
                            x2: self.test.images[:, 1, :, :],
                            keep_prob: 1.0
                        })

        return pred

0 个答案:

没有答案