Ошибка Theano IndexEr: индекс 206 выходит за пределы для оси 1 с размером 1 - PullRequest
0 голосов
/ 24 августа 2018
  • Итак, это мой код:

    #+BEGIN_SRC ipython :session
    net = NeuralNet(
            output_node_size = 10,
            hidden_layers_node_size = [512])
    #+END_SRC
    
    import csv
    
    import pandas as pd
    
    import numpy as np
    
    from keras import preprocessing as pre
    
    list_of_data_sources = [
    'Data/incrementby10.txt',
    'Data/incrementby20.txt',
    'Data/incrementby2.txt',
    'Data/incrementby30.txt',
    'Data/incrementby40.txt',
    'Data/incrementby5.txt',
    'Data/random10reps.txt',
    'Data/random5reps.text'
    ]
    
    count = 1
    
    '''
    TODO: Need to find a way to use only 70% of data so 30$ can be used as validation
    TODO: figure out what least common denom each len(X_data) has with each other so that we know ho many sequences we need
    
    
    '''
    
    
    
    for i in list_of_data_sources:
    
        with open(i, 'r') as csvfile:
    
            data = {'pitch':[],
              'drive':[],
              'input':[]}
    
            spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
            for row in spamreader:
                if len(row) is 5:
                    del row[0]
                    assert(len(row)==4)
    
    
                if len(row) is not 4:
                    continue
    
                if (row[2].split(' ')[-1]=='inp'):
                    continue
                if (row[3].split(' ')[-1]=='inp'):
                    continue
                if len(row[3].split(' ')[-1]) > 5:
                    continue
    
                data['pitch'].append(row[0].split(' ')[-1])
                #print("oh no: %s" % row[2].split(' ')[-1])
                data['drive'].append(row[2].split(' ')[-1])
                #print("oh god: %s" % (row[0].split(' ')[-1]))
                data['input'].append(row[3].split(' ')[-1])
                #print("oh lord: %s" % (row[3].split(' ')[-1]))
    
    
                #print(len(data['pitch']))
    
                #ummmmm = pre.sequence.TimeSeriesGenerator(data, ..., sampling_rate = 0.01, length = len(data['input']), start_index = 0, end_index = 7/10 * len(data))
            #print(data)
    
    
            pandas_frame = pd.DataFrame.from_dict(data)
            X_data = pandas_frame[['pitch','input']].values
            #print(X_data.shape)
            timeBoi = [i/100 for i in range(X_data.shape[0])]
            time = np.asarray(timeBoi)
            time = time.reshape(time.shape[0],1)
            #print(timeBoi)
            X_data = X_data.reshape(1, X_data.shape[0], 1, X_data.shape[1], 1)
            Y_Data = pandas_frame['drive'].values
            # print(Y_Data.shape)
            Y_Data = Y_Data.reshape(1,Y_Data.shape[0],1)
    
            #print(X_data.shape)
            #print(Y_Data.shape)
    
    
            #count+=1
    
            net.train(X_data,Y_Data,epochs=6)
    

Это сеть (я использую emacs ob-ipython);

neural.py Класс NeuralNet (объект):

          def __init__(self,
                       input_node_size = None,               # Number of nodes in input layer
                       output_node_size = None,              # Number of nodes in output layer
                       input_shape = None,
                       hidden_layers_node_size = []          # Number of nodes in each hidden layer
              ):
                          <<NeuralNet_init>>
          <<NeuralNet_train>>
          <<NeuralNet_run>>
          <<NeuralNet_label>>

** init

Последовательная модель представляет собой линейный стек слоев. Мы передаем ему список экземпляров слоев, чтобы создать нейронную сеть.

      from keras.models import Sequential
      from keras import regularizers
      self.model = Sequential()

Давайте импортируем основные слои из Keras, которые почти всегда используются.

      from keras.layers import Dense, Dropout, Activation, ConvLSTM2D, Reshape

Модель должна знать, какую форму ввода она должна ожидать. По этой причине мы разделяем входной размер для первого слоя.

      # First layer requires input dimension ie input_shape
      self.model.add(
                     ConvLSTM2D(filters = 1, 
                                kernel_size = (2, 2),
                                padding='same',
                                input_shape=(None,1,2,1),
                                kernel_initializer='random_uniform',
                                bias_initializer='zeros',
                                kernel_regularizer = regularizers.l2(.01),
                                activity_regularizer = regularizers.l1(.01),
                                return_sequences = True

                           )
                     )
      self.model.add(Activation('relu'))

     #self.model.add(Flatten())


      # Add layers to model for all hidden layers
      for node_size in hidden_layers_node_size:
          self.model.add(
                         Dense(units=node_size)
                        )
          self.model.add(Activation('relu'))
          self.model.add(Dropout(0.3))

Добавление регуляризатора не улучшает модель

      # Last layer requires activation to be softmax
      self.model.add(Reshape((-1,1)))
      self.model.add(
                     Dense(units=1,
                           activation='softmax'
                           )
                    )





      # Compile model
      self.model.compile(loss='sparse_categorical_crossentropy',
                         optimizer='adam',
                         metrics=['accuracy'])
      #model.fit(x_train, y_train, epochs=5, batch_size=32)

** поезд

подходит для модели с обучающими наборами данных

Входы: train_x - тренировочные данные train_y - обучающие метки epochs - количество итераций по объему требуемых данных x и y

возвращает: Ничего

def train(self, train_x, train_y, epochs):
    self.model.fit(train_x, train_y, epochs)

** пробег

оценивает модель с тестовыми данными

Входы: Х - данные испытаний Y - тестовые метки steps - количество итераций по всему набору данных до завершения оценки

возвращает: метрики - тестовые потери, а также метрика, определенная в init , что в данном случае является точностью

def run(self, X, Y, steps):
    metrics = []
    metrics = self.model.evaluate(X, Y, batch_size = 32, steps = steps)
    return metrics

** этикетка

предсказывает метки данных, указанных

Входы: X - немаркированные данные испытаний steps - количество итераций по всему набору данных до завершения оценки

возвращает: предсказания - бесчисленное множество предсказаний

def label(self, X, steps):
    predictions = self.model.predict(X, batch_size = 32, steps = steps)
    return predictions

Это форма данных:

X_data -> (1, 2682, 1, 2, 1) // следует этому формату

Y_Data -> (1, 2682, 1)

(1, 2043, 1, 2, 1) (1, 2043, 1)

(1, 19764, 1, 2, 1) (1, 19764, 1)

(1, 1261, 1, 2, 1) (1, 1261, 1)

(1, 4848, 1, 2, 1) (1, 4848, 1)

(1, 34220, 1, 2, 1) (1, 34220, 1)

(1, 18245, 1, 2, 1) (1, 18245, 1)

(1, 10196, 1, 2, 1) (1, 10196, 1)

Почему-то я получаю эту ошибку:

IndexError: index 206 is out of bounds for axis 1 with size 1
Apply node that caused the error: AdvancedIncSubtensor{inplace=False,      set_instead_of_inc=True}(Alloc.0, TensorConstant{1},    ARange{dtype='int64'}.0, Elemwise{Cast{int32}}.0)
Toposort index: 134
Inputs types: [TensorType(float32, matrix), TensorType(int8, scalar),     TensorType(int64, vector), TensorType(int32, vector)]
Inputs shapes: [(2682, 1), (), (2682,), (2682,)]
Inputs strides: [(4, 4), (), (8,), (4,)]
Inputs values: ['not shown', array(1, dtype=int8), 'not shown', 'not shown']
Outputs clients: [[Reshape{3}(AdvancedIncSubtensor{inplace=False,  set_instead_of_inc=True}.0, MakeVector{dtype='int64'}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2901, in run_ast_nodes
if self.run_code(code, result):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-52-54d679ced0ce>", line 3, in <module>
hidden_layers_node_size = [512])
  File "<ipython-input-51-ad7af15322e6>", line 55, in __init__
metrics=['accuracy'])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 333, in compile
sample_weight, mask)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_utils.py", line 403, in weighted
score_array = fn(y_true, y_pred)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/losses.py", line 73, in sparse_categorical_crossentropy
return K.sparse_categorical_crossentropy(y_true, y_pred)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/theano_backend.py", line 1657, in sparse_categorical_crossentropy
target = T.extra_ops.to_one_hot(target, nb_class=output.shape[-1])

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

Я понимаю, что он почему-то пытается получить индекс 206 из (..., 1), но не знаю, как это исправить ... Пожалуйста, помогите! (Также извините за небрежный код, не знаю, почему он не думает, что некоторые из них не являются python, а некоторые есть)

...