Hello, I am working with Iris dataset. Iris dataset as 4 features: sepal length, sepal width, petal length and petal width. I have created four nodes for each features and hence four functions
import numpy as np
import nengo
import matplotlib.pyplot as plt
data = np.genfromtxt('Iris.csv', delimiter=',', usecols=(1, 2, 3, 4))
# normalization to unity of each pattern in the data
features = np.apply_along_axis(lambda x: x / np.linalg.norm(x), 1, data[1:len(data), :])
dt = 0.001
def sepal_length(t):
ix = int(t / dt) % features.shape[0]
return features[ix, 0]
def sepal_width(t):
ix = int(t / dt) % features.shape[0]
return features[ix, 1]
def petal_length(self, t):
ix = int(t / dt) % features.shape[0]
return features[ix, 2]
def petal_width(self, t):
ix = int(t / dt) % features.shape[0]
return features[ix, 3]
with model:
data_feeder = DataFeeder()
feature_1 = nengo.Node(output=sepal_length, label="Features 1")
feature_2 = nengo.Node(output=sepal_width, label="Features 2")
feature_3 = nengo.Node(output=petal_length, label="Features 3")
feature_4 = nengo.Node(output=petal_width, label="Features 4")
feature_1_probe = nengo.Probe(feature_1)
feature_2_probe = nengo.Probe(feature_2)
feature_3_probe = nengo.Probe(feature_3)
feature_4_probe = nengo.Probe(feature_4)
ens = nengo.Ensemble(n_neurons=10, dimensions=1, label="Encoder")
ens_probe = nengo.Probe(ens)
ens_spikes = nengo.Probe(ens.neurons)
nengo.Connection(feature_1, ens)
nengo.Connection(feature_2, ens)
nengo.Connection(feature_3, ens)
nengo.Connection(feature_4, ens)
dt = 1e-3
with nengo.Simulator(model, dt=dt) as sim:
sim.run(dt * 1000) #14900
spike_count = np.sum(sim.data[ens_spikes] > 0, axis=0)
print(f'spike counts are {spike_count}')
Right now I have only four features, so it is alright to have 4 functions for each features (although it is inefficient). But what if I have a csv dataset with 50 features and I want to create a node for each feature?
Is there an efficient way to create nodes for each features in your dataset? In node.py (source code for nengo.Node), it is written
size_in : int
The number of dimensions for incoming connection.
Can we use size_in in nengo.Node?
How, when and why to use size_in in nengo.Node?
I did this:
import nengo
import matplotlib.pyplot as plt
from nengo.utils.ensemble import sorted_neurons
from nengo.utils.matplotlib import rasterplot
import numpy as np
# reading the iris dataset in the csv format
data = np.genfromtxt('Iris.csv', delimiter=',', usecols=(1, 2, 3, 4))
# normalization to unity of each pattern in the data
features = np.apply_along_axis(lambda x: x / np.linalg.norm(x), 1, data[1:len(data), :])
# loading the labels
target = np.genfromtxt('Iris.csv',
delimiter=',',
usecols=[5],
dtype=str)
target = target[1:len(target)]
labels = np.unique(target)
print(labels)
def feat(t):
ix = int(t / dt) % features.shape[0]
return features[ix, :]
model = nengo.Network()
with model:
feature_1 = nengo.Node(output=feat, size_in=4 ,size_out=4,label="Features 1")
feature_1_probe = nengo.Probe(feature_1[0])
feature_2_probe = nengo.Probe(feature_1[1])
feature_3_probe = nengo.Probe(feature_1[2])
feature_4_probe = nengo.Probe(feature_1[3])
ens = nengo.Ensemble(n_neurons=10, dimensions=1, label="Encoder", neuron_type=nengo.LIF(amplitude=10))
ens_probe = nengo.Probe(ens)
ens_spikes = nengo.Probe(ens.neurons)
nengo.Connection(feature_1[0], ens)
nengo.Connection(feature_1[1], ens)
nengo.Connection(feature_1[2], ens)
nengo.Connection(feature_1[3], ens)
dt = 1e-3
with nengo.Simulator(model, dt=dt) as sim:
sim.run(dt * 1000) #14900
spike_count = np.sum(sim.data[ens_spikes] > 0, axis=0)
print(f'spike counts are {spike_count}')
I got the error message
nengo.exceptions.ValidationError: Node.output: output function ‘<function feat at 0x7fce166f7760>’ is expected to accept exactly 2 arguments (time, as a float and data, as a NumPy array)
I also tried
ens = nengo.Ensemble(n_neurons=10, dimensions=4, label=“Encoder”)
It didn’t work.