训练tensorflow2 LSTM RNN时出现“函数调用堆栈:train_function -> train_function”错误

时间:2021-05-06 05:14:56

标签: python tensorflow

尝试训练我的模型时出现以下错误:





     (13369344,) 
     C
     (44733312,)
     Epoch 1/5
     D
     (12053504,)
     InvalidArgumentError                      Traceback (most recent call last)
    <ipython-input-45-e13f72904f51> in <module>()
         1 model.fit(x=modelTrainGen(maxLen),
         2           epochs=5,
     ---> 3           batch_size=1)

    6 frames
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, 
    batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, 
    sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, 
    validation_freq, max_queue_size, workers, use_multiprocessing)
       1098                 _r=1):
       1099               callbacks.on_train_batch_begin(step)
    -> 1100               tmp_logs = self.train_function(iterator)
       1101               if data_handler.should_sync:
       1102                 context.async_wait()

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, 
    *args, **kwds)
        826     tracing_count = self.experimental_get_tracing_count()
        827     with trace.Trace(self._name) as tm:
    --> 828       result = self._call(*args, **kwds)
        829       compiler = "xla" if self._experimental_compile else "nonXla"
        830       new_tracing_count = self.experimental_get_tracing_count()

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, 
    **kwds)
        886         # Lifting succeeded, so variables are initialized and we can run the
        887         # stateless function.
     --> 888         return self._stateless_fn(*args, **kwds)
        889     else:
        890       _, _, _, filtered_flat_args = \

     /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in __call__(self, *args, 
     **kwargs)
       2941        filtered_flat_args) = self._maybe_define_function(args, kwargs)
       2942     return graph_function._call_flat(
    -> 2943         filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: 
    disable=protected-access
       2944 
      2945   @property

     /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _call_flat(self, args, 
    captured_inputs, cancellation_manager)
        1917       # No tape is watching; skip to running the function.
       1918       return self._build_call_outputs(self._inference_function.call(
     -> 1919           ctx, args, cancellation_manager=cancellation_manager))
        1920     forward_backward = self._select_forward_and_backward_functions(
        1921         args,

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in call(self, ctx, args, 
    cancellation_manager)
        558               inputs=args,
        559               attrs=attrs,
     --> 560               ctx=ctx)
        561         else:
        562           outputs = execute.execute_with_cancellation(

     /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, 
     num_outputs, inputs, attrs, ctx, name)
         58     ctx.ensure_initialized()
         59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
    ---> 60                                         inputs, attrs, num_outputs)
         61   except core._NotOkStatusException as e:
         62     if name is not None:

    InvalidArgumentError: 2 root error(s) found.
      (0) Invalid argument:  indices[8618190,0] = -1 is not in [0, 44733312)
         [[node sequential_13/embedding_13/embedding_lookup (defined at <ipython-input-45- 
     e13f72904f51>:3) ]]
         [[Adam/Adam/update/AssignSubVariableOp/_33]]
       (1) Invalid argument:  indices[8618190,0] = -1 is not in [0, 44733312)
          [[node sequential_13/embedding_13/embedding_lookup (defined at <ipython-input-45- 
     e13f72904f51>:3) ]]
    0 successful operations.
    0 derived errors ignored. [Op:__inference_train_function_149727]

    Errors may have originated from an input operation.
    Input Source operations connected to node sequential_13/embedding_13/embedding_lookup:
     sequential_13/embedding_13/embedding_lookup/144654 (defined at /usr/lib/python3.7/contextlib.py:112)

    Input Source operations connected to node sequential_13/embedding_13/embedding_lookup:
     sequential_13/embedding_13/embedding_lookup/144654 (defined at /usr/lib/python3.7/contextlib.py:112)

    Function call stack:
    train_function -> train_function
    C
    (44733312,)




这是我的代码:

型号:




    model = keras.Sequential()
    # Add an Embedding layer expecting input vocab of size 1000, and
     # output embedding dimension of size 64.
    model.add(layers.Embedding(input_dim=maxLen, output_dim=2,mask_zero=True))
    #model.add(layers.Masking())
    # Add a LSTM layer with 128 internal units.
    #model.add(layers.Input(shape=[1,None]) )
    model.add( layers.LSTM(8,return_sequences=True))
    model.add( layers.Dropout(0.2) )

    model.add( layers.LSTM(8))
    model.add( layers.Dropout(0.2) )

    # Add a Dense layer with 10 units.
   model.add(layers.Dense(16,activation="relu"))
   model.add(layers.Dropout(0.2))

    model.add(layers.Dense(2,activation="softmax"))

    model.compile(loss='categorical_crossentropy',
              optimizer='adam')




生成器:





      def modelTrainGen(maxLen):
      bmTrainDirectory = '/content/drive/MyDrive/death_black_classifier/black_metal_train/'
      dmTrainDirectory = '/content/drive/MyDrive/death_black_classifier/death_metal_train/'

      dmTrainFileNames = os.listdir(dmTrainDirectory)
      bmTrainFileNames = os.listdir(bmTrainDirectory)
      maxAudioLen = maxLen
      bmTensor = tf.convert_to_tensor([[1],[0]])
      dmTensor = tf.convert_to_tensor([[0],[1]])
      allFileNames = []

      for fileName in zip(bmTrainFileNames,dmTrainFileNames):
        bmFileName = fileName[0]
        dmFileName = fileName[1]
        allFileNames.append((bmFileName,1))
        allFileNames.append((dmFileName,0))


       random.shuffle(allFileNames)
       for fileNameVal in allFileNames: 

        fileName = fileNameVal[0]
        val = fileNameVal[1]

        if val == 1:
          bmFileName = fileName
          audio = tfio.audio.AudioIOTensor(bmTrainDirectory + bmFileName)
          audio_slice = tf.reduce_max(tf.transpose(audio[0:]),0)
          del audio
          print(audio_slice.shape)
          padded_x = tf.keras.preprocessing.sequence.pad_sequences( [audio_slice], padding="post", 
      dtype=float,maxlen=maxAudioLen )
          del audio_slice
          converted = tf.convert_to_tensor(padded_x[0])
          del padded_x
          print("A")
          print(converted.shape)
          yield ( converted,bmTensor)
          print("B")
          del converted
        
      
          else:
          dmFileName = fileName
          audio = tfio.audio.AudioIOTensor(dmTrainDirectory + dmFileName)
          audio_slice = tf.reduce_max(tf.transpose(audio[0:]),0)
          del audio
          print(audio_slice.shape)
          padded_x = tf.keras.preprocessing.sequence.pad_sequences( [audio_slice], padding="post", 
       dtype=float,maxlen=maxAudioLen )
          del audio_slice
          converted = tf.convert_to_tensor(padded_x[0])
          del padded_x
          print("C")
          print(converted.shape)
          yield ( converted,dmTensor)
          print("D")
          del converted




这个 train_function 错误是什么意思?我看过的其他解决方案到目前为止还没有奏效。我正在尝试制作一个分类器,可以对两种不同类型的歌曲进行分类。如果它看起来不是很好,也可以随意评论架构。单元如此之少的原因是为了确保这不是 RAM 问题。

0 个答案:

没有答案