如何为我的张量流模型找到输入和输出张量?

时间:2020-05-18 17:03:47

标签: python tensorflow protocol-buffers tensorflow-serving

我正在尝试将保存的Tensorflow模型转换为.pb格式。但是,当我在线看到可用的方法时,它们在sess.run中定义了一个输入张量和输出张量,但是我没有。有什么办法可以找到输入和输出张量吗?另外,为什么输出张量不像其他人那样通过sess.run?这是预测任务还是分类任务? 我的模型如下所示:

with tf.Session(graph=cnn_graph) as sess:
# load model
loader = tf.train.import_meta_graph(model_save_path + '.meta')
loader.restore(sess, model_save_path)

# obtain tensors
x = cnn_graph.get_tensor_by_name('x:0')
y = cnn_graph.get_tensor_by_name('y:0')
keep_prob = cnn_graph.get_tensor_by_name('keep_prob:0')
logits = cnn_graph.get_tensor_by_name('logits:0')

# use model to predict from test features
# loop over test batches
for batch_index in range(test_batches.shape[0]):
    test_features_batch, test_ids_batch = read_batch(test_batches[batch_index])
    predictions = sess.run(tf.nn.softmax(logits), feed_dict={
        x: test_features_batch,
        keep_prob: 1.
    })

    for test_index in range(len(test_ids_batch)):
        # save predictions to file
        prediction_file.write(test_ids_batch[test_index] + ',' + str(predictions[test_index,1]) + 
    '\n')
        # print out predictions
        print('ID: ' + test_ids_batch[test_index] + ', Cancer probability: ' + 
      str(predictions[test_index,1]))

    prediction_file.close()

0 个答案:

没有答案
相关问题