I'm creating a neural net, but I get errors in the loss function and SGD update program. Output y has six labels from 0 to 5, each with one-hot expressions like [1,0,0,0,0] (label 0).torch.Size([19573,6]).
Please let me know how to fix the error below.
grads['dl/df'][y]-=1
IndexError: index1 is out of bounds for dimension 0 with size 1
import torch
from bindsnet.network import Network
from bindsnet.network.nodes import Input, LIFNodes
from bindsnet.network.topology import Connection
from bindsnet.network.monitors import Monitor
import numpy as np
# invocation of pytorch
# Call the network directory in bindsnet and learn about all the key elements
# Invoke nodes.py in the network directory of bindsnet
# Create layers, in this case create layers of LIF neurons
# Network Element Configuration Topology
# Call connection for topology class in bindsnet network directory
# Call Monitor in monitor Class in network directory in bindsnet
time = 25
# Creating a Network
network=Network()
# Create a population of two neurons, one acting as a source
# The other is to create five layers of target neurons.
input=Input(n=64, shape=[1,64], sum_input=True)#n=64 equals the input size
middle=LIFNodes (n=40, trace=True, sum_input=True)
center=LIFNodes (n=40, trace=True, sum_input=True)
final=LIFNodes (n=40, trace=True, sum_input=True)
out=LIFNodes(n=6, sum_input=True)#n=6 should equal the number of labels
# Layer-to-layer connection
input_middle=connection(source=inpt, target=middle, wmin=0, wmax=1e-1)
middle_center= Connection (source=middle, target=center, wmin=0, wmax=1e-1)
center_final=Connection (source=center, target=final, wmin=0, wmax=1e-1)
final_out=Connection (source=final, target=out, wmin=0, wmax=1e-1)
# Connect all 5 layers to the network
network.add_layer(empt,name='A')
network.add_layer(middle, name='B')
network.add_layer(center,name='C')
network.add_layer(final, name='D')
network.add_layer(out,name='E')
forward_connection=Connection(source=inpt, target=middle, w=0.05+0.1*torch.randn(inpt.n,middle.n))
network.add_connection(connection=forward_connection, source="A", target="B")
forward_connection=Connection(source=middle, target=center, w=0.05+0.1*torch.randn(middle.n,center.n))
network.add_connection(connection=forward_connection, source="B", target="C")
forward_connection=Connection(source=center, target=final, w=0.05+0.1*torch.randn(center.n,final.n))
network.add_connection(connection=forward_connection, source="C", target="D")
forward_connection=Connection(source=final, target=out, w=0.05+0.1*torch.randn(final.n, out.n))
network.add_connection(connection=forward_connection, source="D", target="E")
current_connection=Connection(source=out, target=out, w=0.025*(torch.eye(out.n)-1), )
network.add_connection(connection=current_connection, source="E", target="E")
# Create Monitor for input and output layers only (record voltage and spikes)
apt_monitor=Monitor(obj=impt, state_vars=("s", "v"), time=500, )
middle_monitor=Monitor(obj=impt, state_vars=("s", "v"), time=500, )
center_monitor=Monitor(obj=impt, state_vars=("s", "v"), time=500, )
final_monitor=Monitor(obj=impt, state_vars=("s", "v"), time=500, )
out_monitor=Monitor(obj=impt, state_vars=("s", "v"), time=500, )
# Connect Monitor to the Network
network.add_monitor(monitor=impt_monitor, name="A")
network.add_monitor(monitor=middle_monitor, name="B")
network.add_monitor(monitor=center_monitor, name="C")
network.add_monitor(monitor=final_monitor, name="D")
network.add_monitor(monitor=out_monitor, name="E")
for lin network.layers:
m=Monitor(network.layers[l], state_vars=['s'], time=time)
network.add_monitor(m,name=l)
# Load training data
npzfile=np.load("C:/Users/name/Desktop/myo-python-1.0.4/myo-armband-nn-master/data/train_set.npz")
x = npzfile['x'] # Load data ndarray type 1 x 64 arrays
y=npzfile['y']# Load data array of ndarry type 1x6
# Convert to tensor type
x=torch.from_numpy(x).clone()#x is a 1x64 sensor array
y=torch.from_numpy(y).clone()#y is a 1x6 sensor array
# Start of stored training data and store spikes and labels for each neuron
# reflect and store data (one neuron spike and label) in pairs
grads = {}
lr,lr_decay = 1e-2,0.95
criterion=torch.nn.CrossEntropyLoss()#Calculating the Cross Loss Function
spoke_ims, spoke_axes, weight_im = None, None, None
for i, (x,y) in enumerate (zip(x.view(-1,64),y)):
# repeat function (element, array, number of iterations) E generates (time x 1 matrix of hours) (time = 25)i is the index number.
inputs = {'A':x.repeat(time,1), 'E_b':torch.ones(time,1)}
network.run (inputs=inputs, time=time)
# put the spikes together from the whole layer ('s' is a spike)
y=torch.tensor(y).long()
spikes={l:network.monitors[l].get('s') for lin network.layers}
# compile input from all layers
summarized_inputs={l:network.layer[l].summed for lin network.layer}
# softmax function of output, obtaining predictive label
output=spikes['E'].sum(-1).float().softmax(0).view(1,-1)
predicted=output.argmax(1).item()
# Loss and SGD Updates
grads['dl/df'] = summed_inputs['E'].softmax(0)
grads ['dl/df'] [y] - = 1☚ Here it is.
grads['dl/dw'] = torch.ger (summed_inputs['A', grads['dl/df'])
network.connections ['A', 'B', 'C', 'D', 'E'].w-=lr*grads ['dl/dw']
# damping rate
if i>0 and i%300 == 0:
lr = lr_decay
network.reset_()
As shown in the error message, the size of grads['dl/df'] is 1, so index 1 cannot be specified.
For better answers, please refer to this as well.
© 2024 OneMinuteCode. All rights reserved.