问题
在日常用 tensorflow 进行编程的时候,我经常会纳罕一个问题:
明明 manual里面 白纸黑字地注明了 某个参数项 的 输入 必须是 tensor型,可是 非tensor型 的数据 输入后 却不会报错,依然能正常算出结果。
示例
比如,合法的输入应该如下:
import tensorflow as tf
a = tf.constant(10)
b = tf.constant(20)
c = tf.multiply(a, b)
sess = tf.InteractiveSession()
print c.eval()
打印结果:
200
但是如果你这么玩:
import tensorflow as tf
a = 10
b = 20
c = tf.multiply(a, b)
sess = tf.InteractiveSession()
print c.eval()
照样打印出一样的结果来:
200
但是 manual 里面已经写明了 tf.multiply函数
的 参数项输入
必须要是 tensor型 的:
具体见 Args
一栏:
原因
仔细查看 tensorflow 官网 的 api manual,页首经常会有这个 提示 :
通过查看源码,我发现凡是注明了
Note: Functions taking Tensor arguments can also take anything accepted by tf.convert_to_tensor.
这句话的,此类接口在源码中,都有对 input 先进行 tf.convert_to_tensor
的 预处理 。所以当 非tensor型
数据 输入时,当然就 不会报错 啦~