TypeError: __init__() got an unexpected keyword argument 'shape'

采用TensorFlow支持通过tf.Graph函数来生成新的向量图,代码如下:

import tensorflow as tf g1 = tf.Graph() with g1.as_default(): v = tf.get_variable( "v",initializer=tf.zeros_initializer(shape = [1])) g2 = tf.Graph() with g2.as_default(): v = tf.get_variable( "v",initializer=tf.ones_initializer(shape = [1])) with tf.Session(graph=g1) as sess: tf.initialize_all_variables().run() with tf.variable_scope("",reuse=True): print(sess.run(tf.get_variable("v"))) with tf.Session(graph=g2) as sess: tf.initialize_all_variables().run() with tf.variable_scope("",reuse=True): print(sess.run(tf.get_variable("v")))

执行后发生如下错误:


解决办法:因为上述代码写法是TensorFlow旧版本的写法,将Line6 和Line10 改如下如下可以实现代码的正常运行:

v = tf.get_variable("v",initializer=tf.zeros_initializer()(shape = [1]))

v = tf.get_variable( "v",initializer=tf.ones_initializer()(shape = [1]))

输出结果如下图:


明显有一个更新提示,表示该初始化的语句也需要进行更新:

tf.initialize_all_variables().run() 变成 tf.global_variables_initializer().run()

最后输出结果:

参考链接:https://stackoverflow.com/questions/44946189/typeerror-init-got-an-unexpected-keyword-argument-shape

已标记关键词 清除标记
<div><p>When I was running image_demo.py on a branch based on mmdetv2, a problem occurred. I was testing detectors_htc_r50_1x_coco.py, how can I fix it? </p> <p>/home/powerop/.local/lib/python3.6/site-packages/mmcv/utils/registry.py:64: UserWarning: The old API of register_module(module, force=False) is deprecated and will be removed, please use the new API register_module(name=None, force=False, module=None) instead. 'The old API of register_module(module, force=False) ' Traceback (most recent call last): File "image_demo.py", line 26, in main() File "image_demo.py", line 18, in main model = init_detector(args.config, args.checkpoint, device=args.device) File "/home/powerop/work/package/mmdetection-master/mmdet/apis/inference.py", line 33, in init_detector model = build_detector(config.model, test_cfg=config.test_cfg) File "/home/powerop/work/package/mmdetection-master/mmdet/models/builder.py", line 48, in build_detector return build(cfg, DETECTORS, dict(train_cfg=train_cfg, test_cfg=test_cfg)) File "/home/powerop/work/package/mmdetection-master/mmdet/models/builder.py", line 20, in build return build_from_cfg(cfg, registry, default_args) File "/home/powerop/.local/lib/python3.6/site-packages/mmcv/utils/registry.py", line 167, in build_from_cfg return obj_cls(<strong>args) File "/home/powerop/work/package/mmdetection-master/mmdet/models/detectors/htc.py", line 9, in <strong>init</strong> super(HybridTaskCascade, self).<strong>init</strong>(</strong>kwargs) File "/home/powerop/work/package/mmdetection-master/mmdet/models/detectors/cascade_rcnn.py", line 23, in <strong>init</strong> pretrained=pretrained) File "/home/powerop/work/package/mmdetection-master/mmdet/models/detectors/two_stage.py", line 26, in <strong>init</strong> self.backbone = build_backbone(backbone) File "/home/powerop/work/package/mmdetection-master/mmdet/models/builder.py", line 24, in build_backbone return build(cfg, BACKBONES) File "/home/powerop/work/package/mmdetection-master/mmdet/models/builder.py", line 20, in build return build_from_cfg(cfg, registry, default_args) File "/home/powerop/.local/lib/python3.6/site-packages/mmcv/utils/registry.py", line 167, in build_from_cfg return obj_cls(**args) TypeError: <strong>init</strong>() got an unexpected keyword argument 'sac'</p><p>该提问来源于开源项目:joe-siyuan-qiao/DetectoRS</p></div>
©️2020 CSDN 皮肤主题: 我行我“速” 设计师:Amelia_0503 返回首页