在Lasagne中定义空间网络时的属性错误

时间:2016-05-12 13:59:04

标签: deep-learning conv-neural-network lasagne

我无法找到在千层面中使用空间网络的很多例子,因此我可能在定义网络时犯了错误。请查看我的网络定义,如果故障在于我的网络配置或其他一些问题,请告诉我。

``` net = NeuralNet(

layers=[('loc_input',    InputLayer),
        ('loc_conv2d1',  Conv2DLayer),
        ('loc_maxpool1', MaxPool2DLayer),
        ('loc_conv2d2',  Conv2DLayer),
        ('loc_maxpool2', MaxPool2DLayer),
        ('loc_dense',    DenseLayer),
        ('loc_output',   DenseLayer),

        ('STN1', TransformerLayer),

        ('conv2d1',  Conv2DLayer),
        ('maxpool1', MaxPool2DLayer),
        ('conv2d2',  Conv2DLayer),
        ('maxpool2', MaxPool2DLayer),
        ('dense',    DenseLayer),
        ('dropout1', DropoutLayer),
        ('dense',    DenseLayer),
        ('output',   DenseLayer),
        ],

loc_input_shape=(None, 1, X_train.shape[2],X_train.shape[3]),
# layer conv2d1
loc_conv2d1_num_filters=32,
loc_conv2d1_filter_size=(5, 5),
loc_conv2d1_stride=2,
loc_conv2d1_W=lasagne.init.HeUniform(),  
# layer maxpool1
loc_maxpool1_pool_size=(2, 2),    
# layer conv2d2
loc_conv2d2_num_filters=64,
loc_conv2d2_filter_size=(5, 5),
loc_conv2d2_stride=2,
loc_conv2d2_W=lasagne.init.HeUniform(),
# layer maxpool2
loc_maxpool2_pool_size=(2, 2),
loc_dense_num_units=64,     
# dense
loc_output_num_units=6, 

#Spatial Transformer Network
STN1_incoming = 'loc_input',
STN1_localization_network = 'loc_output',
STN1_downsample_factor = 1,

# layer conv2d1
conv2d1_incoming = 'STN1',
conv2d1_num_filters=32,
conv2d1_filter_size=(3, 3),
conv2d1_stride=2,
conv2d1_nonlinearity=lasagne.nonlinearities.rectify,
conv2d1_W=lasagne.init.GlorotUniform(),  
# layer maxpool1
maxpool1_pool_size=(2, 2),    
# layer conv2d2
conv2d2_num_filters=64,
conv2d2_filter_size=(3, 3),
conv2d2_stride=2,
conv2d2_nonlinearity=lasagne.nonlinearities.rectify,
# layer maxpool2
maxpool2_pool_size=(2, 2),
# dropout1
dropout1_p=0.5,    
# dense
dense_num_units=256,
dense_nonlinearity=lasagne.nonlinearities.rectify,       
# output
output_nonlinearity= softmax,
output_num_units=numClasses,

# optimization method params
update=nesterov_momentum,
update_learning_rate=0.01,
update_momentum=0.9,
max_epochs=20,
verbose=1,
)

```

当我启动网络时,出现以下错误:

```
AttributeError                            Traceback (most recent call last)
<ipython-input-84-29eabf8b9697> in <module>()
----> 1 net.initialize()

D:\Python Directory\winPython 2.7\python-2.7.10.amd64\lib\site-packages\nolearn\lasagne\base.pyc in initialize(self)
    360         out = getattr(self, '_output_layer', None)
    361         if out is None:
--> 362             out = self._output_layer = self.initialize_layers()
    363         self._check_for_unused_kwargs()
    364 

D:\Python Directory\winPython 2.7\python-2.7.10.amd64\lib\site-packages\nolearn\lasagne\base.pyc in initialize_layers(self, layers)
    452             try:
    453                 layer_wrapper = layer_kw.pop('layer_wrapper', None)
--> 454                 layer = layer_factory(**layer_kw)
    455             except TypeError as e:
    456                 msg = ("Failed to instantiate {} with args {}.\n"

D:\Python Directory\winPython 2.7\python-2.7.10.amd64\lib\site-packages\lasagne\layers\special.pyc in __init__(self, incoming, localization_network, downsample_factor, **kwargs)
    408                  **kwargs):
    409         super(TransformerLayer, self).__init__(
--> 410             [incoming, localization_network], **kwargs)
    411         self.downsample_factor = as_tuple(downsample_factor, 2)
    412 

D:\Python Directory\winPython 2.7\python-2.7.10.amd64\lib\site-packages\lasagne\layers\base.pyc in __init__(self, incomings, name)
    246         self.input_shapes = [incoming if isinstance(incoming, tuple)
    247                              else incoming.output_shape
--> 248                              for incoming in incomings]
    249         self.input_layers = [None if isinstance(incoming, tuple)
    250                              else incoming

AttributeError: 'str' object has no attribute 'output_shape'

```

1 个答案:

答案 0 :(得分:2)

解决方案在于定义原生千层面中的图层并传递最后一层以便不学习,因为没有学习的神经网络实现可能无法识别传入属性。以上网络的修改对我有用。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addSlideTransitions();

        setContentView(R.layout.activity_main);

    }



    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    protected void addSlideTransitions()
    {
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        Slide slide = new Slide();
        slide.setDuration(1000);
        getWindow().setEnterTransition(slide);
        getWindow().setExitTransition(slide);
    }
}