TensorFlow的异常Reciprocal[T=DT_INT32](Variable_1/read)

在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()

1. TensorFlow API攻略

2. TensorFlow入门基本教程

3. C++标准模板库从入门到精通 

4.跟老菜鸟学C++

5. 跟老菜鸟学python

6. 在VC2015里学会使用tinyxml库

7. 在Windows下SVN的版本管理与实战 

 http://edu.csdn.net/course/detail/2579

8.Visual Studio 2015开发C++程序的基本使用 

http://edu.csdn.net/course/detail/2570

9.在VC2015里使用protobuf协议

10.在VC2015里学会使用MySQL数据库



分析一下这段代码:#include "stdio.h" #include<xmmintrin.h> //Need this for SSE compiler intrinsics #include<math.h> //Needed for sqrt in CPU-only version #include<time.h> int main(int argc,char *argv[]) { printf("Starting calculation...\n"); const int length=64000; //We will be calculating Y=SQRT(x)/x, for x=1->64000 //If you do not properly align your data for SSE instructions, you may take a huge performance hit. float *pResult=(float *)_aligned_malloc(length*sizeof(float),16); //align to 16-byte for SSE __m128 x; __m128 xDelta=_mm_set1_ps(4.0f); //Set the xDelta to (4,4,4,4) __m128 *pResultSSE=(__m128 *)pResult; const int SSELength=length/4; clock_t clock1=clock(); #define TIME_SSE //Define this if you want to run with SSE #ifdef TIME_SSE //lots of stress loops so we can easily use a stopwatch for(int stress=0;stress<1000;stress++) { //Set the initial values of x to (4,3,2,1) x=_mm_set_ps(4.0f,3.0f,2.0f,1.0f); for(int i=0; i<SSELength; i++) { __m128 xSqrt=_mm_sqrt_ps(x); //Note! Division is slow. It's actually faster to take the reciprocal of a number and multiply //Also note that Division is more accurate than taking the reciprocal and multiplying #define USE_DIVISION_METHOD #ifdef USE_FAST_METHOD _m128 xRecip=_mm_rcp_ps(x); pResultSSE[i]=_mm_mul_ps(xRecip,xSqrt); #endif //USE_FAST_METHOD #ifdef USE_DIVISION_METHOD pResultSSE[i]=_mm_div_ps(xSqrt,x); #endif //USE_DIVISION_METHOD //Advance x to the next set of numbers x=_mm_add_ps(x,xDelta); } } clock_t clock2=clock(); printf("SIMDtime:%d ms\n",1000*(clock2-clock1)/CLOCKS_PER_SEC); #endif //TIME_SSE #define TIME_noSSE #ifdef TIME_noSSE clock_t clock3=clock(); //lots of stress loops so we can easily use a stopwatch for(int stress=0;stress<1000;stress++) { clock_t clock3=clock(); float xFloat=1.0f; for(int i=0;i<length;i++) { //Even though division is slow,there are no intrinsic functions like there are in SSE pResult[i]=sqrt(xFloat)/xFloat; xFloat+=1.0f; } } clock_t clock4=clock(); printf("noSIMDtime:%d ms\n",1000*(clock4-clock3)/CLOCKS_PER_SEC); #endif //TIME_noSSE return 0; }
最新发布
06-06
这段代码使用了 SSE 指令来计算 Y=SQRT(x)/x,其中 x 的值从 1 到 64000。程序首先使用 `_aligned_malloc` 函数分配了一段内存,用于存储计算结果。由于 SSE 指令要求数据在内存中的对齐方式为 16 字节对齐,因此在分配内存时需要指定对齐方式为 16 字节。程序定义了一个 SSE 类型的变量 `x`,并使用 `_mm_set1_ps` 函数初始化了 `xDelta` 变量的值为 (4,4,4,4)。接下来,程序使用了条件编译指令,如果定义了 `TIME_SSE` 宏,就会执行使用 SSE 指令计算的代码,否则将执行没有使用 SSE 指令的代码。在使用 SSE 指令计算时,程序使用了嵌套的循环,外层循环执行了 1000 次,内层循环执行了 64000/4=16000 次。在内层循环中,程序使用 `_mm_set_ps` 函数初始化 `x` 变量的值为 (4,3,2,1),然后使用 `_mm_sqrt_ps` 函数计算 `x` 的平方根,并根据条件编译指令使用 SSE 指令计算 Y 值,最后将结果存储在 `pResultSSE` 数组中。在每次循环结束后,程序使用 `_mm_add_ps` 函数将 `x` 的值加上 `xDelta`,以便计算下一组数据的 Y 值。在计算完成后,程序使用 clock 函数计算了执行时间,并输出结果。在没有使用 SSE 指令的情况下,程序使用了嵌套的循环,外层循环执行了 1000 次,内层循环执行了 64000 次。在内层循环中,程序使用 `sqrt` 函数计算 `x` 的平方根,并计算 Y 值,最后将结果存储在 `pResult` 数组中。在每次循环结束后,程序将 x 的值加上 1,以便计算下一组数据的 Y 值。计算完成后,程序使用 clock 函数计算了执行时间,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值