回答
您可以使用動態整形在運行期間通過獲取批量維度的值,並將整個新維度集合計算爲tf.reshape
。以下是將平面列表重新整形爲方形矩陣而不知道列表長度的示例。
tf.reset_default_graph()
sess = tf.InteractiveSession("")
a = tf.placeholder(dtype=tf.int32)
# get [9]
ashape = tf.shape(a)
# slice the list from 0th to 1st position
ashape0 = tf.slice(ashape, [0], [1])
# reshape list to scalar, ie from [9] to 9
ashape0_flat = tf.reshape(ashape0,())
# tf.sqrt doesn't support int, so cast to float
ashape0_flat_float = tf.to_float(ashape0_flat)
newshape0 = tf.sqrt(ashape0_flat_float)
# convert [3, 3] Python list into [3, 3] Tensor
newshape = tf.pack([newshape0, newshape0])
# tf.reshape doesn't accept float, so convert back to int
newshape_int = tf.to_int32(newshape)
a_reshaped = tf.reshape(a, newshape_int)
sess.run(a_reshaped, feed_dict={a: np.ones((9))})
您應該看到
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]], dtype=int32)
你可以用tf.reshape()輕鬆地做到這一點不知道批量大小。
x = tf.placeholder(tf.float32, shape=[None, 9,2])
shape = x.get_shape().as_list() # a list: [None, 9, 2]
dim = numpy.prod(shape[1:]) # dim = prod(9,2) = 18
x2 = tf.reshape(x, [-1, dim]) # -1 means "all"
在最後一行的-1
意味着整個列無論batchsize是在運行什麼。你可以在tf.reshape()看到它。
更新:形狀= [無,3,無]
由於@kbrose。對於未定義多於1維的情況,我們可以使用tf.shape()和tf.reduce_prod()。
x = tf.placeholder(tf.float32, shape=[None, 3, None])
dim = tf.reduce_prod(tf.shape(x)[1:])
x2 = tf.reshape(x, [-1, dim])
tf.shape()返回一個可以在運行時評估的形狀張量。 tf.get_shape()和tf.shape()之間的區別可以看出in the doc。
我也試過tf.contrib.layers.flatten()在另一個。第一種情況最簡單,但不能處理第二種情況。
這種運作良好,如果你知道所有的其他尺寸的大小,但不會,如果其他方面有不明尺寸。例如。 'x = tf.placeholder(tf.float32,shape = [None,9,None])' – kbrose
thanks @kbrose。我已經更新了案例的答案。 – weitang114
@ weitang114太棒了! – kbrose
flat_inputs = tf.contrib.layers.flatten(inputs)
- 1. 張量流中的批量歸一化
- 2. torch7:如何拼合張量?
- 3. 張量流中的批處理問題
- 4. 張量流中graph_cnn的批處理
- 5. PIL圖像張量張量,張量流
- 6. 比較張量(張量流)
- 7. 如何加入張量流中的張量張量?
- 8. 批量輸入到張量流中的某一層
- 9. 爲什麼在無張量流的批量維中使用None?
- 10. 張量流程中的默認在線或批量培訓
- 11. 總結張量流量張量列表
- 12. 如何在張量流中優化批量加載?
- 13. 張量流的深度和寬度的小批量
- 14. 如何將我的三維張量組合成張量流中的單張張量?
- 15. 如何在張量流中對一批二維張量進行排序?
- 16. 張量流中的Torch.Min
- 17. 將張量表傳遞給張量流
- 18. 將張量分配給張量流
- 19. 張量層疊教程張量流?
- 20. Tensorflow大張量分流到小張量
- 21. 帶張量流量動量的SGD
- 22. 評估張量流中兩個張量行的所有對組合
- 23. 張量流立體圖像批量學習
- 24. 使用張量流執行批量歸一化
- 25. 張量流的tensordot中使用批乘法的障礙
- 26. 張量流會議
- 27. 改變張量流中的張量的尺度
- 28. 如何對張量流中的一批圖像執行tf.image.per_image_standardization
- 29. 如何爲張量流中的張量子集賦值?
- 30. 在張量流中,如何將張量的行重置爲零?
我沒有看到任何方法'tf.batch'在此解決方案或Tensorflow ... – Muneeb