python-3.x - Tensorflow 中 hstack 和 vstack 的对应物
问题描述
numpy
函数hstack
和Tensorflow的正确对应物是vstack
什么?
有tf.stack
和tf.concat
,Tensorflow
但我不知道如何使用它们或使用正确的axis
值,以在 Tensorflow 中实现相同的行为。
解决方案
您应该使用tf.concat
with 不同的参数来获得与 with oraxis
相同的结果:hstack
vstack
arr1 = np.random.random((2,3))
arr2 = np.random.random((2,3))
arr1
array([[0.72315241, 0.9374959 , 0.18808236],
[0.74153715, 0.85361367, 0.13258545]])
arr2
array([[0.80159933, 0.8123236 , 0.80555496],
[0.82570606, 0.4092662 , 0.69123989]])
np.hstack([arr1, arr2])
array([[0.72315241, 0.9374959 , 0.18808236, 0.80159933, 0.8123236 ,
0.80555496],
[0.74153715, 0.85361367, 0.13258545, 0.82570606, 0.4092662 ,
0.69123989]])
np.hstack([arr1, arr2]).shape
(2, 6)
np.vstack([arr1, arr2])
array([[0.72315241, 0.9374959 , 0.18808236],
[0.74153715, 0.85361367, 0.13258545],
[0.80159933, 0.8123236 , 0.80555496],
[0.82570606, 0.4092662 , 0.69123989]])
np.vstack([arr1, arr2]).shape
(4, 3)
t1 = tf.convert_to_tensor(arr1)
t2 = tf.convert_to_tensor(arr2)
tf.concat([t1, t2], axis=1)
<tf.Tensor: id=9, shape=(2, 6), dtype=float64, numpy=
array([[0.72315241, 0.9374959 , 0.18808236, 0.80159933, 0.8123236 ,
0.80555496],
[0.74153715, 0.85361367, 0.13258545, 0.82570606, 0.4092662 ,
0.69123989]])>
tf.concat([t1, t2], axis=1).shape.as_list()
[2, 6]
tf.concat([t1, t2], axis=0)
<tf.Tensor: id=19, shape=(4, 3), dtype=float64, numpy=
array([[0.72315241, 0.9374959 , 0.18808236],
[0.74153715, 0.85361367, 0.13258545],
[0.80159933, 0.8123236 , 0.80555496],
[0.82570606, 0.4092662 , 0.69123989]])>
tf.concat([t1, t2], axis=0).shape.as_list()
[4, 3]
tf.stack
仅当您想沿新轴连接张量时才应使用:
tf.stack([t1, t2]).shape.as_list()
[2, 2, 3]
换句话说,tf.stack
创建一个新维度并将张量堆叠在一起。
推荐阅读
- mysql - 使用 Datadog 监控 Aurora 不显示查询量的变化
- c - 我已经在 C 中声明了一个大小为 1 的字符数组,但我已经读取了一个大小为 10 的字符串,但给定的代码工作得非常好。
- ios - 当 iOS 应用收到推送通知且状态为关闭时触发 API 调用
- d3.js - D3 - 位置工具提示
- django - 按条件顺序检索 Django 查询集
- c++ - Ctrl+c 杀死进程,尽管有信号处理,但在不同的机器上工作
- bing-maps - 隐藏加载平铺动画覆盖 bing V8
- android - 改造2等待响应
- qt - QT - 最大化 QGraphicsScene SIZE
- php - PHP 警告:无法修改标头信息需要错误解决