簡體   English   中英

如何在Tensorflow中打印出預測概率

[英]How to print out the prediction probabilities in Tensorflow

我是tf新手。 我已經訓練了一個編碼器-使用張量流的解碼器。 該程序將一個單詞作為輸入並打印出其音素。

例如:Hello World-> ['h','E','l','“,'@ U',','w','”,'3`','r',' 5','d']

我想獲得所選每個音素的預測概率。

在預測部分,我使用的代碼如下:

def predict(words, sess):

    if len(words) > hp.batch_size:
        after = predict(words[hp.batch_size:], sess)
        words = words[:hp.batch_size]

    else:
        after = []
    x = np.zeros((len(words), hp.maxlen), np.int32)  # 0: <PAD>
    for i, w in enumerate(words):
        for j, g in enumerate((w + "E")[:hp.maxlen]):
            x[i][j] = g2idx.get(g, 2)         


    preds = np.zeros((len(x), hp.maxlen), np.int32)
    for j in range(hp.maxlen):

        xpreds = sess.run(graph.preds, {graph.x: x, graph.y: preds})
        preds[:, j] = xpreds[:, j]

先感謝您!

我的主要問題是“隱藏”這些概率的位置以及如何訪問它們。 例如,單詞“ Hello”中的字母“ o”被映射為音素“ @U”。 我想找出“ @U”被選為理想音素的可能性。

在討論之后,我想我可以指出您應該在哪里更改代碼。 在train.py中,第104行:

self.preds = tf.to_int32(tf.argmax(logits, -1))

他們將preds變量分配給索引的可能性最高。 為了獲得softmax預測,可以如下更改代碼:

self.preds = tf.nn.softmax(logits)

我認為應該這樣做。

如何查看概率:

preds = np.zeros((len(x), hp.maxlen), np.float32)
for j in range(hp.maxlen):
    xpreds = sess.run(graph.preds, {graph.x: x, graph.y: preds})
    # print shape of output -> batch_size, max_length,number_of_output_options
    print(xpreds.shape)
    # print all predictions of the first output
    print(xpreds[0, 0])
    # print the probabilty of the network prediction
    print(xpreds[0, 0, np.argmax(xpreds[0][0])])
    # preds[:, j] = _preds[:, j]     Need to accumulate the results according to the correct output shape

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM