————————————————————————— InvalidArgumentError Traceback (most recent call last) Cell In[212], line 34 31 model.compile(optimizer=’adam’, loss=’sparse_categorical_crossentropy’, metrics=[‘accuracy’]) 33 # モデルのトレーニング —> 34 history = model.fit(X_train_scaled, y_train, epochs=5, batch_size=32, validation_data=(X_test_scaled, y_test)) 36 # テストデータでの予測と評価 37 loss, accuracy = model.evaluate(X_test_scaled, y_test) File /home/share/temp/venv/lib/python3.8/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs) 67 filtered_tb = _process_traceback_frames(e.__traceback__) 68 # To get the full stack trace, call: 69 # `tf.debugging.disable_traceback_filtering()` —> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb File /home/share/temp/venv/lib/python3.8/site-packages/tensorflow/python/eager/execute.py:53, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 51 try: 52 ctx.ensure_initialized() —> 53 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, 54 inputs, attrs, num_outputs) 55 except core._NotOkStatusException as e: 56 if name is not None: … File “/home/share/temp/venv/lib/python3.8/site-packages/keras/src/backend.py”, line 5762, in sparse_categorical_crossentropy res = tf.nn.sparse_softmax_cross_entropy_with_logits( Node: ‘sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits’ Received a label value of 160 which is outside the valid range of [0, 6). Label values: 60 80 80 160 60 100 60 60 100 80 60 60 120 60 60 60 60 100 100 100 160 140 100 160 100 100 100 120 80 120 160 60 [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_159060] Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings…
エラーメッセージから、ラベル値の範囲が[0, 6)の範囲外であることが示されています。これは、モデルの出力として想定されるクラス数よりも大きな値がラベルとして与えられていることを意味します。
この問題を解決するには、モデルの出力層のユニット数をクラスの数に一致させる必要があります。そのため、num_classesを正しく取得していることを確認し、出力層のユニット数を修正します。
次に示すように、出力層のユニット数を修正してください:
python
# モデルの作成 model = Sequential([ Dense(64, activation='relu', input_shape=(X_train_scaled.shape[1],)), Dense(64, activation='relu'), Dense(num_classes, activation='softmax') # 多クラス分類用の出力層 ])
この修正を行うことで、モデルが正しく訓練されるはずです。

ディスカッション
コメント一覧
まだ、コメントがありません