簡體   English   中英

如何使用嵌套形狀的tf.data.Dataset.padded_batch?

[英]How to use tf.data.Dataset.padded_batch with a nested shape?

我正在為每個元素構建一個具有兩個形狀[張,寬,高,3]和[批次,類]張量的數據集。 為簡單起見,我們可以說class = 5。

您將什么形狀提供給dataset.padded_batch(1000,shape) ,以便沿寬度/高度/ 3軸填充圖像?

我嘗試過以下方法:

tf.TensorShape([[None,None,None,3],[None,5]])
[tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5])]
[[None,None,None,3],[None,5]]
([None,None,None,3],[None,5])
(tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5])‌​)

每次引發TypeError

文檔說明:

padded_shapes:tf.TensorShape或tf.int64向量張量類對象的嵌套結構,表示在批處理之前應填充每個輸入元素的相應組件的形狀。 任何未知的尺寸(例如tf.TensorShape中的tf.Dimension(無)或類似張量的對象中的-1)將被填充到每個批次中該維度的最大尺寸。

相關代碼:

dataset = tf.data.Dataset.from_generator(generator,tf.float32)
shapes = (tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5]))
batch = dataset.padded_batch(1,shapes)

感謝mrry尋找解決方案。 事實證明,from_generator中的類型必須與條目中的張量數相匹配。

新代碼:

dataset = tf.data.Dataset.from_generator(generator,(tf.float32,tf.float32))
shapes = (tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5]))
batch = dataset.padded_batch(1,shapes)

TensorShape不接受嵌套列表。 tf.TensorShape([None, None, None, 3, None, 5])TensorShape(None) (注意沒有[] )是合法的。

但是,結合這兩個張量對我來說聽起來很奇怪。 我不確定你要完成什么,但我建議你嘗試不使用不同尺寸的張量。

暫無
暫無

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

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