簡體   English   中英

Tensorflow 條件 x==y 錯誤:A InvalidArgumentError

[英]Tensorflow condition x==y error: A InvalidArgumentError

我正在尋求幫助我得到的錯誤。 我希望輸出為 3 個多類。 所以我做了什么來附加這些輸出並創建一個自定義損失函數來處理它。 但似乎我被形狀卡住了。

class Model(keras.Model):
def __init__(self):
    super(Model, self).__init__()
    self.dense1 = layers.Dense(10, input_shape=(1, 5), activation="relu")
    self.l = []
    self.u = layers.Dense(4, activation="softmax")
    self.l.append(self.u)
    self.c = layers.Dense(4, activation="softmax")
    self.l.append(self.c)
    self.k = layers.Dense(4, activation="softmax")
    self.l.append(self.k)
    self.outputs = layers.Concatenate()

def call(self, inputs):
    x = tf.convert_to_tensor(inputs)
    x = self.dense1(x)
    ls = []
    u = self.u(x)
    ls.append(u)
    c = self.c(x)
    ls.append(c)
    k = self.k(x)
    ls.append(k)
    return self.outputs(ls)

def process(self, observations):
    action_probs = self.predict_on_batch(observations)
    return action_probs

這是我使用的 custom_cross_entropy,它是 3 個 sparse_categorical_crossentropy 的串聯:

def custom_cross_entropy(y_true, y_pred):
l = []
for i in range(3):
    l.append(tf.keras.metrics.sparse_categorical_crossentropy(y_true[:,i].reshape((-1,1)), y_pred[:, 4 * i: 4 * (i+1)].reshape(-1, 1, 4), from_logits=False, axis=-1))
return K.concatenate(l).reshape((-1,1))

我創建了一個虛擬數據來顯示輸出。

X = [[[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]]]
y_t = [[0, 1, 3], [0, 2, 1], [2, 1, 1], [1, 2, 0], [1, 2, 3], [1, 2, 1]]

這是我得到的錯誤。 似乎模型試圖將每個值用作 y_true 值,我不明白為什么。

model = Model()
model.compile(loss=custom_cross_entropy, optimizer='adam', metrics=['accuracy'])
model.fit(X, y_t, epochs=5)

控制台顯示:

Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_37044/2147060259.py in <module>
----> 1 model.fit(X, y_t, epochs=5)

~\anaconda3\lib\site-packages\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)
   1182                 _r=1):
   1183               callbacks.on_train_batch_begin(step)
-> 1184               tmp_logs = self.train_function(iterator)
   1185               if data_handler.should_sync:
   1186                 context.async_wait()

~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
    883 
    884       with OptionalXlaContext(self._jit_compile):
--> 885         result = self._call(*args, **kwds)
    886 
    887       new_tracing_count = self.experimental_get_tracing_count()

~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
    948         # Lifting succeeded, so variables are initialized and we can run the
    949         # stateless function.
--> 950         return self._stateless_fn(*args, **kwds)
    951     else:
    952       _, _, _, filtered_flat_args = \

~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in __call__(self, *args, **kwargs)
   3037       (graph_function,
   3038        filtered_flat_args) = self._maybe_define_function(args, kwargs)
-> 3039     return graph_function._call_flat(
   3040         filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: disable=protected-access
   3041 

~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1961         and executing_eagerly):
   1962       # No tape is watching; skip to running the function.
-> 1963       return self._build_call_outputs(self._inference_function.call(
   1964           ctx, args, cancellation_manager=cancellation_manager))
   1965     forward_backward = self._select_forward_and_backward_functions(

~\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in call(self, ctx, args, cancellation_manager)
    589       with _InterpolateFunctionError(self):
    590         if cancellation_manager is None:
--> 591           outputs = execute.execute(
    592               str(self.signature.name),
    593               num_outputs=self._num_outputs,

~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     57   try:
     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:

InvalidArgumentError:  assertion failed: [Condition x == y did not hold element-wise:] [x (custom_cross_entropy/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [6 1] [y (custom_cross_entropy/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [18 1]
     [[node custom_cross_entropy/SparseSoftmaxCrossEntropyWithLogits/assert_equal_1/Assert/Assert (defined at \AppData\Local\Temp/ipykernel_37044/2925072780.py:6) ]] [Op:__inference_train_function_51479]

Function call stack:
train_function

歡迎任何解決問題的建議。

我認為您在custom_cross_entropy函數中解決了錯誤的維度。 也許嘗試這樣的事情:

import tensorflow as tf

class Model(tf.keras.Model):
  def __init__(self):
      super(Model, self).__init__()
      self.dense1 = tf.keras.layers.Dense(10, input_shape=(1, 5), activation="relu")
      self.l = []
      self.u = tf.keras.layers.Dense(4, activation="softmax")
      self.l.append(self.u)
      self.c = tf.keras.layers.Dense(4, activation="softmax")
      self.l.append(self.c)
      self.k = tf.keras.layers.Dense(4, activation="softmax")
      self.l.append(self.k)
      self.outputs = tf.keras.layers.Concatenate()

  def call(self, inputs):
      x = tf.convert_to_tensor(inputs)
      x = self.dense1(x)
      ls = []
      u = self.u(x)
      ls.append(u)
      c = self.c(x)
      ls.append(c)
      k = self.k(x)
      ls.append(k)
      return self.outputs(ls)

  def process(self, observations):
      action_probs = self.predict_on_batch(observations)
      return action_probs

def custom_cross_entropy(y_true, y_pred):
  l = []
  for i in range(3):
      temp_y_true = tf.reshape(y_true[:,i], (-1,1))
      temp_y_pred = tf.reshape(y_pred[:, :, 4 * i: 4 * (i+1)],(-1, 1, 4))
      tf.print('Fixed solution ::: Y_true -->', tf.shape(temp_y_true), 'Y_pred -->', tf.shape(temp_y_pred))

      temp_y_true_wrong = tf.reshape(y_true[:,i], (-1,1))
      temp_y_pred_wrong = tf.reshape(y_pred[:, 4 * i: 4 * (i+1)],(-1, 1, 4))
      tf.print('Wrong solution ::: Y_true -->', tf.shape(temp_y_true_wrong), 'Y_pred -->', tf.shape(temp_y_pred_wrong))

      l.append(tf.keras.metrics.sparse_categorical_crossentropy(temp_y_true, temp_y_pred, from_logits=False, axis=-1))
  return tf.reshape(tf.keras.backend.concatenate(l), (-1,1))

X = [[[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]], [[1, 2, 3, 4, 5]]]
y_t = [[0, 1, 3], [0, 2, 1], [2, 1, 1], [1, 2, 0], [1, 2, 3], [1, 2, 1]]

model = Model()
model.compile(loss=custom_cross_entropy, optimizer='adam', metrics=['accuracy'])
model.fit(X, y_t, epochs=5)
Epoch 1/5
Fixed solution ::: Y_true --> [6 1] Y_pred --> [6 1 4]
Wrong solution ::: Y_true --> [6 1] Y_pred --> [18 1 4]
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM