ValueError: Shapes must be equal rank, but are 4 and 0 for 'soft_lif_17/Select_1'


#1

I am trying to run hunse repository for image classification on my own dataset whcih have images of jpg type and 7 classes. i am trying to use keras and nengo for this.when i try to run the code it gives the following errors .pls suggest me any solution.thanks

from future import print_function

import os
os.environ['THEANO_FLAGS'] = 'device=gpu,floatX=float32'

import nengo
import numpy as np

import keras

from keras.models import Sequential
from keras.layers import (
    Dense, Dropout, Activation, Flatten, Convolution2D, AveragePooling2D)
from keras.layers.noise import GaussianNoise
from keras.utils import np_utils

import nengo
from nengo_extras.keras import (
    load_model_pair, save_model_pair, SequentialNetwork, SoftLIF)
from nengo_extras.gui import image_display_function



img_rows, img_cols = 227, 227

np.random.seed(5)


#X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
#X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
#(X_train, y_train), (X_test, y_test) = mnist.load_data()
#X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
#X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)



#X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
#X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
(X_train, y_train), (X_test, y_test), label_names = ((X_train, iy_train),(X_test, y_test),class_names)

X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols)
#X_train = X_train.reshape(-1, 227, 227,3).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols)
#X_train = X_train.astype('float32')/255 - 1
#X_test = X_test.astype('float32')/255 - 1

nb_classes = len(label_names)
#_train = X_train[:, :, 16:-16, 16:-16]
#_test = X_test[:, :, 16:-16, 16:-16]


# --- Train model
nb_epoch = 25

    # number of convolutional filters to use
nb_filters = 32
    # size of pooling area for max pooling
nb_pool = 2
    # convolution kernel size
nb_conv = 3

    # convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

kmodel = Sequential()
softlif_params = dict(
sigma=0.002, amplitude=0.063, tau_rc=0.022, tau_ref=0.002)
kmodel.add(GaussianNoise(0.1, input_shape=(227,227,3)))
#kmodel.add(Conv2D(32, (3, 3), input_shape=(227, 227, 3), padding='same'))
kmodel.add(Conv2D(nb_filters, (nb_conv, nb_conv), padding='valid'))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Convolution2D(nb_filters, (nb_conv, nb_conv)))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(AveragePooling2D(pool_size=(nb_pool, nb_pool)))
kmodel.add(Dropout(0.25))

kmodel.add(Flatten())
kmodel.add(Dense(227))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Dropout(0.5))
kmodel.add(Dense(nb_classes))
kmodel.add(Activation('softmax'))

kmodel.compile(loss='categorical_crossentropy',
               optimizer='adadelta',
               metrics=['accuracy'])

kmodel.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
           verbose=1, validation_data=(X_test, Y_test))
score = kmodel.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])

save_model_pair(kmodel, filename, overwrite=True)

the following errros are traceback

InvalidArgumentError Traceback (most recent call last)
C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
685 graph_def_version, node_def_str, input_shapes, input_tensors,
–> 686 input_tensors_as_shapes, status)
687 except errors.InvalidArgumentError as err:

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\errors_impl.py in exit(self, type_arg, value_arg, traceback_arg)
472 compat.as_text(c_api.TF_Message(self.status.status)),
–> 473 c_api.TF_GetCode(self.status.status))
474 # Delete the underlying status object from memory otherwise it stays alive

InvalidArgumentError: Shapes must be equal rank, but are 4 and 0 for ‘soft_lif_17/Select_1’ (op: ‘Select’) with input shapes: [?,?,?,?], [?,32,227,3], [].

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in ()
5 kmodel.add(Conv2D(32, (3, 3), input_shape=(227, 227, 3), padding=‘same’))
6 #kmodel.add(Conv2D(nb_filters, (nb_conv, nb_conv), padding=‘valid’))
----> 7 kmodel.add(SoftLIF(**softlif_params))
8 kmodel.add(Convolution2D(nb_filters, (nb_conv, nb_conv)))
9 kmodel.add(SoftLIF(**softlif_params))

C:\ProgramData\Anaconda34\lib\site-packages\keras\models.py in add(self, layer)
490 output_shapes=[self.outputs[0]._keras_shape])
491 else:
–> 492 output_tensor = layer(self.outputs[0])
493 if isinstance(output_tensor, list):
494 raise TypeError('All layers in a Sequential model ’

C:\ProgramData\Anaconda34\lib\site-packages\keras\engine\topology.py in call(self, inputs, **kwargs)
615
616 # Actually call the layer, collecting output(s), mask(s), and shape(s).
–> 617 output = self.call(inputs, **kwargs)
618 output_mask = self.compute_mask(inputs, previous_mask)
619

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in call(self, x, mask)
23 j = K.softplus(x / self.sigma) * self.sigma
24 r = self.amplitude / (self.tau_ref + self.tau_rc*K.log(1 + 1/j))
—> 25 return K.switch(j > 0, r, 0)
26
27 def get_config(self):

C:\ProgramData\Anaconda34\lib\site-packages\keras\backend\tensorflow_backend.py in switch(condition, then_expression, else_expression)
2833 tile_shape = tf.where(shape_diff > 0, expr_shape, tf.ones_like(expr_shape))
2834 condition = tf.tile(condition, tile_shape)
-> 2835 x = tf.where(condition, then_expression, else_expression)
2836 return x
2837

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\array_ops.py in where(condition, x, y, name)
2538 return gen_array_ops.where(condition=condition, name=name)
2539 elif x is not None and y is not None:
-> 2540 return gen_math_ops._select(condition=condition, x=x, y=y, name=name)
2541 else:
2542 raise ValueError(“x and y must both be non-None or both be None.”)

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\gen_math_ops.py in _select(condition, x, y, name)
4526 if _ctx.in_graph_mode():
4527 _, _, _op = _op_def_lib._apply_op_helper(
-> 4528 “Select”, condition=condition, t=x, e=y, name=name)
4529 _result = _op.outputs[:]
4530 _inputs_flat = _op.inputs

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
785 op = g.create_op(op_type_name, inputs, output_types, name=scope,
786 input_types=input_types, attrs=attr_protos,
–> 787 op_def=op_def)
788 return output_structure, op_def.is_stateful, op
789

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
3160 op_def=op_def)
3161 self._create_op_helper(ret, compute_shapes=compute_shapes,
-> 3162 compute_device=compute_device)
3163 return ret
3164

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in _create_op_helper(self, op, compute_shapes, compute_device)
3206 # compute_shapes argument.
3207 if op._c_op or compute_shapes: # pylint: disable=protected-access
-> 3208 set_shapes_for_outputs(op)
3209 # TODO(b/XXXX): move to Operation.init once _USE_C_API flag is removed.
3210 self._add_op(op)

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in set_shapes_for_outputs(op)
2425 return _set_shapes_for_outputs_c_api(op)
2426 else:
-> 2427 return _set_shapes_for_outputs(op)
2428
2429

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in _set_shapes_for_outputs(op)
2398 shape_func = _call_cpp_shape_fn_and_require_op
2399
-> 2400 shapes = shape_func(op)
2401 if shapes is None:
2402 raise RuntimeError(

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in call_with_requiring(op)
2328
2329 def call_with_requiring(op):
-> 2330 return call_cpp_shape_fn(op, require_shape_fn=True)
2331
2332 _call_cpp_shape_fn_and_require_op = call_with_requiring

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\common_shapes.py in call_cpp_shape_fn(op, require_shape_fn)
625 res = _call_cpp_shape_fn_impl(op, input_tensors_needed,
626 input_tensors_as_shapes_needed,
–> 627 require_shape_fn)
628 if not isinstance(res, dict):
629 # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, require_shape_fn)
689 missing_shape_fn = True
690 else:
–> 691 raise ValueError(err.message)
692
693 if missing_shape_fn:

ValueError: Shapes must be equal rank, but are 4 and 0 for ‘soft_lif_17/Select_1’ (op: ‘Select’) with input shapes: [?,?,?,?], [?,32,227,3], [].


#2

I’ve fixed this and other related errors in this branch of nengo_extras: https://github.com/nengo/nengo-extras/tree/fix-keras


#3

now some new errrors are appearing. i am using the example with out making changes in this.

AssertionError                            Traceback (most recent call last)
<ipython-input-3-573ee5607237> in <module>()
      5 with model:
      6     u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time))
----> 7     knet = SequentialNetwork(kmodel, synapse=nengo.synapses.Alpha(0.005))
      8     nengo.Connection(u, knet.input, synapse=None)
      9 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in __init__(self, model, synapse, lif_type, **kwargs)
     82         self.add_data_layer(np.prod(model.input_shape[1:]))
     83         for layer in model.layers:
---> 84             self._add_layer(layer)
     85 
     86     def _add_layer(self, layer):

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in _add_layer(self, layer)
    102         for cls in type(layer).__mro__:
    103             if cls in layer_adder:
--> 104                 return layer_adder[cls](layer)
    105 
    106         raise NotImplementedError("Cannot build layer type %r" %

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in _add_conv2d_layer(self, layer)
    127         nf, nc2, ni, nj = filters.shape
    128         filters = filters[..., ::-1, ::-1]  # flip
--> 129         assert nc == nc2, "Filter channels must match input channels"
    130 
    131         if layer.padding == 'valid':

AssertionError: Filter channels must match input channels

#4

when i run my own dataset which is composed of 227X227X3 images .it gives the following errors. i am posting the code snippets below.kindly suggest a solution to these erros.

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import (Dense, Dropout, Activation, Flatten, Convolution2D, AveragePooling2D)
from keras.layers.noise import GaussianNoise
from keras.utils import np_utils
filename = 'myfile'
kmodel = Sequential()
softlif_params = dict(
    sigma=0.002, amplitude=0.063, tau_rc=0.022, tau_ref=0.002)
kmodel.add(GaussianNoise(0.1, input_shape=(3,227,227)))
kmodel.add(Convolution2D(32, (3, 3),input_shape=(3,227, 227), padding='valid'))
kmodel.add(Convolution2D(nb_filters, (nb_conv, nb_conv),padding='valid'))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Convolution2D(nb_filters, (nb_conv, nb_conv)))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(AveragePooling2D(pool_size=(nb_pool, nb_pool)))
kmodel.add(Dropout(0.25))
kmodel.add(Flatten())
kmodel.add(Dense(3))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Dropout(0.5))
kmodel.add(Dense(nb_classes))
kmodel.add(Activation('softmax'))

kmodel.compile(loss='categorical_crossentropy',
                   optimizer='adadelta',
                   metrics=['accuracy'])



kmodel.fit(X_train, iy_train, validation_data=(X_train, iy_train), epochs=3, batch_size=1)
# Final evaluation of the model
scores = kmodel.evaluate(X_train, iy_train, verbose=2)
print("Accuracy: %.2f%%" % (scores[1]*100))

#kmodel.fit(X_train, y_train, epochs=nb_epoch,
               #verbose=1, validation_data=(X_test,y_test))
score = kmodel.evaluate(X_train, iy_train, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])

save_model_pair(kmodel, filename, overwrite=True)


# --- Run model in Nengo
presentation_time = 0.2

model = nengo.Network()
with model:
    u = nengo.Node(nengo.processes.PresentInput(X_train, presentation_time))
    knet = SequentialNetwork(kmodel,synapse=nengo.synapses.Alpha(0.001))
    nengo.Connection(u, knet.input, synapse=None)

    input_p = nengo.Probe(u)
    output_p = nengo.Probe(knet.output)

    # --- image display
    image_shape = model.input_shape[1:]
    display_f = image_display_function(image_shape)
    display_node = nengo.Node(display_f, size_in=u.size_out)
    nengo.Connection(u, display_node, synapse=None)


the errror trace is below.
    ValueError                                Traceback (most recent call last)
    <ipython-input-16-0a86ac996899> in <module>()
          5 with model:
          6     u = nengo.Node(nengo.processes.PresentInput(X_train, presentation_time))
    ----> 7     knet = SequentialNetwork(kmodel,synapse=nengo.synapses.Alpha(0.001))
          8     nengo.Connection(u, knet.input, synapse=None)
          9 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in __init__(self, model, synapse, lif_type, **kwargs)
     82         self.add_data_layer(np.prod(model.input_shape[1:]))
     83         for layer in model.layers:
---> 84             self._add_layer(layer)
     85 
     86     def _add_layer(self, layer):

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in _add_layer(self, layer)
    102         for cls in type(layer).__mro__:
    103             if cls in layer_adder:
--> 104                 return layer_adder[cls](layer)
    105 
    106         raise NotImplementedError("Cannot build layer type %r" %

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in _add_conv2d_layer(self, layer)
    137 
    138         return self.add_conv_layer(shape_in, filters, biases, strides=strides,
--> 139                                    padding=padding, name=layer.name)
    140 
    141     def _add_pool2d_layer(self, layer, kind=None):

C:\ProgramData\Anaconda34\lib\site-packages\nengo\utils\magic.py in __call__(self, *args, **kwargs)
    179             else:
    180                 return self.wrapper(
--> 181                     self.__wrapped__, self.instance, args, kwargs)
    182         else:
    183             instance = getattr(self.__wrapped__, '__self__', None)

C:\ProgramData\Anaconda34\lib\site-packages\nengo\utils\network.py in with_self(method, network, args, kwargs)
     26     """
     27     with network:
---> 28         return method(*args, **kwargs)
     29 
     30 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\deepnetworks.py in add_conv_layer(self, input_shape, filters, biases, inputs, name, **kwargs)
     63                        name=None, **kwargs):
     64         kwargs.setdefault('label', name)
---> 65         layer = ConvLayer(input_shape, filters, biases, **kwargs)
     66         return self.add_layer(layer, inputs=inputs, name=name)
     67 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\deepnetworks.py in __init__(self, input_shape, filters, biases, strides, padding, **kwargs)
    511         biases = np.array(biases)  # copy
    512         p = Conv2d(input_shape, filters, biases,
--> 513                    strides=strides, padding=padding)
    514         super(ConvLayer, self).__init__(p, **kwargs)
    515 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\convnet.py in __init__(self, shape_in, filters, biases, strides, padding)
     58         if not all(s % 2 == 1 for s in self.filters.shape[-2:]):
     59             raise ValueError("Filter shapes must be odd (got %r)"
---> 60                              % (self.filters.shape[-2:],))
     61 
     62         self.strides = strides if is_iterable(strides) else [strides] * 2

ValueError: Filter shapes must be odd (got (3, 32))

#5

Things are now working for mnist_spiking_cnn.py in the fix-keras branch (https://github.com/nengo/nengo-extras/pull/71).

Basically, there have been a lot of changes between old and new Keras. Also, I had never tested with Tensorflow, and that introduced some problems. But it should all be okay now.


#6

i installed new nengo_extras and try to run the Mnist_spiking_cnn example after your suggestion but got the following errors. i am using the keras version =2.1.4 and theano version=1.0.1
from future import print_function

import os
os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float32'

import nengo
import nengo_ocl
import numpy as np

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import (
    Dense, Dropout, Activation, Flatten, Convolution2D, AveragePooling2D)
from keras.layers.noise import GaussianNoise
from keras.utils import np_utils

import nengo
from nengo_extras.keras import (
    load_model_pair, save_model_pair, SequentialNetwork, SoftLIF)
from nengo_extras.gui import image_display_function

np.random.seed(1)
filename = 'mnist_spiking_cnn'

# --- Load data
img_rows, img_cols = 28, 28
nb_classes = 10

# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')/128 - 1
X_test = X_test.astype('float32')/128 - 1

# --- Train model
if not os.path.exists(filename + '.h5'):
    batch_size = 128
    nb_epoch = 6

    # number of convolutional filters to use
    nb_filters = 32
    # size of pooling area for max pooling
    nb_pool = 2
    # convolution kernel size
    nb_conv = 3

    # convert class vectors to binary class matrices
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)

    kmodel = Sequential()

    softlif_params = dict(
        sigma=0.002, amplitude=0.063, tau_rc=0.022, tau_ref=0.002)

    kmodel.add(GaussianNoise(0.1, input_shape=(1, img_rows, img_cols)))
    kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid'))
    kmodel.add(SoftLIF(**softlif_params))
    kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv))
    kmodel.add(SoftLIF(**softlif_params))
    kmodel.add(AveragePooling2D(pool_size=(nb_pool, nb_pool)))
    kmodel.add(Dropout(0.25))

    kmodel.add(Flatten())
    kmodel.add(Dense(128))
    kmodel.add(SoftLIF(**softlif_params))
    kmodel.add(Dropout(0.5))
    kmodel.add(Dense(nb_classes))
    kmodel.add(Activation('softmax'))

    kmodel.compile(loss='categorical_crossentropy',
                   optimizer='adadelta',
                   metrics=['accuracy'])

    kmodel.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
               verbose=1, validation_data=(X_test, Y_test))
    score = kmodel.evaluate(X_test, Y_test, verbose=0)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])

    save_model_pair(kmodel, filename, overwrite=True)

else:
    kmodel = load_model_pair(filename)


# --- Run model in Nengo
presentation_time = 0.2

model = nengo.Network()
with model:
    u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time))
    knet = SequentialNetwork(kmodel, synapse=nengo.synapses.Alpha(0.005))
    nengo.Connection(u, knet.input, synapse=None)

    input_p = nengo.Probe(u)
    output_p = nengo.Probe(knet.output)

    # --- image display
    image_shape = kmodel.input_shape[1:]
    display_f = image_display_function(image_shape)
    display_node = nengo.Node(display_f, size_in=u.size_out)
    nengo.Connection(u, display_node, synapse=None)

    # --- output spa display
    vocab_names = ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR',
                   'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE']
    vocab_vectors = np.eye(len(vocab_names))

    vocab = nengo.spa.Vocabulary(len(vocab_names))
    for name, vector in zip(vocab_names, vocab_vectors):
        vocab.add(name, vector)

    config = nengo.Config(nengo.Ensemble)
    config[nengo.Ensemble].neuron_type = nengo.Direct()
    with config:
        output = nengo.spa.State(len(vocab_names), subdimensions=10, vocab=vocab)
    nengo.Connection(knet.output, output.input)


nb_presentations = 100

if 0:
    # run ANN in Theano
    os.environ['THEANO_FLAGS'] = 'device=gpu,floatX=float32'
    Q = knet.theano_compute(X_test[:nb_presentations])
    Z = np.argmax(Q, axis=-1) == y_test[:nb_presentations]
    print("ANN accuracy (%d examples): %0.3f" % (nb_presentations, Z.mean()))


with nengo_ocl.Simulator(model) as sim:
    sim.run(nb_presentations * presentation_time)

nt = int(presentation_time / sim.dt)
blocks = sim.data[output_p].reshape(nb_presentations, nt, nb_classes)
choices = np.argmax(blocks[:, -20:, :].mean(axis=1), axis=1)
accuracy = (choices == y_test[:nb_presentations]).mean()
print('Spiking accuracy (%d examples): %0.3f' % (nb_presentations, accuracy))

Error trace

ValueError                                Traceback (most recent call last)
<ipython-input-2-8b2c17b3a311> in <module>()
     29 
     30     kmodel.add(Flatten())
---> 31     kmodel.add(Dense(128))
     32     kmodel.add(SoftLIF(**softlif_params))
     33     kmodel.add(Dropout(0.5))

C:\ProgramData\Anaconda34\lib\site-packages\keras\models.py in add(self, layer)
    490                           output_shapes=[self.outputs[0]._keras_shape])
    491         else:
--> 492             output_tensor = layer(self.outputs[0])
    493             if isinstance(output_tensor, list):
    494                 raise TypeError('All layers in a Sequential model '

C:\ProgramData\Anaconda34\lib\site-packages\keras\engine\topology.py in __call__(self, inputs, **kwargs)
    588                                          '`layer.build(batch_input_shape)`')
    589                 if len(input_shapes) == 1:
--> 590                     self.build(input_shapes[0])
    591                 else:
    592                     self.build(input_shapes)

C:\ProgramData\Anaconda34\lib\site-packages\keras\layers\core.py in build(self, input_shape)
    840                                       name='kernel',
    841                                       regularizer=self.kernel_regularizer,
--> 842                                       constraint=self.kernel_constraint)
    843         if self.use_bias:
    844             self.bias = self.add_weight(shape=(self.units,),

C:\ProgramData\Anaconda34\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name +
     90                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

C:\ProgramData\Anaconda34\lib\site-packages\keras\engine\topology.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
    409         if dtype is None:
    410             dtype = K.floatx()
--> 411         weight = K.variable(initializer(shape),
    412                             dtype=dtype,
    413                             name=name,

C:\ProgramData\Anaconda34\lib\site-packages\keras\initializers.py in __call__(self, shape, dtype)
    215             limit = np.sqrt(3. * scale)
    216             return K.random_uniform(shape, -limit, limit,
--> 217                                     dtype=dtype, seed=self.seed)
    218 
    219     def get_config(self):

C:\ProgramData\Anaconda34\lib\site-packages\keras\backend\theano_backend.py in random_uniform(shape, minval, maxval, dtype, seed)
   2255         seed = np.random.randint(1, 10e6)
   2256     rng = RandomStreams(seed=seed)
-> 2257     return rng.uniform(shape, low=minval, high=maxval, dtype=dtype)
   2258 
   2259 

C:\ProgramData\Anaconda34\lib\site-packages\theano\sandbox\rng_mrg.py in uniform(self, size, low, high, ndim, dtype, nstreams, **kwargs)
    860                 raise ValueError(
    861                     "The specified size contains a dimension with value <= 0",
--> 862                     size)
    863 
    864         else:

ValueError: ('The specified size contains a dimension with value <= 0', (-768, 128))

#7

similarly when i am trying to replicate the above code on my own dataset it shows the following errors please suggest me the solution for these . the code snippets is

import os
os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float32,optimizer=fast_compile'
#THEANO_FLAGS=optimizer=fast_compile

import nengo
import numpy as np

import keras

from keras.models import Sequential
from keras.layers import (
    Dense, Dropout, Activation, Flatten, Convolution2D, AveragePooling2D)
from keras.layers.noise import GaussianNoise
from keras.utils import np_utils
from nengo_extras.cuda_convnet import CudaConvnetNetwork
import nengo
from nengo_extras.keras import (
    load_model_pair, save_model_pair, SequentialNetwork, SoftLIF)
from nengo_extras.gui import image_display_function



img_rows, img_cols = 227, 227

np.random.seed(2)


#X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
#X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
#(X_train, y_train), (X_test, y_test) = mnist.load_data()
#X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
#X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)

(X_train, y_train), (X_test, y_test), label_names = ((X_train, iy_train),(X_test, y_test),class_names)

X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols)

X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols)
X_train = X_train.astype('uint8')/255 - 1
X_test = X_test.astype('uint8')/255 - 1

nb_classes = len(label_names)

# --- Train model
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test, nb_classes)

nb_epoch = 2

    # number of convolutional filters to use
nb_filters = 227
    # size of pooling area for max pooling
nb_pool = 2
    # convolution kernel size
nb_conv = 3

#print(X_train.shape)
#print(X_test.shape)
print(Y_train.shape)
print(y_test.shape)
print(nb_classes)

filename = 'myfile'
kmodel = Sequential()
softlif_params = dict(
    sigma=0.002, amplitude=0.063, tau_rc=0.022, tau_ref=0.002)
kmodel.add(GaussianNoise(0.1, input_shape=(3,227,227)))
kmodel.add(Convolution2D(32, (3, 3),input_shape=(3,227, 227), padding='valid'))
kmodel.add(Convolution2D(nb_filters, (nb_conv, nb_conv),padding='valid'))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Convolution2D(nb_filters, (nb_conv, nb_conv)))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(AveragePooling2D(pool_size=(nb_pool, nb_pool)))
kmodel.add(Dropout(0.25))
kmodel.add(Flatten())
kmodel.add(Dense(3))
kmodel.add(SoftLIF(**softlif_params))
kmodel.add(Dropout(0.5))
kmodel.add(Dense(nb_classes))
kmodel.add(Activation('softmax'))

kmodel.compile(loss='categorical_crossentropy',
                   optimizer='adadelta',
                   metrics=['accuracy'])



kmodel.fit(X_train, iy_train, validation_data=(X_test, Y_test), epochs=3, batch_size=1)
# Final evaluation of the model
scores = kmodel.evaluate(X_test, Y_test, verbose=2)
print("Accuracy: %.2f%%" % (scores[1]*100))

#kmodel.fit(X_train, y_train, epochs=nb_epoch,
               #verbose=1, validation_data=(X_test,y_test))
#score = kmodel.evaluate(X_train, iy_train, verbose=0)
print('Test score:', scores[0])
print('Test accuracy:', scores[1])

save_model_pair(kmodel, filename, overwrite=True)

presentation_time = 0.2

model = nengo.Network()
with model:
    u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time))
    knet = SequentialNetwork(kmodel,synapse=nengo.synapses.Alpha(0.001))
    nengo.Connection(u, knet.input, synapse=None)

    input_p = nengo.Probe(u)
    output_p = nengo.Probe(knet.output)

    # --- image display
    image_shape = model.input_shape[1:]
    display_f = image_display_function(image_shape)
    display_node = nengo.Node(display_f, size_in=u.size_out)
    nengo.Connection(u, display_node, synapse=None)

the error trace is given below

ValueError                                Traceback (most recent call last)
<ipython-input-16-0d02fae250db> in <module>()
      5 with model:
      6     u = nengo.Node(nengo.processes.PresentInput(X_test, presentation_time))
----> 7     knet = SequentialNetwork(kmodel,synapse=nengo.synapses.Alpha(0.001))
      8     nengo.Connection(u, knet.input, synapse=None)
      9 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in __init__(self, model, synapse, lif_type, **kwargs)
     82         self.add_data_layer(np.prod(model.input_shape[1:]))
     83         for layer in model.layers:
---> 84             self._add_layer(layer)
     85 
     86     def _add_layer(self, layer):

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in _add_layer(self, layer)
    102         for cls in type(layer).__mro__:
    103             if cls in layer_adder:
--> 104                 return layer_adder[cls](layer)
    105 
    106         raise NotImplementedError("Cannot build layer type %r" %

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\keras.py in _add_conv2d_layer(self, layer)
    137 
    138         return self.add_conv_layer(shape_in, filters, biases, strides=strides,
--> 139                                    padding=padding, name=layer.name)
    140 
    141     def _add_pool2d_layer(self, layer, kind=None):

C:\ProgramData\Anaconda34\lib\site-packages\nengo\utils\magic.py in __call__(self, *args, **kwargs)
    179             else:
    180                 return self.wrapper(
--> 181                     self.__wrapped__, self.instance, args, kwargs)
    182         else:
    183             instance = getattr(self.__wrapped__, '__self__', None)

C:\ProgramData\Anaconda34\lib\site-packages\nengo\utils\network.py in with_self(method, network, args, kwargs)
     26     """
     27     with network:
---> 28         return method(*args, **kwargs)
     29 
     30 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\deepnetworks.py in add_conv_layer(self, input_shape, filters, biases, inputs, name, **kwargs)
     63                        name=None, **kwargs):
     64         kwargs.setdefault('label', name)
---> 65         layer = ConvLayer(input_shape, filters, biases, **kwargs)
     66         return self.add_layer(layer, inputs=inputs, name=name)
     67 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\deepnetworks.py in __init__(self, input_shape, filters, biases, strides, padding, **kwargs)
    511         biases = np.array(biases)  # copy
    512         p = Conv2d(input_shape, filters, biases,
--> 513                    strides=strides, padding=padding)
    514         super(ConvLayer, self).__init__(p, **kwargs)
    515 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\convnet.py in __init__(self, shape_in, filters, biases, strides, padding)
     58         if not all(s % 2 == 1 for s in self.filters.shape[-2:]):
     59             raise ValueError("Filter shapes must be odd (got %r)"
---> 60                              % (self.filters.shape[-2:],))
     61 
     62         self.strides = strides if is_iterable(strides) else [strides] * 2

ValueError: Filter shapes must be odd (got (3, 32))

#8
now when i run the Mnist example i got the following errors 
AttributeError                            Traceback (most recent call last)
<ipython-input-17-233213b62dea> in <module>()
     48 blocks = sim.data[output_p].reshape(n_presentations, nt, n_classes)
     49 choices = np.argmax(blocks[:, -20:, :].mean(axis=1), axis=1)
---> 50 accuracy = (choices == y_test[:n_presentations]).mean()
     51 print('Spiking accuracy (%d examples): %0.3f' % (n_presentations, accuracy))

    AttributeError: 'bool' object has no attribute 'mean'

#9

similarly the following errors are also raising .please suggest me the solution

if 1:
    # run ANN in Theano
    os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float64'
    Q = knet.theano_compute(X_test[:n_presentations])
    Z = np.argmax(Q, axis=-1) == y_test[:n_presentations]
    print("ANN accuracy (%d examples): %0.3f" % (n_presentations, Z.mean()))


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-22-8f4b54ea11b1> in <module>()
      2     # run ANN in Theano
      3     os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float64'
----> 4     Q = knet.theano_compute(X_test[:n_presentations])
      5     Z = np.argmax(Q, axis=-1) == y_test[:n_presentations]
      6     print("ANN accuracy (%d examples): %0.3f" % (n_presentations, Z.mean()))

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\deepnetworks.py in theano_compute(self, x, output_layer, batch_size)
    142         import theano.tensor as tt
    143         sx = tt.matrix(name='x')
--> 144         sy = self.theano(sx, output_layer=output_layer)
    145         f = theano.function([sx], sy, allow_input_downcast=True)
    146 

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\deepnetworks.py in theano(self, sx, output_layer)
    134         sy = sx
    135         for layer in self.layers_to(output_layer):
--> 136             sy = layer.theano(sy)
    137 
    138         return sy

C:\ProgramData\Anaconda34\lib\site-packages\nengo_extras\deepnetworks.py in theano(self, x)
    534             x = tt.set_subtensor(xx[:, :, :nxi, :nxj], x)
    535         else:
--> 536             assert nxi == nxi2 and nxj == nxj2
    537 
    538         y = tt.nnet.conv2d(x, filters,

AssertionError: