博主在import tensorflow 的时候,出现如下错误:
>>> import tensorflow as tf
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.4/dist-packages/tensorflow/init.py", line 22, in
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-im port
File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/init.py", l ine 52, in
from tensorflow.core.framework.graph_pb2 import *
File "/usr/local/lib/python3.4/dist-packages/tensorflow/core/framework/graph_p b2.py", line 15, in
from tensorflow.core.framework import node_def_pb2 as tensorflow_dot_core_do t_framework_dot_node__def__pb2
File "/usr/local/lib/python3.4/dist-packages/tensorflow/core/framework/node_de f_pb2.py", line 15, in
from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_ dot_framework_dot_attr__value__pb2
File "/usr/local/lib/python3.4/dist-packages/tensorflow/core/framework/attr_va lue_pb2.py", line 15, in
from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_ framework_dot_tensor__pb2
File "/usr/local/lib/python3.4/dist-packages/tensorflow/core/framework/tensor_ pb2.py", line 15, in
from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_ core_dot_framework_dot_resource__handle__pb2
File "/usr/local/lib/python3.4/dist-packages/tensorflow/core/framework/resourc e_handle_pb2.py", line 22, in
serialized_pb=_b('\n/tensorflow/core/framework/resource_handle.proto\x12\nte nsorflow"r\n\x13ResourceHandleProto\x12\x0e\n\x06\x64\x65vice\x18\x01 \x01(\t\x 12\x11\n\tcontainer\x18\x02 \x01(\t\x12\x0c\n\x04name\x18\x03 \x01(\t\x12\x11\n\ thash_code\x18\x04 \x01(\x04\x12\x17\n\x0fmaybe_type_name\x18\x05 \x01(\tBn\n\x1 8org.tensorflow.frameworkB\x0eResourceHandleP\x01Z=github.com/tensorflow/tensorf low/tensorflow/go/core/framework\xf8\x01\x01\x62\x06proto3')
TypeError: init() got an unexpected keyword argument 'serialized_options'
分析原因:出现这个问题,博主认为主要以为TensorFlow版本与protobuf版本不匹配的原因。
解决办法:首先输入命令 conda list 查看你TensorFlow版本与protobuf版本,如果你使用的是tensorflow 1.8或更早版本,请使用protobuf 3.4或更早版本。博主亲测,TensorFlow1.8与protobuf3.4匹配。如果您使用的是TF 1.9或更高版本,请使用最新版本的protobuf。目前最新好像是3.6.
tensorflow 1.11.0必须要求protobuf> = 3.6.0
方法1:先卸载:
pip uninstall protobuf
重新安装protobuf:
pip install -U protobuf
方法2:
pip3 uninstall protobuf
pip3 uninstall tensorflow (卸载之前版本)
pip3 install tensorflow == 1.9.0(安装protobuf-3.6.0 tensorflow-1.5.0)
总之,TensorFlow版本与protobuf版本,必须要匹配,可以降低TensorFlow版本。亲测的版本是:TensorFlow1.9与protobuf3.6匹配。TensorFlow1.8与protobuf3.4匹配。至于其他的,可以慢慢试那种搭配合适。