我正在尝试使用Keras编写一个神经网络。以下是我导入库和模块的方式:
import kerasimport osimport numpy as npfrom keras.models import Sequentialfrom keras.layers import Activationfrom keras.layers.core import Densefrom keras.optimizers import Adamfrom keras.metrics import categorical_crossentropyfrom keras.preprocessing.image import ImageDataGeneratorfrom keras.layers.normalization import BatchNormalizationfrom 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')])
我定义了一个基本的三层神经网络。接下来,我用以下代码编译它:
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 10GPU:无IDE:Visual Studio Code解释器: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 found2021-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 set2021-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 found2021-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-78M3QUGV2021-03-08 14:08:15.200385: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: LAPTOP-78M3QUGV2021-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: AVX2To 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 set2021-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/112021-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 13Traceback (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
感谢您花时间阅读这个问题!任何帮助都深表感谢!祝好!
回答:
你的目标标签需要从零开始索引,并且最大值需要与你的输出单元相对应,以便使用CategoricalCrossentropy
。TensorFlow知道你有2个输出单元,而6452746
是你的目标类别之一,所以它无法理解。
这是一个回归问题,因此使用1个输出单元,并使用loss='MAE'
或其他回归损失函数,不需要最终的激活函数。
import tensorflow as tfimport numpy as npx = np.arange(1000)y = x + 1model = 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)
你可以将其作为分类问题来处理,但你需要将你的目标转换为从零开始的类别。