在TF里,使用32位整数来计算倒数时会抛出这个异常:
Traceback (most recent call last):
File "C:\python35\lib\site-packages\tensorflow\python\client\session.py", line 1022, in _do_call
return fn(*args)
File "C:\python35\lib\site-packages\tensorflow\python\client\session.py", line 1000, in _run_fn
self._extend_graph()
File "C:\python35\lib\site-packages\tensorflow\python\client\session.py", line 1049, in _extend_graph
self._session, graph_def.SerializeToString(), status)
File "C:\python35\lib\contextlib.py", line 66, in __exit__
next(self.gen)
File "C:\python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 469, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: No OpKernel was registered to support Op 'Reciprocal' with these attrs. Registered devices: [CPU,GPU], Registered kernels:
device='CPU'; T in [DT_FLOAT]
device='CPU'; T in [DT_HALF]
device='CPU'; T in [DT_DOUBLE]
device='CPU'; T in [DT_COMPLEX64]
device='CPU'; T in [DT_COMPLEX128]
device='GPU'; T in [DT_FLOAT]
device='GPU'; T in [DT_HALF]
device='GPU'; T in [DT_DOUBLE]
device='GPU'; T in [DT_INT64]
[[Node: Reciprocal = Reciprocal[T=DT_INT32](Variable_1/read)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\work\csdn\TF_API\src\TFAPI_6\TFAPI_6\TFAPI_17.py", line 31, in <module>
demo.run_graph()
File "D:\work\csdn\TF_API\src\TFAPI_6\TFAPI_6\TFAPI_17.py", line 21, in run_graph
sess.run(init)
File "C:\python35\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
run_metadata_ptr)
File "C:\python35\lib\site-packages\tensorflow\python\client\session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "C:\python35\lib\site-packages\tensorflow\python\client\session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "C:\python35\lib\site-packages\tensorflow\python\client\session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: No OpKernel was registered to support Op 'Reciprocal' with these attrs. Registered devices: [CPU,GPU], Registered kernels:
device='CPU'; T in [DT_FLOAT]
device='CPU'; T in [DT_HALF]
device='CPU'; T in [DT_DOUBLE]
device='CPU'; T in [DT_COMPLEX64]
device='CPU'; T in [DT_COMPLEX128]
device='GPU'; T in [DT_FLOAT]
device='GPU'; T in [DT_HALF]
device='GPU'; T in [DT_DOUBLE]
device='GPU'; T in [DT_INT64]
[[Node: Reciprocal = Reciprocal[T=DT_INT32](Variable_1/read)]]
Caused by op 'Reciprocal', defined at:
File "<string>", line 1, in <module>
File "C:\python35\lib\idlelib\run.py", line 130, in main
ret = method(*args, **kwargs)
File "C:\python35\lib\idlelib\run.py", line 357, in runcode
exec(code, self.locals)
File "D:\work\csdn\TF_API\src\TFAPI_6\TFAPI_6\TFAPI_17.py", line 30, in <module>
demo = DemoTF()
File "D:\work\csdn\TF_API\src\TFAPI_6\TFAPI_6\TFAPI_17.py", line 16, in __init__
self.reciprocal = tf.reciprocal(self.num2)
File "C:\python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 1964, in reciprocal
result = _op_def_lib.apply_op("Reciprocal", x=x, name=name)
File "C:\python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op
op_def=op_def)
File "C:\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2395, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1264, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): No OpKernel was registered to support Op 'Reciprocal' with these attrs. Registered devices: [CPU,GPU], Registered kernels:
device='CPU'; T in [DT_FLOAT]
device='CPU'; T in [DT_HALF]
device='CPU'; T in [DT_DOUBLE]
device='CPU'; T in [DT_COMPLEX64]
device='CPU'; T in [DT_COMPLEX128]
device='GPU'; T in [DT_FLOAT]
device='GPU'; T in [DT_HALF]
device='GPU'; T in [DT_DOUBLE]
device='GPU'; T in [DT_INT64]
[[Node: Reciprocal = Reciprocal[T=DT_INT32](Variable_1/read)]]
从异常信息上来看,它是不支持tf.int32的类型运算的,所以抛出异常。只支持下面的类型:
device='CPU'; T in [DT_FLOAT]
device='CPU'; T in [DT_HALF]
device='CPU'; T in [DT_DOUBLE]
device='CPU'; T in [DT_COMPLEX64]
device='CPU'; T in [DT_COMPLEX128]
device='GPU'; T in [DT_FLOAT]
device='GPU'; T in [DT_HALF]
device='GPU'; T in [DT_DOUBLE]
device='GPU'; T in [DT_INT64]
代码如下:
#python 3.5.3/TensorFlow 1.0/win 10 #2017-04-01 蔡军生 http://blog.csdn.net/caimouse # #导入要使用的库 import tensorflow as tf import numpy as np #演示API的类 class DemoTF: def __init__(self): '''构造函数''' self.num1 = tf.Variable([1, -2, -3], dtype = tf.int32) self.num2 = tf.Variable([4, 5, -6], dtype = tf.int32) self.sign = tf.sign(self.num1) self.reciprocal = tf.reciprocal(self.num2) def run_graph(self): init = tf.global_variables_initializer() # 初始化变量 with tf.Session() as sess: sess.run(init) self.main(sess) #运行主函数 def main(self, sess): '''主函数''' print(sess.run(self.sign)) print(sess.run(self.reciprocal)) #主程序入口 if __name__ == "__main__": demo = DemoTF() demo.run_graph()