brian-team / brian2

Brian is a free, open source simulator for spiking neural networks.
http://briansimulator.org
Other
932 stars 220 forks source link

Name is not defined #1122

Closed between-xtm closed 4 years ago

between-xtm commented 5 years ago

My network can train as CNN, but when I want to use brian2, there occurs a error:

NameError: name 'input_layer' is not defined

The error is: `Initializing brian2 simulator...

Loading data set from '.npz' files in D:\yaner\machine_learning\snn_toolbox-master\temp\1571226069.808398.

ERROR Brian 2 encountered an unexpected error. If you think this is bug in Brian 2, please report this issue either to the mailing list at http://groups.google.com/group/brian-development/, or to the issue tracker at https://github.com/brian-team/brian2/issues. Please include this file with debug information in your report: C:\Users\WONDER~1\AppData\Local\Temp\brian_debug_j1ayx563.log Additionally, you can also include a copy of the script that was run, available at: C:\Users\WONDER~1\AppData\Local\Temp\brian_script_bjb8gs1n.py Thanks! [brian2] Traceback (most recent call last): File "mnist_keras_brian2.py", line 169, in main(config_filepath) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\snntoolbox\bin\run.py", line 31, in main run_pipeline(config) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\snntoolbox\bin\utils.py", line 77, in run_pipeline config.get('paths', 'filename_ann')) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\snntoolbox\parsing\model_libs\keras_input_lib.py", line 144, in load get_custom_activations_dict(filepath_custom_objects)) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\saving.py", line 492, in load_wrapper return load_function(args, kwargs) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\saving.py", line 584, in load_model model = _deserialize_model(h5dict, custom_objects, compile) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\saving.py", line 274, in _deserialize_model model = model_from_config(model_config, custom_objects=custom_objects) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\saving.py", line 627, in model_from_config return deserialize(config, custom_objects=custom_objects) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\layers__init__.py", line 168, in deserialize printable_module_name='layer') File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object list(custom_objects.items()))) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\network.py", line 1075, in from_config process_node(layer, node_data) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\network.py", line 1025, in process_node layer(unpack_singleton(input_tensors), kwargs) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper return func(args, kwargs) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\base_layer.py", line 489, in call output = self.call(inputs, kwargs) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\layers\core.py", line 716, in call return self.function(inputs, **arguments) File "mnist_keras_brian2.py", line 70, in input_layer1=Lambda(lambda layer:K.expand_dims(input_layer,axis=1))(input_layer) NameError: name 'input_layer' is not defined`

And My network is:

`input_layer = Input(input_shape)

input_layer1=Lambda(lambda layer:K.expand_dims(input_layer,axis=1))(input_layer)

layer=ConvLSTM2D(filters=16,kernel_size=(5,5),strides=(2,2),activation='relu',return_sequences=True)(input_layer1) layer1=ConvLSTM2D(filters=32,kernel_size=(3,3),activation='relu',return_sequences=True)(layer)

layer2 = Flatten()(layer1)

layer3 = Dropout(0.01)(layer2)

layer4 = Dense(units=10, activation='softmax', use_bias=False)(layer3)

model = Model(inputs=input_layer, outputs=layer4)

model.compile('adam', 'categorical_crossentropy', ['accuracy'])`

Could you tell me why my brian2 cannot find the input_layer? Thanks!

mstimberg commented 5 years ago

Hi. The generic message that "Brian 2 encountered an unexpected error" will be triggered whenever code imports Brian 2 and an exception is raised, regardless of whether the problem actually has to do with Brian 2 or not (note that it says "if you think this is bug in Brian 2, ..." :smile: ). In your case, the traceback does not refer to any Brian 2 code (only code in snntoolbox and keras), therefore you should probably ask the snntoolbox project for help.

I've never used neither snntoolbox nor keras, but as a wild guess: does it maybe work if you replace:

input_layer1=Lambda(lambda layer:K.expand_dims(input_layer,axis=1))(input_layer)

by

input_layer1=Lambda(lambda layer:K.expand_dims(layer,axis=1))(input_layer)

?

between-xtm commented 5 years ago

Thank you, this problem is solved. But there is another question:

Layer weight shape (784, 10) not compatible with provided weight shape (3200, 10)

and my input shape is 28*28

The error is:

ERROR Brian 2 encountered an unexpected error. If you think this is bug in Brian 2, please report this issue either to the mailing list at http://groups.google.com/group/brian-development/, or to the issue tracker at https://github.com/brian-team/brian2/issues. Please include this file with debug information in your report: C:\Users\WONDER~1\AppData\Local\Temp\brian_debug_d2qd15d7.log Additionally, you can also include a copy of the script that was run, available at: C:\Users\WONDER~1\AppData\Local\Temp\brian_script__jk2krmx.py Thanks! [brian2] Traceback (most recent call last): File "D:\yaner\machine_learning\snn_toolbox-master\examples\mnist_keras_brian2.py", line 202, in main(config_filepath) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\snntoolbox\bin\run.py", line 31, in main run_pipeline(config) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\snntoolbox\bin\utils.py", line 92, in run_pipeline parsed_model = model_parser.build_parsed_model() File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\snntoolbox\parsing\utils.py", line 675, in build_parsed_model parsed_layers[layer['name']] = parsed_layer(layer)(inbound) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn_wrapper return func(*args, *kwargs) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\base_layer.py", line 468, in call self.set_weights(self._initial_weights) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\backend\tensorflow_backend.py", line 105, in eager_fn_wrapper out = func(args, kwargs) File "C:\Users\wonderjolie\AppData\Local\conda\conda\envs\final\lib\site-packages\keras\engine\base_layer.py", line 1126, in set_weights 'provided weight shape ' + str(w.shape)) ValueError: Layer weight shape (784, 10) not compatible with provided weight shape (3200, 10)

Thank you!

vigneswaran-chandrasekaran commented 5 years ago

Hi, I reckon that this problem is again not caused by Brian2. From the error message, I guess you're trying to assign an incorrect dimension (3200, 10) of parameters to the model's layer which expects dimension of (28*28, 10).

between-xtm commented 5 years ago

OK, I will check it again, thanks for your helping!