python - Tensorflow-tf.concat() 显示不同的结果
问题描述
with tf.Session() as sess:
t1 = [
[[0],[1],[2]],
[[3],[4],[5]],
]
print(sess.run(tf.shape(t1)))
print(sess.run(tf.concat(t1,1)))
print("**********")
t2 = np.arange(6).reshape([2,3,1])
print(sess.run(tf.shape(t2)))
print(sess.run(tf.concat(t2,1)))
然后它显示
[2 3 1]
[[0 3] [1 4] [2 5]]
[2 3 1]
[[[0] [1] [2]] [[3] [4] [5]]]
t1 和 t2 具有相同的形状和值,为什么结果不同?
解决方案
因为t1
是list
,t2
不是。
tf.concat
连接张量列表。因此t1
被视为3x1
要连接的 2 个大小张量的列表。另一方面,t2
是一个 numpy 数组,它被转换为一个2x3x1
Tensor
,并且没有其他东西可以连接这个张量。
我认为可能让您感到惊讶的是,t1
根据上下文的不同,它的解释也不同。tf.shape
期望 aTensor
作为参数,而不是张量列表,因此当传递给此函数时,t1
被解释为2x3x1
张量。
推荐阅读
- android - Android Q RecoverableSecurityException 未授予访问权限
- asp.net-core - 如何防止 NLog 处理引用库中的日志语句?
- wso2 - 带有用户名和密码的 WSO2 分析
- java - 如何为像 Ada 这样的简单编程语言扩展抽象语法分析器 Java 类
- python - 正则表达式查找并列出字符串中包含破折号的任何三个字符和最后一个匹配项
- javascript - 调用请求后如何重新渲染屏幕?
- django - 未找到参数“(”,)”的“update_order”反向。尝试了 1 种模式:['update_order\\/(?P
[^/]+)\\/$'] - swift - 为什么 NSWindowController 会返回零值窗口属性?
- angular - ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。以前的值:“未定义”异常
- javascript - 如何使 javascript 值等于我从 Razor 代码收到的值