How to input a list of lists with different sizes in tf.data.Dataset

前端 未结 4 1399
一整个雨季
一整个雨季 2020-12-03 01:49

I have a long list of lists of integers (representing sentences, each one of different sizes) that I want to feed using the tf.data library. Each list (of the lists of list)

4条回答
  •  天命终不由人
    2020-12-03 02:04

    You can use tf.data.Dataset.from_generator() to convert any iterable Python object (like a list of lists) into a Dataset:

    t = [[4, 2], [3, 4, 5]]
    
    dataset = tf.data.Dataset.from_generator(lambda: t, tf.int32, output_shapes=[None])
    
    iterator = dataset.make_one_shot_iterator()
    next_element = iterator.get_next()
    
    with tf.Session() as sess:
      print(sess.run(next_element))  # ==> '[4, 2]'
      print(sess.run(next_element))  # ==> '[3, 4, 5]'
    

提交回复
热议问题