回答
您可以使用动态整形在运行期间通过获取批量维度的值,并将整个新维度集合计算为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