Memory errors inspite of very small dataset in nengo_dl


#1

i am trying to run the nenog_dl example using my own dataset which is composed of jpg images of 227X227.i have 2 images in test and 10 in train but my computer run out of memory.any one suggest a solution .i have 4GB RAM with 64 bit windows OS
code snippet is given below.

def build_network(neuron_type):
    with nengo.Network() as net:
        # we'll make all the nengo objects in the network
        # non-trainable. we could train them if we wanted, but they don't
        # add any representational power so we can save some computation
        # by ignoring them. note that this doesn't affect the internal
        # components of tensornodes, which will always be trainable or
        # non-trainable depending on the code written in the tensornode.
        nengo_dl.configure_settings(trainable=True)

        # the input node that will be used to feed in input images
        inp = nengo.Node(nengo.processes.PresentInput(X_train, 0.1))

        # add the first convolutional layer
        x = nengo_dl.tensor_layer(
            inp, tf.layers.conv2d, shape_in=(227, 227,3), filters=32,
            kernel_size=3)

        # apply the neural nonlinearity
        x = nengo_dl.tensor_layer(x, neuron_type, **ens_params)

        # add another convolutional layer
        x = nengo_dl.tensor_layer(
            x, tf.layers.conv2d, shape_in=(225, 225, 32),
            filters=32, kernel_size=3)
        x = nengo_dl.tensor_layer(x, neuron_type, **ens_params)

        # add a pooling layer
        x = nengo_dl.tensor_layer(
            x, tf.layers.average_pooling2d, shape_in=(223, 223, 32),
            pool_size=2, strides=2)

        # add a dense layer, with neural nonlinearity.
        # note that for all-to-all connections like this we can use the
        # normal nengo connection transform to implement the weights
        # (instead of using a separate tensor_layer). we'll use a
        # Glorot uniform distribution to initialize the weights.
        x, conn = nengo_dl.tensor_layer(
            x, neuron_type, **ens_params, transform=nengo_dl.dists.Glorot(),
            shape_in=(255,), return_conn=True)
        # we need to set the weights and biases to be trainable
        # (since we set the default to be trainable=False)
        # note: we used return_conn=True above so that we could access
        # the connection object for this reason.
        net.config[x].trainable = True
        net.config[conn].trainable = True

        # add a dropout layer
        x = nengo_dl.tensor_layer(x, tf.layers.dropout, rate=0.4)

        # the final 10 dimensional class output
        x = nengo_dl.tensor_layer(x, tf.layers.dense, units=10)

    return net, inp, x

# construct the network
net, inp, out = build_network(softlif_neurons)
with net:
    out_p = nengo.Probe(out)

# construct the simulator
minibatch_size = None
sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size)

#2

4GB of RAM is not a lot, so you may just be running out of memory for normal reasons (note that most of that will be used up by your OS, so you won’t have the full 4GB available to run your model). But if you show how you are generating the inputs (e.g., what is X_train? what is minibatch_size?) then I can see if there is anything you are doing that might be using up more memory than expected.

Also, when does the memory error occur? At the last line there, sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size)? What is the last thing printed to the console?


#4

i am commenting the code snippets which i am using to get the images for test and train from dataset along with labels. yes the errors pointing to sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size).the size of mininbatch is None in this snippet. i have added 2 GB extra now it gives the same memory errors

import os
import pandas as pd
from PIL import Image
import numpy as np
from numpy import array
import matplotlib.pyplot as plt 
def get_data(is_test=False):
	data_path = "train"
	if is_test:
		data_path = "test"
	return pd.read_csv('dataset/bisting-'+data_path+'.csv', encoding="ISO-8859-1",
				names=['AN', 'FNAME', 'IMAGE_URL', 'TITLE', 'AR', 'CATEGORY_ID', 'CATEGORY'])


def loadImages(fnames,is_test=False):
	path = os.getcwd() + "/dataset/train_images/"
	if is_test:
		path = os.getcwd() + "/dataset/test_images/"
	loadedImages = []
	for image in fnames:
		tmp = Image.open(path + image)
		img = tmp.copy()
		loadedImages.append(img)
		tmp.close()
	return loadedImages


def get_pixels(fnames,is_test):
	imgs = loadImages(fnames, is_test)
	pixel_list = []
	for img in imgs:
		img = img.resize((227, 227), Image.ANTIALIAS)
		arr = np.array(img, dtype="float32")
		pixel_list.append(list(arr))
	return np.array(pixel_list)


def label_from_category(category_id=None):
	label_list = np.zeros(3)
	label_list[category_id] = 1
	return list(label_list)


def features_from_data(data, is_test=False):
	pixels = get_pixels(data.FILENAME, is_test)
	labels = np.array(data["CATEGORY_ID"].apply(label_from_category).tolist())
	return pixels, labels



test_data = get_data(is_test=False)
X_train, Y_train = features_from_data(test_data, is_test=False)

def get_data(is_test=False):
	data_path = "train"
	if is_test:
		data_path = "test"
	return pd.read_csv('dataset/bisting-'+data_path+'.csv', encoding="ISO-8859-1",
				names=['AN', 'FNAME', 'IMAGE_URL', 'TITLE', 'AR', 'CATEGORY_ID', 


def loadImages(fnames,is_test):
	path = os.getcwd() + "/dataset/train_images/"
	if is_test:
		path = os.getcwd() + "/dataset/test_images/"
	loadedImages = []
	for image in fnames:
		tmp = Image.open(path + image)
		img = tmp.copy()
		loadedImages.append(img)
		tmp.close()
	return loadedImages


def get_pixels(fnames,is_test):
	imgs = loadImages(fnames, is_test)
	pixel_list = []
	for img in imgs:
		img = img.resize((227, 227), Image.ANTIALIAS)
		arr = np.array(img, dtype="float32")
		pixel_list.append(list(arr))
	return np.array(pixel_list)


def label_from_category(category_id=None):
	label_list = np.zeros(1)
	label_list[category_id] = 1
	return list(label_list)


def features_from_data(data, is_test=False):
	pixels = get_pixels(data.FILENAME, is_test)
	labels = np.array(data["CATEGORY_ID"].apply(label_from_category).tolist())
	return pixels, labels



test_data = get_data(is_test=True)
X_test, Y_test = features_from_data(test_data, is_test=True)



# lif parameters
lif_neurons = nengo.LIF(tau_rc=0.02,tau_ref=0.002)

# softlif parameters (lif parameters + sigma)
softlif_neurons = nengo_dl.SoftLIFRate(tau_rc=0.02, tau_ref=0.002,
                                       sigma=0.002)

# ensemble parameters
ens_params = dict(max_rates=nengo.dists.Choice([100]), intercepts=nengo.dists.Choice([0]))

# plot some example LIF tuning curves
for neuron_type in (lif_neurons, softlif_neurons):
    with nengo.Network(seed=0) as net:
        ens = nengo.Ensemble(10, 1, neuron_type=neuron_type)

    with nengo_dl.Simulator(net) as sim:
        plt.figure()
        plt.plot(*nengo.utils.ensemble.tuning_curves(ens, sim))
        plt.xlabel("input value")
        plt.ylabel("firing rate")
        plt.title(str(neuron_type))


def build_network(neuron_type):
    with nengo.Network() as net:
        # we'll make all the nengo objects in the network
        # non-trainable. we could train them if we wanted, but they don't
        # add any representational power so we can save some computation
        # by ignoring them. note that this doesn't affect the internal
        # components of tensornodes, which will always be trainable or
        # non-trainable depending on the code written in the tensornode.
        nengo_dl.configure_settings(trainable=True)

        # the input node that will be used to feed in input images
        inp = nengo.Node(nengo.processes.PresentInput(X_train, 0.1))

        # add the first convolutional layer
        x = nengo_dl.tensor_layer(
            inp, tf.layers.conv2d, shape_in=(227, 227,3), filters=32,
            kernel_size=3)

        # apply the neural nonlinearity
        x = nengo_dl.tensor_layer(x, neuron_type, **ens_params)

        # add another convolutional layer
        x = nengo_dl.tensor_layer(
            x, tf.layers.conv2d, shape_in=(225, 225, 32),
            filters=32, kernel_size=3)
        x = nengo_dl.tensor_layer(x, neuron_type, **ens_params)

        # add a pooling layer
        x = nengo_dl.tensor_layer(
            x, tf.layers.average_pooling2d, shape_in=(223, 223, 32),
            pool_size=2, strides=2)

        # add a dense layer, with neural nonlinearity.
        # note that for all-to-all connections like this we can use the
        # normal nengo connection transform to implement the weights
        # (instead of using a separate tensor_layer). we'll use a
        # Glorot uniform distribution to initialize the weights.
        x, conn = nengo_dl.tensor_layer(
            x, neuron_type, **ens_params, transform=nengo_dl.dists.Glorot(),
            shape_in=(255,), return_conn=True)
        # we need to set the weights and biases to be trainable
        # (since we set the default to be trainable=False)
        # note: we used return_conn=True above so that we could access
        # the connection object for this reason.
        net.config[x].trainable = True
        net.config[conn].trainable = True

        # add a dropout layer
        x = nengo_dl.tensor_layer(x, tf.layers.dropout, rate=0.4)

        # the final 10 dimensional class output
        x = nengo_dl.tensor_layer(x, tf.layers.dense, units=1)

    return net, inp, x

# construct the network
net, inp, out = build_network(softlif_neurons)
with net:
    out_p = nengo.Probe(out)

# construct the simulator
minibatch_size = None
sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size)

#5

When I run your model I only see about 1.8GB of memory usage:

Line #    Mem usage    Increment   Line Contents
================================================
    75  229.930 MiB  229.930 MiB   @profile
    76                             def main():
    77                                 # construct the network
    78  233.652 MiB    3.723 MiB       net, inp, out = build_network(softlif_neurons)
    79  233.652 MiB    0.000 MiB       with net:
    80  233.652 MiB    0.000 MiB           out_p = nengo.Probe(out)
    81
    82                                 # construct the simulator
    83  233.652 MiB    0.000 MiB       minibatch_size = None
    84 1824.109 MiB 1824.109 MiB       sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size)

So I think you are losing memory elsewhere in your simulation, assuming that you have at least that much available.

Here is the test script I am running if you would like to try it yourself:

import nengo
import nengo_dl
import numpy as np
import tensorflow as tf

X_train = np.random.randn(100, 227, 227, 3)

# lif parameters
lif_neurons = nengo.LIF(tau_rc=0.02,tau_ref=0.002)

# softlif parameters (lif parameters + sigma)
softlif_neurons = nengo_dl.SoftLIFRate(tau_rc=0.02, tau_ref=0.002,
                                       sigma=0.002)

# ensemble parameters
ens_params = dict(max_rates=nengo.dists.Choice([100]), intercepts=nengo.dists.Choice([0]))



def build_network(neuron_type):
    with nengo.Network() as net:
        # we'll make all the nengo objects in the network
        # non-trainable. we could train them if we wanted, but they don't
        # add any representational power so we can save some computation
        # by ignoring them. note that this doesn't affect the internal
        # components of tensornodes, which will always be trainable or
        # non-trainable depending on the code written in the tensornode.
        nengo_dl.configure_settings(trainable=True)

        # the input node that will be used to feed in input images
        inp = nengo.Node(nengo.processes.PresentInput(X_train, 0.1))

        # add the first convolutional layer
        x = nengo_dl.tensor_layer(
            inp, tf.layers.conv2d, shape_in=(227, 227, 3), filters=32,
            kernel_size=3)

        # apply the neural nonlinearity
        x = nengo_dl.tensor_layer(x, neuron_type, **ens_params)

        # add another convolutional layer
        x = nengo_dl.tensor_layer(
            x, tf.layers.conv2d, shape_in=(225, 225, 32),
            filters=32, kernel_size=3)
        x = nengo_dl.tensor_layer(x, neuron_type, **ens_params)

        # add a pooling layer
        x = nengo_dl.tensor_layer(
            x, tf.layers.average_pooling2d, shape_in=(223, 223, 32),
            pool_size=2, strides=2)

        # add a dense layer, with neural nonlinearity.
        # note that for all-to-all connections like this we can use the
        # normal nengo connection transform to implement the weights
        # (instead of using a separate tensor_layer). we'll use a
        # Glorot uniform distribution to initialize the weights.
        x, conn = nengo_dl.tensor_layer(
            x, neuron_type, **ens_params, transform=nengo_dl.dists.Glorot(),
            shape_in=(255,), return_conn=True)
        # we need to set the weights and biases to be trainable
        # (since we set the default to be trainable=False)
        # note: we used return_conn=True above so that we could access
        # the connection object for this reason.
        net.config[x].trainable = True
        net.config[conn].trainable = True

        # add a dropout layer
        x = nengo_dl.tensor_layer(x, tf.layers.dropout, rate=0.4)

        # the final 10 dimensional class output
        x = nengo_dl.tensor_layer(x, tf.layers.dense, units=1)

    return net, inp, x

def main():
    # construct the network
    net, inp, out = build_network(softlif_neurons)
    with net:
        out_p = nengo.Probe(out)

    # construct the simulator
    minibatch_size = None
    sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size)

main()

#6

now it runs and following line appears.but the kernel become dead due to which i became unable to run the “code cells” after its death. i am using core i3 with two 2.4GHZ processors and 6GB RAM.Now want to now why system become annoyed and kernel dead? do you have any suggestion to get rid of this?

Building network
Build finished in 0:00:08
| Optimizing graph: creating signals | 0:00:01
C:\ProgramData\Anaconda34\lib\site-packages\nengo_dl\graph_optimizer.py:1132: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
if np.issubdtype(sig.dtype, np.float):
| Optimizing graph: creating signals # | 0:00:19


#7

Sorry, it’s hard to know what’s going wrong without any error messages. I’m testing on a similar lightweight system (2.3Ghz i5, 6GB memory) and everything runs without problem, so it seems like it may be some specific quirk in your system unfortunately.

One thing you could try is upgrading to the development version of nengo_dl. You can find the instructions here. I wouldn’t expect that to make a difference, but you never know.


#8

That might indicate a problem with your Python installation (or a Python module that uses C code, potentially Tensorflow). Usually the kernel should not just crash …


#9

yes after upgradeation it start working now.


#10
# note that we need to add the time dimension (axis 1), which has length 1
# in this case. we're also going to reduce the number of test images, just to
# speed up this example.
train_inputs = {inp: X_train[:, None, :]}
train_targets = {out_p: Y_train[:, None, :]}
test_inputs = {inp: X_test[:, None, :]}
test_targets = {out_p: Y_test[:, None, :]}
def objective(x, y):
    return tf.nn.softmax_cross_entropy_with_logits(logits=x, labels=y)
opt = tf.train.AdadeltaOptimizer(learning_rate=1)

def classification_error(outputs, targets):
    return 100 * tf.reduce_mean(
        tf.cast(tf.not_equal(tf.argmax(outputs[:, -1], axis=-1),
                             tf.argmax(targets[:, -1], axis=-1)),
                tf.float32))

print("error before training: %.2f%%" % sim.loss(test_inputs, test_targets,
                                                 classification_error))

do_training = True
if do_training:
    # run training
    sim.train(train_inputs, train_targets, opt, objective=objective, n_epochs=5)

    # save the parameters to file
    sim.save_params("./my_params")


print("error after training: %.2f%%" % sim.loss(test_inputs, test_targets,
                                                classification_error))

sim.close()

it gives the following errors. i tried to hard to rectify but failed any one can suggest me how to solve this

ValidationError                           Traceback (most recent call last)
<ipython-input-41-a4f0da281df8> in <module>()
      1 print("error before training: %.2f%%" % sim.loss(test_inputs, test_targets,
----> 2                                                  classification_error))
      3 
      4 do_training = True
      5 if do_training:

C:\ProgramData\Anaconda34\lib\site-packages\nengo_dl\simulator.py in loss(self, inputs, targets, objective)
    555             raise SimulatorClosed("Loss cannot be computed after simulator is "
    556                                   "closed.")
--> 557         self._check_data(inputs, mode="input")
    558         self._check_data(targets, mode="target", n_steps=n_steps,
    559                          n_batch=batch_size)

C:\ProgramData\Anaconda34\lib\site-packages\nengo_dl\simulator.py in _check_data(self, data, mode, n_batch, n_steps)
   1007                     "Dimensionality of data (%s) does not match "
   1008                     "dimensionality of %s (%s)" % (x.shape[2], n, d),
-> 1009                     "%s data" % mode)
   1010 
   1011     @property

ValidationError: input data: Dimensionality of data (227) does not match dimensionality of <Node (unlabeled) at 0x28e709bfd0> (154587)

#11

this is the memory error trace that i am getting .

Building network
Build finished in 0:00:05 
Optimization finished in 0:00:08                                               
Construction finished in 0:11:54                                               
---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-7-0527e80faca3> in <module>()
     79 # construct the simulator
     80 minibatch_size = None
---> 81 sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size)

C:\ProgramData\Anaconda34\lib\site-packages\nengo_dl\simulator.py in __init__(self, network, dt, seed, model, dtype, device, unroll_simulation, minibatch_size, tensorboard)
    132         with utils.ProgressBar("Constructing graph", "Construction",
    133                                max_value=None) as progress:
--> 134             self.tensor_graph.build(progress)
    135 
    136         # output simulation data for viewing via TensorBoard

C:\ProgramData\Anaconda34\lib\site-packages\nengo_dl\tensor_graph.py in func_with_self(self, *args, **kwargs)
     27     def func_with_self(self, *args, **kwargs):
     28         with self.graph.as_default(), tf.device(self.device):
---> 29             return func(self, *args, **kwargs)
     30 
     31     return func_with_self

C:\ProgramData\Anaconda34\lib\site-packages\nengo_dl\tensor_graph.py in build(self, progress)
    181                     var = tf.get_variable(
    182                         name, initializer=tf.constant_initializer(v),
--> 183                         dtype=v.dtype, shape=v.shape, trainable=True)
    184             else:
    185                 with tf.variable_scope("local_vars", reuse=False):

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\variable_scope.py in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint)
   1260       partitioner=partitioner, validate_shape=validate_shape,
   1261       use_resource=use_resource, custom_getter=custom_getter,
-> 1262       constraint=constraint)
   1263 get_variable_or_local_docstring = (
   1264     """%s

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\variable_scope.py in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint)
   1095           partitioner=partitioner, validate_shape=validate_shape,
   1096           use_resource=use_resource, custom_getter=custom_getter,
-> 1097           constraint=constraint)
   1098 
   1099   def _get_partitioned_variable(self,

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\variable_scope.py in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter, constraint)
    433           caching_device=caching_device, partitioner=partitioner,
    434           validate_shape=validate_shape, use_resource=use_resource,
--> 435           constraint=constraint)
    436 
    437   def _get_partitioned_variable(

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\variable_scope.py in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, constraint)
    402           trainable=trainable, collections=collections,
    403           caching_device=caching_device, validate_shape=validate_shape,
--> 404           use_resource=use_resource, constraint=constraint)
    405 
    406     if custom_getter is not None:

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\variable_scope.py in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape, use_resource, constraint)
    804           dtype=variable_dtype,
    805           validate_shape=validate_shape,
--> 806           constraint=constraint)
    807     if context.in_graph_mode() or self._store_eager_variables:
    808       # In eager mode we do not want to keep default references to Variable

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\variables.py in __init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope, constraint)
    227           dtype=dtype,
    228           expected_shape=expected_shape,
--> 229           constraint=constraint)
    230 
    231   def __repr__(self):

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\variables.py in _init_from_args(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, expected_shape, constraint)
    321             with ops.name_scope("Initializer"), ops.device(None):
    322               self._initial_value = ops.convert_to_tensor(
--> 323                   initial_value(), name="initial_value", dtype=dtype)
    324               shape = (self._initial_value.get_shape()
    325                        if validate_shape else tensor_shape.unknown_shape())

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\variable_scope.py in <lambda>()
    778           initializer = initializer(dtype=dtype)
    779         init_val = lambda: initializer(  # pylint: disable=g-long-lambda
--> 780             shape.as_list(), dtype=dtype, partition_info=partition_info)
    781         variable_dtype = dtype.base_dtype
    782 

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\ops\init_ops.py in __call__(self, shape, dtype, partition_info, verify_shape)
    198       verify_shape = self._verify_shape
    199     return constant_op.constant(
--> 200         self.value, dtype=dtype, shape=shape, verify_shape=verify_shape)
    201 
    202   def get_config(self):

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name, verify_shape)
    216       attrs={"value": tensor_value,
    217              "dtype": dtype_value},
--> 218       name=name).outputs[0]
    219   return const_tensor
    220 

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)
   3214     for key, value in self._attr_scope_map.items():
   3215       try:
-> 3216         op.get_attr(key)
   3217       except ValueError:
   3218         if callable(value):

C:\ProgramData\Anaconda34\lib\site-packages\tensorflow\python\framework\ops.py in get_attr(self, name)
   2171       if name not in self._node_def.attr:
   2172         raise ValueError(
-> 2173             "No attr named '" + name + "' in " + str(self._node_def))
   2174       x = self._node_def.attr[name]
   2175 

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\internal\python_message.py in __str__(self)
    964   """Helper for _AddMessageMethods()."""
    965   def __str__(self):
--> 966     return text_format.MessageToString(self)
    967   cls.__str__ = __str__
    968 

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in MessageToString(message, as_utf8, as_one_line, pointy_brackets, use_index_order, float_format, use_field_number, descriptor_pool, indent, message_formatter)
    161                      use_index_order, float_format, use_field_number,
    162                      descriptor_pool, message_formatter)
--> 163   printer.PrintMessage(message)
    164   result = out.getvalue()
    165   out.close()

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintMessage(self, message)
    347           # TODO(haberman): refactor and optimize if this becomes an issue.
    348           entry_submsg = value.GetEntryClass()(key=key, value=value[key])
--> 349           self.PrintField(field, entry_submsg)
    350       elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
    351         for element in value:

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintField(self, field, value)
    381       out.write(': ')
    382 
--> 383     self.PrintFieldValue(field, value)
    384     if self.as_one_line:
    385       out.write(' ')

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintFieldValue(self, field, value)
    417     out = self.out
    418     if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
--> 419       self._PrintMessageFieldValue(value)
    420     elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
    421       enum_value = field.enum_type.values_by_number.get(value, None)

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in _PrintMessageFieldValue(self, value)
    402       self.out.write(' %s\n' % openb)
    403       self.indent += 2
--> 404       self.PrintMessage(value)
    405       self.indent -= 2
    406       self.out.write(' ' * self.indent + closeb)

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintMessage(self, message)
    352           self.PrintField(field, element)
    353       else:
--> 354         self.PrintField(field, value)
    355 
    356   def PrintField(self, field, value):

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintField(self, field, value)
    381       out.write(': ')
    382 
--> 383     self.PrintFieldValue(field, value)
    384     if self.as_one_line:
    385       out.write(' ')

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintFieldValue(self, field, value)
    417     out = self.out
    418     if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
--> 419       self._PrintMessageFieldValue(value)
    420     elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
    421       enum_value = field.enum_type.values_by_number.get(value, None)

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in _PrintMessageFieldValue(self, value)
    402       self.out.write(' %s\n' % openb)
    403       self.indent += 2
--> 404       self.PrintMessage(value)
    405       self.indent -= 2
    406       self.out.write(' ' * self.indent + closeb)

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintMessage(self, message)
    352           self.PrintField(field, element)
    353       else:
--> 354         self.PrintField(field, value)
    355 
    356   def PrintField(self, field, value):

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintField(self, field, value)
    381       out.write(': ')
    382 
--> 383     self.PrintFieldValue(field, value)
    384     if self.as_one_line:
    385       out.write(' ')

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintFieldValue(self, field, value)
    417     out = self.out
    418     if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
--> 419       self._PrintMessageFieldValue(value)
    420     elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
    421       enum_value = field.enum_type.values_by_number.get(value, None)

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in _PrintMessageFieldValue(self, value)
    402       self.out.write(' %s\n' % openb)
    403       self.indent += 2
--> 404       self.PrintMessage(value)
    405       self.indent -= 2
    406       self.out.write(' ' * self.indent + closeb)

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintMessage(self, message)
    352           self.PrintField(field, element)
    353       else:
--> 354         self.PrintField(field, value)
    355 
    356   def PrintField(self, field, value):

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintField(self, field, value)
    381       out.write(': ')
    382 
--> 383     self.PrintFieldValue(field, value)
    384     if self.as_one_line:
    385       out.write(' ')

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_format.py in PrintFieldValue(self, field, value)
    435       else:
    436         out_as_utf8 = self.as_utf8
--> 437       out.write(text_encoding.CEscape(out_value, out_as_utf8))
    438       out.write('\"')
    439     elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:

C:\ProgramData\Anaconda34\lib\site-packages\google\protobuf\text_encoding.py in CEscape(text, as_utf8)
     77   if as_utf8:
     78     return ''.join(_cescape_utf8_to_str[Ord(c)] for c in text)
---> 79   return ''.join(_cescape_byte_to_str[Ord(c)] for c in text)
     80 
     81 

MemoryError:

#12

The training data needs to have shape (batch_size, n_steps, node_dimensions), where node_dimensions is the dimensionality of the node for which you are providing data. That error is telling you that you are feeding in an input with shape (batch_size, n_steps, 227) to a node with dimensionality 154587. Note that 154587 is the correct value (227x227x3), so you’d have to figure out why test_inputs has the wrong shape.


#14

i have change the code to Mnist example and kept the train and test from train but the errors are same

train_inputs = {inp: X_train[:,None, :]}
train_targets = {out_p: Y_train[:, None, :]}
test_inputs = {inp: X_train[:, None, :]}
test_targets = {out_p: Y_train[:, None, :]}

but the errors are same


#15

Sorry, it’s hard to guess why your data has the wrong shape. Just to reiterate, your input data should have shape (batch_size, n_steps, dimensions). In your case n_steps is probably 1 and dimensions is 28*28=784 for MNIST. So your train_inputs/test_inputs data should have shape (batch_size, 1, 784). If it doesn’t have that shape, you’ll need to go through the way you are loading your data and see why that isn’t the case (perhaps you’re not flattening the images?). There’s an example of loading MNIST data here showing all the steps required https://www.nengo.ai/nengo_dl/examples/spiking_mnist.html, so one approach would just be to exactly duplicate that, or figure out how whatever you are doing differs from those steps.


#16

i am now showing you the whole code that i am trying to make a copy of Mnist Example.i tried to replication the code of that example and make the necessary changes but the dimesionality error remains the same .can you suggest me the the required changes to remove that errors. Xtrain have (3,227,227,3) and Xtest have (1,227,227,3) shape.
%matplotlib inline

from urllib.request import urlretrieve
import zipfile

import nengo
import nengo_dl
import tensorflow as tf
#from tensorflow.contrib.learn.python.learn.datasets import mnist
import numpy as np
import matplotlib.pyplot


import tensorflow as tf
import os
import pandas as pd
from PIL import Image
import numpy as np
from numpy import array
import matplotlib.pyplot as plt 
def get_data(is_test=False):
	data_path = "train"
	if is_test:
		data_path = "test"
	return pd.read_csv('dataset/blist-'+data_path+'.csv', encoding="ISO-8859-1",
				names=[''])


def loadImages(fnames,is_test):
	path = os.getcwd() + "/dataset/train_images/"
	if is_test:
		path = os.getcwd() + "/dataset/test_images/"
	loadedImages = []
	for image in fnames:
		tmp = Image.open(path + image)
		img = tmp.copy()
		loadedImages.append(img)
		tmp.close()
	return loadedImages


def get_pixels(fnames,is_test):
	imgs = loadImages(fnames, is_test)
	pixel_list = []
	for img in imgs:
		img = img.resize((227, 227), Image.ANTIALIAS)
		arr = np.array(img, dtype="float32")
		pixel_list.append(list(arr))
	return np.array(pixel_list)


def label_from_category(category_id=None):
	label_list = np.zeros(3)
	label_list[category_id] = 1
	return list(label_list)


def features_from_data(data, is_test=False):
	pixels = get_pixels(data.FILENAME, is_test)
	labels = np.array(data["CATEGORY_ID"].apply(label_from_category).tolist())
	return pixels, labels



test_data = get_data(is_test=True)
X_test, Y_test = features_from_data(test_data, is_test=True)
#print(test_x)
#print(test_labels)
#Sample Image 
plt.imshow(X_test[0])
plt.axis('off')
plt.title('Sample image with label {}'.format(Y_test[0]))
plt.show()


import tensorflow as tf
import os
import pandas as pd
from PIL import Image
import numpy as np
from numpy import array
import matplotlib.pyplot as plt 
def get_data(is_test=False):
	data_path = "train"
	if is_test:
		data_path = "test"
	return pd.read_csv('dataset/blist-'+data_path+'.csv', encoding="ISO-8859-1",
				names=[''])


def loadImages(fnames,is_test=False):
	path = os.getcwd() + "/dataset/train_images/"
	if is_test:
		path = os.getcwd() + "/dataset/test_images/"
	loadedImages = []
	for image in fnames:
		tmp = Image.open(path + image)
		img = tmp.copy()
		loadedImages.append(img)
		tmp.close()
	return loadedImages


def get_pixels(fnames,is_test):
	imgs = loadImages(fnames, is_test)
	pixel_list = []
	for img in imgs:
		img = img.resize((227, 227), Image.ANTIALIAS)
		arr = np.array(img, dtype="float32")
		pixel_list.append(list(arr))
	return np.array(pixel_list)


def label_from_category(category_id=None):
	label_list = np.zeros(3)
	label_list[category_id] = 1
	return list(label_list)


def features_from_data(data, is_test=False):
	pixels = get_pixels(data.FILENAME, is_test)
	labels = np.array(data["CATEGORY_ID"].apply(label_from_category).tolist())
	return pixels, labels



test_data = get_data(is_test=False)
X_train, Y_train = features_from_data(test_data, is_test=False)
#print(test_x)
#print(test_labels)
#Sample Image 
plt.imshow(X_train[1])
plt.axis('off')
plt.title('Sample image with label {}'.format(Y_train[1]))
plt.show()


lif_neurons = nengo.LIF(tau_rc=0.02,tau_ref=0.002)

# softlif parameters (lif parameters + sigma)
softlif_neurons = nengo_dl.SoftLIFRate(tau_rc=0.02, tau_ref=0.002,
                                       sigma=0.002)

# ensemble parameters
ens_params = dict(max_rates=nengo.dists.Choice([100]), intercepts=nengo.dists.Choice([0]))

# plot some example LIF tuning curves
for neuron_type in (lif_neurons, softlif_neurons):
    with nengo.Network(seed=0) as net:
        ens = nengo.Ensemble(10, 1, neuron_type=neuron_type)

    with nengo_dl.Simulator(net) as sim:
        plt.figure()
        plt.plot(*nengo.utils.ensemble.tuning_curves(ens, sim))
        plt.xlabel("input value")
        plt.ylabel("firing rate")
        plt.title(str(neuron_type))


import nengo
import nengo_dl
import numpy as np
import tensorflow as tf

X_train = np.random.randn(3, 227, 227, 3)

# lif parameters
lif_neurons = nengo.LIF(tau_rc=0.02,tau_ref=0.002)

# softlif parameters (lif parameters + sigma)
softlif_neurons = nengo_dl.SoftLIFRate(tau_rc=0.02, tau_ref=0.002,
                                       sigma=0.002)

# ensemble parameters
ens_params = dict(max_rates=nengo.dists.Choice([100]), intercepts=nengo.dists.Choice([0]))



def build_network(neuron_type):
    with nengo.Network() as net:
        # we'll make all the nengo objects in the network
        # non-trainable. we could train them if we wanted, but they don't
        # add any representational power so we can save some computation
        # by ignoring them. note that this doesn't affect the internal
        # components of tensornodes, which will always be trainable or
        # non-trainable depending on the code written in the tensornode.
        nengo_dl.configure_settings(trainable=True)

        # the input node that will be used to feed in input images
        inp = nengo.Node(nengo.processes.PresentInput(X_test, 0.1))

        # add the first convolutional layer
        x = nengo_dl.tensor_layer(
            inp, tf.layers.conv2d, shape_in=(227, 227, 3), filters=32,
            kernel_size=3)

        # apply the neural nonlinearity
        x = nengo_dl.tensor_layer(x, neuron_type, **ens_params)

        # add another convolutional layer
        x = nengo_dl.tensor_layer(
            x, tf.layers.conv2d, shape_in=(225, 225, 32),
            filters=32, kernel_size=3)
        x = nengo_dl.tensor_layer(x, neuron_type, **ens_params)

        # add a pooling layer
        x = nengo_dl.tensor_layer(
            x, tf.layers.average_pooling2d, shape_in=(223, 223, 32),
            pool_size=2, strides=2)

        # add a dense layer, with neural nonlinearity.
        # note that for all-to-all connections like this we can use the
        # normal nengo connection transform to implement the weights
        # (instead of using a separate tensor_layer). we'll use a
        # Glorot uniform distribution to initialize the weights.
        x, conn = nengo_dl.tensor_layer(
            x, neuron_type, **ens_params, transform=nengo_dl.dists.Glorot(),
            shape_in=(128,), return_conn=True)
        # we need to set the weights and biases to be trainable
        # (since we set the default to be trainable=False)
        # note: we used return_conn=True above so that we could access
        # the connection object for this reason.
        net.config[x].trainable = True
        net.config[conn].trainable = True

        # add a dropout layer
        x = nengo_dl.tensor_layer(x, tf.layers.dropout, rate=0.4)

        # the final 10 dimensional class output
        x = nengo_dl.tensor_layer(x, tf.layers.dense, units=1)

    return net, inp, x

net, inp, out = build_network(softlif_neurons)
with net:
    out_p = nengo.Probe(out)

# construct the simulator
minibatch_size = 1
sim = nengo_dl.Simulator(net, minibatch_size=minibatch_size)

train_inputs = {inp: X_train[:, None, :]}
train_targets = {out_p: Y_train[:, None, :]}
test_inputs = {inp: X_test[:, None, :]}
test_targets = {out_p: Y_test[:, None, :]}

def objective(x, y):
    return tf.nn.softmax_cross_entropy_with_logits(logits=x, labels=y)

def classification_error(outputs, targets):
    return 100 * tf.reduce_mean(
        tf.cast(tf.not_equal(tf.argmax(outputs[:, -1], axis=-1),
                             tf.argmax(targets[:, -1], axis=-1)),
                tf.float32))

print("error before training: %.2f%%" % sim.loss(test_inputs, test_targets,
                                                 classification_error))

do_training = True
if do_training:
    # run training
    sim.train(train_inputs, train_targets, opt, objective=objective, n_epochs=3)

    # save the parameters to file
    sim.save_params("./my_params")


print("error after training: %.2f%%" % sim.loss(test_inputs, test_targets,
                                                classification_error))

sim.close()

the errors remains the same
ValidationError Traceback (most recent call last)
in ()
1 print(“error before training: %.2f%%” % sim.loss(test_inputs, test_targets,
----> 2 classification_error))
3
4 do_training = True
5 if do_training:

C:\ProgramData\Anaconda34\lib\site-packages\nengo_dl\simulator.py in loss(self, inputs, targets, objective)
555 raise SimulatorClosed("Loss cannot be computed after simulator is "
556 “closed.”)
–> 557 self._check_data(inputs, mode=“input”)
558 self._check_data(targets, mode=“target”, n_steps=n_steps,
559 n_batch=batch_size)

C:\ProgramData\Anaconda34\lib\site-packages\nengo_dl\simulator.py in _check_data(self, data, mode, n_batch, n_steps)
1007 "Dimensionality of data (%s) does not match "
1008 “dimensionality of %s (%s)” % (x.shape[2], n, d),
-> 1009 “%s data” % mode)
1010
1011 @property

ValidationError: input data: Dimensionality of data (227) does not match dimensionality of <Node (unlabeled) at 0x5b43bd6208> (154587)


#17

So your training data has a batch size of 3, n_steps is 1, and the node has dimensionality 154587. You say your data has shape (3,227,227,3). So you should use the numpy reshape function to change your data to the correct shape (e.g., np.reshape(train_inputs, (3, 1, 154587))).


#18

now i am this error i tried to get ride of this but error always appears please suggest me the how to remove this errrors

    Y_train shape: (3, 3)
    3 train samples

X_train=np.reshape(X_train, (3, 1, 154587))


train_inputs = {inp: X_train}
train_targets = {out_p: Y_train}

but i ran the above code which raises the errors

ValidationError: target data: Data for <Probe of ‘output’ of <TensorNode (unlabeled) at 0xc1f711d860>> has number of timesteps=3, which does not match expected size 1


#19

Your target data (Y_train) also has the wrong shape. It should have shape (batch_size, n_steps, dimensions), as with the input data. Dimensions in this case would be the number of target classes (e.g., 10 for MNIST).


#20

i have target data((Y_train) has shape ``(Y_train.shape)=(3,3)my batch size =3 and n_steps=1 in this case and for the dimensions is 2 . now i am trying to reshape this according to your suggestion(batch_size, n_steps, dimensions)like(3,1,2 )` but it is not working ’ i am now showing you the code snippet of what i am trying .kindly suggest me where i am doing wrong

import tensorflow as tf
import os
import pandas as pd
from PIL import Image
import numpy as np
from numpy import array
import matplotlib.pyplot as plt 
def get_data(is_test=False):
	data_path = "train"
	if is_test:
		data_path = "test"
	return pd.read_csv('dataset/blist-'+data_path+'.csv', encoding="ISO-8859-1",
				names=[',,,'])


def loadImages(fnames,is_test=False):
	path = os.getcwd() + "/dataset/train_images/"
	if is_test:
		path = os.getcwd() + "/dataset/test_images/"
	loadedImages = []
	for image in fnames:
		tmp = Image.open(path + image)
		img = tmp.copy()
		loadedImages.append(img)
		tmp.close()
	return loadedImages


def get_pixels(fnames,is_test):
	imgs = loadImages(fnames, is_test)
	pixel_list = []
	for img in imgs:
		img = img.resize((227, 227), Image.ANTIALIAS)
		arr = np.array(img, dtype="float32")
		pixel_list.append(list(arr))
	return np.array(pixel_list)


def label_from_category(category_id=None):
	label_list = np.zeros(3)
	label_list[category_id] = 1
	return list(label_list)


def features_from_data(data, is_test=False):
	pixels = get_pixels(data.FILENAME, is_test)
	labels = np.array(data["CATEGORY_ID"].apply(label_from_category).tolist())
	return pixels, labels



test_data = get_data(is_test=False)
X_train, Y_train = features_from_data(test_data, is_test=False)
#print(test_x)
#print(test_labels)
#Sample Image 
plt.imshow(X_train[1])
plt.axis('off')
plt.title('Sample image with label {}'.format(Y_train[1]))
plt.show()

train_data=X_train
train_labels_one_hot=Y_train


def classified_data():
total_data = get_data(is_test=False)

classified_data_list = []
class_names = []
for i in range(2):
classified_data_list.append(total_data[total_data.CATEGORY_ID == i])
class_names.append(total_data[total_data.CATEGORY_ID == i].CATEGORY.values[0])
return classified_data_list, class_names
myvalue,class_names=classified_data()
print(class_names)
nb_classes = len(class_names)    
print(nb_classes)

Y_train = np_utils.to_categorical(Y_train)
print(Y_train.shape)

i am get name of classes ['Business ', ‘Calendars’]
no of classes=2
shape of Y_train=(3, 3)
my dataset have 3 images that belong to 2 classes and each image is 227X227

Y_train=np.reshape(Y_train, (3,1,2))

ValueError: cannot reshape array of size 9 into shape (3,1,2)

#21

Looking at your code, it looks like the number of target classes is 3, not 2 (label_list = np.zeros(3)).


#22

after your suggested changes now i have rectify that very error but the following error is is existing now.i dont know why this error is showing .according to my openion target classes have shape
now Y_train((batch_size, n_steps, dimensions)) but failed to understand why this is looking for 1.
i am using this snippets for Y_train
X_train=np.reshape(X_train, (3, 1, 154587))
X_test=np.reshape(X_train, (3, 1, 154587))
Y_train=np.reshape(Y_train, (3,1,2))

train_inputs = {inp: X_train}
train_targets = {out_p: Y_train}
test_inputs = {inp: X_train}
test_targets = {out_p: Y_train}

def objective(x, y):
    return tf.nn.softmax_cross_entropy_with_logits(logits=x, labels=y)
opt = tf.train.AdadeltaOptimizer(learning_rate=1)
def classification_error(outputs, targets):
    return 100 * tf.reduce_mean(
        tf.cast(tf.not_equal(tf.argmax(outputs[:, -1], axis=-1),
                             tf.argmax(targets[:, -1], axis=-1)),
                tf.float32))

print("error before training: %.2f%%" % sim.loss(test_inputs, test_targets,
                                                 classification_error))

do_training = True
if do_training:
    # run training
    sim.train(train_inputs, train_targets, opt, objective=objective, n_epochs=3)

    # save the parameters to file
    sim.save_params("./my_params")


print("error after training: %.2f%%" % sim.loss(test_inputs, test_targets,
                                                classification_error))

sim.close()

ValidationError: target data: Dimensionality of data (2) does not match dimensionality of <Probe of 'output' of <TensorNode (unlabeled) at 0x381dff5fd0>> (1)

so where can i make changes either to get the dimensionality 2. i am using the above code snippets