【问题标题】:How to join object and its member in python如何在python中加入对象及其成员
【发布时间】:2019-01-05 05:32:13
【问题描述】:

我有一个名为net 的python caffe object。 通常在python中,net为其层设置为

net.conv1_1 = L.Convolution(net[from_layer], num_output=64, pad=1, kernel_size=3, **kwargs)

但对我来说,我将层名称作为程序中的变量而不是硬编码。 那么我如何将层名称 conv1 加入网络。我做了

join( net.,'%s'%(layer[lIdx]['l_name']))=L.Convolution(net[layer[lIdx-1]['l_name']], num_output=layer[lIdx]['n_channels'], pad=layer[lIdx]['l_struct'][2], kernel_size=layer[lIdx]['l_struct'][0], **kwargs)

这个join( net.,'%s'%(layer[lIdx]['l_name']))给了我SyntaxError: ('invalid syntax',

【问题讨论】:

    标签: python neural-network deep-learning caffe pycaffe


    【解决方案1】:

    使用__getitem__查看this answer,或者使用setattr查看this answer

    n[layer[lIdx]['l_name']] = L.Convolution( # ...
    

    setattr(n, layer[lIdx]['l_name'], L.Convolution( # ...
    

    Python 的join 作用于字符串并生成字符串,net.conv1_1 不是字符串:它是对象netconv1_1 属性。

    【讨论】:

      猜你喜欢
      • 2012-07-21
      • 1970-01-01
      • 2018-02-20
      • 2012-06-07
      • 2011-09-10
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      相关资源
      最近更新 更多