首页 > 解决方案 > 如何在python中加入对象及其成员

问题描述

我有一个caffe object名为net. 通常在python中,网络为其层设置为

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',

标签: pythonneural-networkdeep-learningcaffepycaffe

解决方案


使用查看此答案__getitem__,或使用查看此答案setattr

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

或者

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

Pythonjoin处理字符串并生成字符串,net.conv1_1不是字符串:它conv1_1是 object 的属性net


推荐阅读