【问题标题】:tensorflow.python.framework.errors_impl.InvalidArgumentError: Received a label value of 357436800 which is outside the valid range of [0, 2)tensorflow.python.framework.errors_impl.InvalidArgumentError:收到的标签值 357436800 超出 [0, 2) 的有效范围
【发布时间】:2021-06-08 17:47:07
【问题描述】:

我正在尝试使用 keras 编写神经网络。 以下是我导入库和模块的方式:

import keras
import os
import numpy as np
from keras.models import Sequential
from keras.layers import Activation
from keras.layers.core import Dense
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D

我希望它输出输入的继任者。例如,如果我输入 5,它应该输出 6。 我为神经网络创建了一个具有这种格式的训练数据集:

data = [2, 5, 12, 300, 123, 52, 8, 64, 112, 6452746, 12638, 799378, 69967, 654, 89, 61, 24, 60, 40, 20385, 999, 764, 7, 357436786]
labels = list(item+1 for item in data)
scaled_train_samples = np.array(data)
train_labels = np.array(labels)

我尝试根据一些数据来测试我的模型。我想在训练后输入 94,希望它能输出接近 95 的东西。

所以我写了以下内容:

scaled_test_samples = np.array([94])

然后,我在某处读到要修复我的错误,我需要添加这行代码,所以我在这里添加:

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

它没有解决我的错误,但我也懒得去删除它。

接下来,我使用以下代码定义我的模型:

model = Sequential([
    Dense(units=16, input_shape=(1,), activation='relu'),
    Dense(units=32, activation='relu'),
    Dense(units=2, activation='sigmoid')
])

我已经定义了一个基本的 3 层神经网络。接下来,我用下面的代码编译它:

model.compile(
    optimizer=Adam(learning_rate=0.0001), 
    loss='sparse_categorical_crossentropy', 
    metrics=['accuracy']
)

一旦我准备好定义和编译的 keras 神经网络,在预测或测试它之前,我会拟合模型。 我使用以下代码:

model.fit(
    x=scaled_train_samples, 
    y=train_labels, 
    batch_size=2, 
    epochs=11, 
    shuffle=True, 
    verbose=2
)

现在,我终于完成了我的神经网络。我现在尝试预测和测试 94 是否输出 95:

predictions = model.predict(
    x=scaled_test_samples, 
    batch_size=10, 
    verbose=1
) 

当然,我打印我的预测:

print(predictions)

以下是一些基本信息,如果有帮助的话:

操作系统:Windows 10 显卡:没有 IDE:Visual Studio 代码 解释器:Python 3.8.8 64 位

我尝试在 linux 上运行它,并在 repl.it 上运行过一次,但它仍然会产生错误。下面是输出。

首先,它显示了一些奇怪的错误,但我忽略了它,因为我没有使用 GPU:

2021-03-08 14:08:11.945167: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-03-08 14:08:11.945684: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.

然后是真正的错误。错误似乎来自训练过程,因为错误在训练输出“epoch 1/11”开始后立即开始。

2021-03-08 14:08:15.192424: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-03-08 14:08:15.193919: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2021-03-08 14:08:15.194352: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)   
2021-03-08 14:08:15.199531: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: LAPTOP-78M3QUGV
2021-03-08 14:08:15.200385: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: LAPTOP-78M3QUGV
2021-03-08 14:08:15.201202: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-03-08 14:08:15.202400: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-03-08 14:08:15.285471: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
Epoch 1/11
2021-03-08 14:08:15.636802: W tensorflow/core/framework/op_kernel.cc:1763] OP_REQUIRES failed at sparse_xent_op.cc:90 : Invalid argument: Received a label value of 357436800 which is outside the valid range of [0, 2).  Label values: 357436800 13
Traceback (most recent call last):
  File "c:/Users/Maanav/Desktop/New folder/add.py", line 31, in <module>
    model.fit(
  File "C:\Users\Maanav\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit
    tmp_logs = self.train_function(iterator)
  File "C:\Users\Maanav\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\Maanav\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 888, in _call
    return self._stateless_fn(*args, **kwds)
  File "C:\Users\Maanav\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 2942, in __call__
    return graph_function._call_flat(
  File "C:\Users\Maanav\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 1918, in _call_flat
    return self._build_call_outputs(self._inference_function.call(
  File "C:\Users\Maanav\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 555, in call
    outputs = execute.execute(
  File "C:\Users\Maanav\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\execute.py", line 59, 
in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Received a label value of 357436800 which is outside the valid range of [0, 2).  Label values: 357436800 13
         [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at c:/Users/Maanav/Desktop/New folder/add.py:31) ]] [Op:__inference_train_function_614]

Function call stack:
train_function

感谢您抽出宝贵时间阅读此问题!任何帮助都深表感谢! 和平相处。

【问题讨论】:

    标签: python python-3.x tensorflow machine-learning keras


    【解决方案1】:

    您的目标标签需要为零索引,并且最大值需要对应于您的输出单位才能使用CategoricalCrossentropy。 Tensorflow 知道你有 2 个输出单元,而 6452746 是你的目标类别之一,所以它不明白。

    这是一个回归问题,所以使用 1 个输出单元并使用loss='MAE' 或其他一些回归损失函数,没有最终激活函数。

    import tensorflow as tf
    import numpy as np
    
    x = np.arange(1000)
    y = x + 1
    
    
    model = tf.keras.Sequential([
        tf.keras.layers.Dense(units=16, input_shape=(1,), activation='relu'),
        tf.keras.layers.Dense(units=32, activation='relu'),
        tf.keras.layers.Dense(units=1)
    ])
    
    model.compile('adam', 'mae')
    
    history = model.fit(x, y, epochs=500, verbose=0)
    
    model.predict([96])
    
    array([[97.32837]], dtype=float32)
    

    您可以将其作为分类问题来解决,但您需要将目标转换为零索引类别。

    【讨论】:

      猜你喜欢
      • 2022-06-29
      • 1970-01-01
      • 2020-04-03
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 2021-04-28
      • 1970-01-01
      相关资源
      最近更新 更多