Giter Club home page Giter Club logo

nengo-extras's Introduction

Nengo extras

Extra utilities and add-ons for Nengo.

This repository contains utilities that occupy a liminal space not quite generic enough for inclusion in Nengo, but useful enough that they should be publicly accessible.

Some of these utilities may eventually migrate to Nengo, and others may be split off into their own separate repositories.

Installation

To install Nengo extras, we recommend using pip.

pip install nengo-extras

Usage

Example notebooks can be found in the docs/examples directory.

For a listing of the contents of this repository, and information on how to use it, see the full documentation.

Development

To run the unit tests:

pytest nengo_extras [--plots]

To run the static checks:

.ci/static.sh run

To build the documentation:

sphinx-build docs docs/_build

nengo-extras's People

Contributors

arvoelke avatar drasmuss avatar hunse avatar jgosmann avatar seanny123 avatar tbekolay avatar tcstewar avatar xchoo avatar youssefzaky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nengo-extras's Issues

Run slow tests regularly

One of our TravisCI tests could run all the slow tests, so that they get run sometimes. Or maybe just run them before release. I haven't actually looked at how long they all take.

Examples format

Should all examples be in the form of a Jupyter Notebook for the sake of simplicity? Currently, of the examples that exist in this repository, there are Nengo GUI, pure Nengo and Jupyter Notebook implementations of the same example.

Hard to probe CudaConvnet neural layers

The current cuda_convnet.py code does not store a reference to the nengo.Ensemble layers, making probing these layers a pain (had to go figure out what the label names are, and iterate through the network to find them)

from nengo_extras import SoftLIFRate failed

trying to import softlifrate from nengo but failed i am using ubuntu 12.04

import nengo
nengo.log(level='info')
import nengo_extras

ImportErrorTraceback (most recent call last)
in ()
6 nengo.log(level='info')
7 import nengo_extras
----> 8 from nengo_extras import SoftLIFRate

ImportError: cannot import name SoftLIFRate

ImportErrorTraceback (most recent call last)
in ()
6 nengo.log(level='info')
7 import nengo_extras
----> 8 from nengo_extras import SoftLIFRate

ImportError: cannot import name SoftLIFRate

Make pip release

A pip release would make it easier to declare nengo_extras as a dependcy in other projects/models. Currently, it has to be included as dependency link which requires the additional option --process-dependency-links to pip. Furthermore, this option has been deprecated and will be removed in a future pip release.

Tools for saving and loading decoders

In nengo/nengo#649 and nengo/nengo#608 (and maybe other places), there have been requests for ways to save and load decoders. There are two common use cases:

  1. you've computed decoders in some weird way and would like to use those instead

  2. you're using a learning rule and would like to start the learning rule off with the decoders from the end point of a previous run.

Use case 1 can usually be handled by doing nengo.Connection(a.neurons, b, transform=decoders). However, this doesn't work everywhere -- for example, nengo_spinnaker doesn't allow Connections from .neurons. Use case 2 is even more problematic -- there's currently no way to seed the start of a learning rule with anything other than the result of some Solver.

The workaround that a few people have implemented is to define a Solver that just returns whatever matrix you've explicitly told it to:

class Explicit(nengo.solvers.Solver):
    def __init__(self, value, weights=False):
        super(Explicit, self).__init__(weights=weights)
        self.value = value
            
    def __call__(self, A, Y, rng=None, E=None):
        return self.value, {}

This is quite handy for use case 1, and should at least be put into nengo_extras (and maybe even into core nengo).

This also forms the good basis for a solution to use case 2, and indeed it's not too bad to explicitly implement use case 2 with this Explicit solver:

model = nengo.Network(seed=1)
with model:
    stim = nengo.Node(lambda t: np.sin(t*np.pi*2))
    a = nengo.Ensemble(100, 1)
    b = nengo.Ensemble(50, 1)
    
    filename = 't2.npy'
    try:
        value = np.load(filename)
    except IOError:
        value = np.zeros((100, 1))
    
    c = nengo.Connection(a, b, learning_rule_type=nengo.PES(), solver=Explicit(value))
    p_c = nengo.Probe(c, 'weights', sample_every=1.0)
    
    nengo.Connection(stim, c.learning_rule, transform=-1)
    nengo.Connection(b, c.learning_rule, transform=1)
    nengo.Connection(stim, a)    
    
sim = nengo.Simulator(model)
sim.run(3)

np.save(filename, sim.data[p_c][-1].T)

But that's rather ugly. You have to open the file, handle the initial case when the file doesn't exist, create a probe, and save the data at the end (remembering to take the transpose). So let's make a helper for this:

# loads a decoder from a file, defaulting to zero if it doesn't exist
class LoadFrom(nengo.solvers.Solver):
    def __init__(self, filename, weights=False):
        super(LoadFrom, self).__init__(weights=weights)
        self.filename = filename
            
    def __call__(self, A, Y, rng=None, E=None):
        if self.weights:
            shape = (A.shape[1], E.shape[1])
        else:
            shape = (A.shape[1], Y.shape[1])
            
        try:
            value = np.load(self.filename)
            assert value.shape == shape
        except IOError:
            value = np.zeros(shape)
        return value, {}

# helper to create the LoadFrom solver and the needed probe and do the saving
class WeightSaver(object):
    def __init__(self, connection, filename, sample_every=1.0, weights=False):
        assert isinstance(connection.pre, nengo.Ensemble)
        if not filename.endswith('.npy'):
            filename = filename + '.npy'
        self.filename = filename
        connection.solver = LoadFrom(self.filename, weights=weights)
        self.probe = nengo.Probe(connection, 'weights', sample_every=sample_every)
        self.connection = connection
    def save(self, sim):
        np.save(self.filename, sim.data[self.probe][-1].T)

In order to use this, we can do something like this, only needing to add 2 lines to the whole thing:

model = nengo.Network(seed=1)
with model:
    stim = nengo.Node(lambda t: np.sin(t*np.pi*2))
    a = nengo.Ensemble(100, 1)
    b = nengo.Ensemble(50, 1)
    c = nengo.Connection(a, b, learning_rule_type=nengo.PES())
    nengo.Connection(stim, c.learning_rule, transform=-1)
    nengo.Connection(b, c.learning_rule, transform=1)
    nengo.Connection(stim, a)    
    
    ws = WeightSaver(c, 'my_weights')   # add this line

sim = nengo.Simulator(model)
sim.run(3)
ws.save(sim)   # and add this line when you're done

One interesting feature of this approach is that it loads the file at build time. I think that's what we usually want....

Deep learning repo organization

How should we arrange the deep learning repo? (i was going to do some deep learning stuff, but can’t install/import nengo_lasagne cuz it’s not set up correctly atm).

The options seem to be that we can either split it into two (or more?) or keep it all together.

I kinda like the idea of having nengo_deeplearning being the thing we use to stick deep nets in nengo (i.e. lasagne). I'd be fine if the other methods stay there as well. The other methods in there now include hessian free code from dan, and eric's supporting functions for things like convolution, etc.

Add fMRI generator

Terry used the code below in Nengo 1.4 to generate fMRI signals. It should be converted to Nengo 2.0. The basic flow of the code, is to toss a probe on the spiking output, and then multiply the spiking output by the absolute value of the connection weight matrix (i.e. to get the total amount of neurotransmitter used). For the hemodynamic function, just take it from this ACT-R paper.

class FMRI(nef.SimpleNode):
    def __init__(self,name,filename,ensembles,terminations):
        self.spikes=0
        self.synapses=0
        nef.SimpleNode.__init__(self,name)
        self.ensembles=ensembles
        self.encoders={}
        for t in terminations:
            while hasattr(t,'wrappedTermination'):
                t=t.wrappedTermination
            self.encoders[t]=numeric.array(t.node.encoders).__abs__()


        self.filename=filename

    def tick(self):
        self.spikes=0
        for n in self.ensembles:
            total=0
            for v in n.getOrigin('AXON').getValues().getValues():
                total+=v
            self.spikes+=total

        self.synapses=0
        for t,encoders in self.encoders.items():
            x=t.output
            enc=self.encoders[t]
            self.synapses+=sum(numeric.dot(enc,x))

        f=file(self.filename,'a')
        f.write('%g,%g,%g\n'%(self.t_start,self.spikes,self.synapses))
        f.close()

    def origin_spikes(self):
        return [self.spikes]
    def origin_synpases(self):
        return [self.synapses]

I figured I'd make an issue for this, so people could have access to this code.

Adding MemBlock

I want to add Xuan's MemBlock network (the thing that was kind of like a flip-flop) that he used in his Master's Thesis to this repository. Is it okay to add a networks.py file and just shove everything in there?

Improved accuracy in higher dimensions with alternate intercept distribution

So, Aaron's talk reminded me about something I'd wanted to look at ages ago: in high dimensions, it might not make sense to have a uniform distribution of intercepts. A neuron with an intercept of 0.8 in 16-dimensions will only fire for a very small proportion of points in the 16-D space (0.00196%, as it turns out). And this also means a neuron with an intercept of -0.8 will fire for 99.998% of the space. This means that those 40% of neurons (and likely many more) are kind of useless.

So, I finally sat down an analyzed this and figured out the math of what the intercept distribution should be such that the proportion of the space for which a neuron is active is a uniform distribution. It turns out the transformation is

sqrt(1-betaincinv((d+1)/2, 1/2, x+1))

(where x is the original intercept, uniformly distributed between -1 and 1, and d is the dimensionality)

I've got the derivation here: https://github.com/tcstewar/testing_notebooks/blob/master/Intercept%20Distribution%20.ipynb along with some analysis showing that this makes a pretty big improvement in decoder accuracy for higher dimensions. Oddly enough, it does make decoding a constant function slightly worse, and it seems to make 2 and 4-dimensional linear functions a very very small amount worse. The graphs below show the decoder error for various functions (constant (1), linear (x_i), squaring (x_i^2), and other quadratics (x_i * y_i) both with and without this system. The green bars are with this system.

For a constant function, we actually get worse with this method.
image

For a linear function, we get a touch worse up until D=4, and then we get better
image

For a squaring function, we get an improvement everywhere
image

And same for the other pairwise quadratics
image

The implementation of this ends up being fairly compact, but it does rely on scipy:

class AreaIntercepts(nengo.dists.Distribution):
    dimensions = nengo.params.NumberParam('dimensions')
    base = nengo.dists.DistributionParam('base')
    
    def __init__(self, dimensions, base=nengo.dists.Uniform(-1, 1)):
        super(AreaIntercepts, self).__init__()
        self.dimensions = dimensions
        self.base = base
        
    def __repr(self):
        return "AreaIntercepts(dimensions=%r, base=%r)" % (self.dimensions, self.base)
    
    def transform(self, x):
        sign = 1
        if x > 0:
            x = -x
            sign = -1
        return sign * np.sqrt(1-scipy.special.betaincinv((self.dimensions+1)/2.0, 0.5, x+1))
    
    def sample(self, n, d=None, rng=np.random):
        s = self.base.sample(n=n, d=d, rng=rng)
        for i in range(len(s)):
            s[i] = self.transform(s[i])
        return s

This feels like something that might help out SPA models a fair bit....

Update Params

ShapeParam as defined in convnet.py is now in Nengo, so we can use that.

DeltaRuleFunctionParam in learning_rules.py uses the validate method, which is now obsolete. This needs to change to coerce.

MemBlock

In Spaun, @xchoo uses a network which is essentially a neural flip-flop. This should be moved into this repository while contrasting the difference between an integrator, an input gated memory and the MemBlock.

Frequency controlled oscillator

This is used a lot in control systems. Would adding this as a network to this repository be acceptable? I'm assuming it shouldn't go into core nengo.

Camera process issues

There are a few issues with the Camera process.

  • I was unable to get it running properly, but I figured it was an environment thing. A forum poster had the same issue. Dan suggested using OpenCV to get frames from the camera, and I suspect that OpenCV will be better supported than vext.

  • I just spent a day figuring out why I was getting extra logging messages printed to my console. Turns out it's some oddness with how vext does logging -- somehow just by having vext installed, doing python -c "import logging; print(logging.root.handlers)" shows a stream handler installed, so the only way around it is to detect it and remove the handler later. It would be nice if we could do camera input without using vext due to this annoying and surprising behavior.

Cannot be imported without tkinter

The nengo-extras import fails if Python is not installed with tkinter. Can we fix this and have a release quickly? I'd like to use nengo-extras in my tutorial on Friday, but I am afraid that participants might run into this problem without any quick way to fix it due to a variety environments.

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-170-cae318af19f6> in <module>()
----> 1 import nengo_extras

~/Library/Python/3.6/lib/python/site-packages/nengo_extras/__init__.py in <module>()
      5 from .convnet import Conv2d, Pool2d
      6 from .neurons import FastLIF, SoftLIFRate
----> 7 from . import (
      8     camera, data, dists, graphviz, gui, networks, neurons, probe, vision)
      9 

~/Library/Python/3.6/lib/python/site-packages/nengo_extras/data.py in <module>()
      9 import numpy as np
     10 
---> 11 from .compat import pickle_load_bytes, urlretrieve
     12 
     13 

~/Library/Python/3.6/lib/python/site-packages/nengo_extras/compat.py in <module>()
      9     from io import StringIO
     10     from urllib.request import urlretrieve
---> 11     import tkinter
     12 
     13 

/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py in <module>()
     34 import sys
     35 
---> 36 import _tkinter # If this fails your Python may not be configured for Tk
     37 TclError = _tkinter.TclError
     38 from tkinter.constants import *

ModuleNotFoundError: No module named '_tkinter'

CudaConvnetNetwork on PyTorch model

I'm trying to convert a pretrained PyTorch GAN model (generator) to spiking format using Nengo and NengoDL. I'm following the "CIFAR-10 classifier with a spiking CNN" tutorial from https://www.nengo.ai/nengo-extras/v0.5.0/examples/cuda_convnet/cifar10_spiking_cnn.html where I am using my model instead.

After running this line of code:

ccnet = CudaConvnetNetwork(netG_A2B_pkl, synapse=nengo.synapses.Alpha(0.005))

I get this error:

TypeError: 'Generator' object is not subscriptable

I'm using the netG_A2B model from here: https://github.com/yz-wang/Cycle-SNSPGAN

Any help or recommendations will be appreciated!

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

i am trying to run CNN_Spiking from nengo on my own dataset which is composed of jpg images. i made neccessary changes in code according to the size of my dataset images and run following code but got the follwoing error.pls suggest me the solution.

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)
X_train = X_train[:, :, 16:-16, 16:-16]
X_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=(3, 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(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 error appears
C:\ProgramData\Anaconda34\lib\site-packages\ipykernel_launcher.py:71: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(32, (3, 3), padding="valid")

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_7/Select_1' (op: 'Select') with input shapes: [?,?,?,?], [?,32,225,1], [].

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in ()
70 #kmodel.add(GaussianNoise(0.1, input_shape=(3,3, img_rows, img_cols)))
71 kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid'))
---> 72 kmodel.add(SoftLIF(**softlif_params))
73 kmodel.add(Convolution2D(nb_filters, nb_conv, nb_conv))
74 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_7/Select_1' (op: 'Select') with input shapes: [?,?,?,?], [?,32,225,1], [].

Conv2D' object has no attribute 'subsample

i am trying to run the Mnist_CNN spiking code but got the errors "Conv2D' object has no attribute 'subsample" .Dont know how to rectify this error. i am using keras version2.X with theano latest version and python3.6(i have update the the nengo extras after its update released but same below error exist)
if not os.path.exists(filename + '.h5'):
batch_size = 128
nb_epoch = 1

    # 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)
   # model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(1,28,28), data_format='channels_first'))
    kmodel.add(GaussianNoise(0.1, input_shape=(img_rows, img_cols,1)))
    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(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)
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)

A```
ttributeError Traceback (most recent call last)
in ()
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

~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in init(self, model, synapse, lif_type, **kwargs)
79 self.add_data_layer(np.prod(model.input_shape[1:]))
80 for layer in model.layers:
---> 81 self._add_layer(layer)
82
83 def _add_layer(self, layer):

~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in _add_layer(self, layer)
99 for cls in type(layer).mro:
100 if cls in layer_adder:
--> 101 return layer_addercls
102
103 raise NotImplementedError("Cannot build layer type %r" %

~\Anaconda3.0\lib\site-packages\nengo_extras\keras.py in _add_conv2d_layer(self, layer)
112 filters, biases = layer.get_weights()
113 filters = filters[..., ::-1, ::-1] # flip
--> 114 strides = layer.subsample
115
116 nf, nc, ni, nj = filters.shape

AttributeError: 'Conv2D' object has no attribute 'subsample'

loading pickled model

how to create the file like cifar10-lif-1628.pkl for your dataset.why this is necessary what this file is holding.

data_mean = X_train.mean(axis=0)
X_train -= data_mean
X_test -= data_mean

# retrieve from https://figshare.com/s/49741f9e2d0d29f68871
cc_model = load_model_pickle('cifar10-lif-1628.pkl')


# --- Run model in Nengo
presentation_time = 0.2

Bad performance when running mnist_spiking_cnn.py

I ran the code of examples/keras/mnist_spiking_cnn.py and get a bad result. This is the final output:

Using Theano backend.
Train on 50000 samples, validate on 10000 samples
Epoch 1/6
50000/50000 [==============================] - 154s - loss: 2.5341 - acc: 0.1081 - val_loss: 2.3020 - val_acc: 0.1064
Epoch 2/6
50000/50000 [==============================] - 148s - loss: 2.3018 - acc: 0.1134 - val_loss: 2.3018 - val_acc: 0.1064
Epoch 3/6
50000/50000 [==============================] - 148s - loss: 2.3017 - acc: 0.1136 - val_loss: 2.3018 - val_acc: 0.1064
Epoch 4/6
50000/50000 [==============================] - 151s - loss: 2.3012 - acc: 0.1135 - val_loss: 2.3019 - val_acc: 0.1064
Epoch 5/6
50000/50000 [==============================] - 160s - loss: 2.3011 - acc: 0.1136 - val_loss: 2.3019 - val_acc: 0.1064
Epoch 6/6
50000/50000 [==============================] - 158s - loss: 2.3014 - acc: 0.1135 - val_loss: 2.3019 - val_acc: 0.1064
Test score: 2.30193813934
Test accuracy: 0.1064
Building finished in 0:00:01.
Simulating finished in 0:05:36.
Spiking accuracy (100 examples): 0.110

I altered some code to fit my computer, these are the alters.
When the code loads the training data and test data in (X_train, y_train), (X_test, y_test) = mnist.load_data(), an error happens saying

Using Theano backend.
Traceback (most recent call last):
  File "mnist_spiking_cnn.py", line 34, in <module>
    (X_train, y_train), (X_test, y_test) = mnist.load_data()
ValueError: too many values to unpack

So, I scanned the code of keras, finding that the load_data function returns a tuple and I can only get data in this way:

data = mnist.load_data()
X_train = data[0][0]  # shape is (50000. 768)
y_train = data[0][1]   # shape is (50000,)
X_test = data[1][0]   # shape is (10000, 768)
y_test = data[1][1]    # shape is (10000,)

Besides, I use the cpu instead of gpu by changing os.environ['THEANO_FLAGS'] = 'device=cpu,floatX=float32'.

My keras version is 1.2.0. I think the example code use a different version of keras, but I don't think the way I load data in makes this code perform bad.

So, can you give me some advice on how to tune the code?

Issues with Lasagne Branch

Stuff I was wondering about while looking at the @drasmuss Nengo Lasagne branch of this repository:

  1. Why is there a function called self.train in the self.train method in this code. It's very confusing. Could it just be called self.train_func? Also, why isn't it initialised in __ini__()?
  2. Why is all the data automatically shuffled in each minibatch for each epoch in this code? If the user wants their data shuffled before training, shouldn't they specify it?

Add QIF neuron

This has come up a couple times, so I thought I'd post it here. I'm not sure if QIF should just be added to Nengo itself, or if it should be used as an example of how to define your own neuron model.

Add utilities for spike raster plots

My current base code that I am copying into each notebook where I need it (based on code by @tcstewar):

import matplotlib
import numpy
import scipy
import scipy.ndimage

def prepare_spikes(time, data, sample_by_variance=None,
                     sample=None, sample_filter_width=20,
                     cluster=False, cluster_filter_width=2,
                     merge=None, contrast_scale=1.0, yticks=None,
                     sample_index=None, cluster_index=None):

    if sample_index:
        data = data[:, sample_index]    
    elif sample_by_variance is not None and sample_by_variance<len(data.T):
        dd=scipy.ndimage.gaussian_filter1d(data.astype(float).T,sample_filter_width,axis=1)
        vard=numpy.var(dd,axis=1)

        threshold=sorted(vard)[-sample_by_variance]                        
        index=[k for k,v in enumerate(vard) if v>=threshold]
        data=data[:,index]

    if sample is not None and sample<len(data.T):    
        stepsize=float(len(data.T))/sample
        data2=[]
        for k in range(sample):
            sub=data[:,int(k*stepsize):int((k+1)*stepsize)]
            count=numpy.sum(sub,axis=0)
            data2.append(sub[:, numpy.argmax(count)])
        data=numpy.array(data2).T    

    if cluster_index:
        data = data[:, cluster_index]    
    elif cluster:
        dd=scipy.ndimage.gaussian_filter1d(data.astype(float).T,cluster_filter_width,axis=1)
        z=scipy.cluster.hierarchy.linkage(dd)
        tree=scipy.cluster.hierarchy.to_tree(z)
        order=tree.pre_order()
        data=data[:,order]
    if merge is not None and merge<len(data.T):    
        stepsize=float(len(data.T))/merge
        data2=[]
        for k in range(merge):
            v=numpy.sum(data[:,int(k*stepsize):int((k+1)*stepsize)],axis=1)
            data2.append(v)
        data=numpy.array(data2).T    
    return data, time
               
def add_spikes(ax, data, time, contrast_scale=1.0):
    imgplt=ax.imshow(data.T,aspect='auto',cmap=matplotlib.cm.gray_r,interpolation='nearest',extent=(time[0],time[-1],0,len(data.T)))
    imgplt.set_clim(0.0,numpy.max(data)*contrast_scale)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.