Home > Blockchain >  IndexError: list index out of range [Tensorflow 1.5 & Python 3.6]
IndexError: list index out of range [Tensorflow 1.5 & Python 3.6]

Time:08-16

Currently, I'm working on implementing code in "Differentially Private Federated Learning: A Client Level Perspective" where the GitHub link is LINK. However, I followed the instruction but got an error which is

File "/content/drive/MyDrive/Colab_Notebooks/machine-learning-diff-private-federated-learning-main/Helper_Functions.py", line 217, in load_from_directory_or_initialize
Accuracy_accountant = Lines[-1]
IndexError: list index out of range

The part of the code is

if os.path.isfile(directory   '/specs.csv'):
  with open(directory   '/specs.csv', 'rb') as csvfile:
      reader = csv.reader(csvfile)
      Lines = []
      for line in reader:
          Lines.append([float(j) for j in line])

      Accuracy_accountant = Lines[-1]
      Delta_accountant = Lines[1]

I don't think there's an error. I'm using Tensorflow 1.5 and python 3.6. Feel free the check the code here. Thank you so much in advance!!

CodePudding user response:

Remove open() method and make your own csv directory file (example: path=directory '/specs.csv' ) The problem is from reader, I think reader is empty so Lines will be empty and it will give you that error. Instead, try this:

csvfile = directory   '/specs.csv'
reader = csv.reader(csvfile)
Lines = []
for line in reader:
      Lines.append([float(j) for j in line])

Accuracy_accountant = Lines[-1]
Delta_accountant = Lines[1]

CodePudding user response:

import pandas as pd

csvfile = 'specs.csv'
reader = pd.read_csv(csvfile)
Lines = []
for line in reader:
      Lines.append([float(j) for j in line])

Accuracy_accountant = Lines[-1]
Delta_accountant = Lines[1]

CodePudding user response:

I cannot add a comment, so I will answer it here. In Lines[-1] you are accessing the last value of the list Lines. Try printing the reader to see if you read the input correctly, or the file might be empty. Also, how is the file split? Is it split by '\n' or it by ' '? Also then you are accessing List[1]. If the list has only 1 element this will fail as well.

  • Related