簡體   English   中英

Tensorflow:如何使用 tensorflow 操作從給定的字符串列表中選擇一個隨機字符串

[英]Tensorflow: How to chose a random string from a given list of strings using tensorflow operation

我有一個像下面這樣的字符串列表,我想做張量流操作。 對於來自list_1的給定輸入字符串,我想從list_2隨機選擇一個字符串。

我的功能是這樣的;

list_1 = ['hello', 'how are you?', 'this is just a test']
list_2 = ['helloooo', 'thanks', 'okay']

def test(string):
    if string in list_1:
        print("list: ", random.choice(list_2)) 
test("hello") 

我如何使用 tensorflow 實現這一目標? 因為我不覺得任何東西tf.random.choice在tensorflow。

這是一種基於 Tensorflow Ops 的不同方法:

import tensorflow as tf

list_2 = ['helloooo', 'thanks', 'okay']
len_list = tf.size(list_2)
rand_var = tf.random_uniform([1],0,len_list, dtype=tf.int32)
output = tf.gather(list_2,rand_var[0])

with tf.Session() as sess:
    print(sess.run(output))

暫無
暫無

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

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