実現したいこと
ここに実現したいことを箇条書きで書いてください。
- [1] python / keras 環境にてディープラーニングを実現したい。
- [2] model.fit をおこなうとEpoch 1/5 で止まってしまうので、対応策を確認したい。
前提
Pythonを使用してcsvからOneHotエンコーディングまでは完了したデータがあります。
計算データはは824列、結果データは3列(OneHot済み)でこれから機械学習を行うところです。
発生している問題・エラーメッセージ
例外が発生しました: InvalidArgumentError Graph execution error: Detected at node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits defined at (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main ---- logits and labels must have the same first dimension, got logits shape [32,3] and labels shape [96] [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_758] ------ logits and labels must have the same first dimension, got logits shape [32,3] and labels shape [96] [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_758]
該当のソースコード
Python
1 2 dfload=pd.DataFrame() 3 dfload = pd.read_csv(r"D:\exportHot.csv") 4 5 #インデクスリセット 6 print("reset_index") 7 dfload.reset_index(inplace=True) 8 #数値に変換 9 dfload.astype('float32') 10 11 #データをy:結果データとx:計算データに分割します。 12 y = dfload[['Ret_1','Ret_2','Ret_3']] 13 x = dfload.drop(['Ret_1','Ret_2','Ret_3'], axis = 1) 14 15 #学習用とテスト用に分割します。 16 print("train_test_split") 17 x_train, x_test, y_train, y_test = train_test_split(x, y,train_size=0.8) 18 19 print(x_train.shape) 20 print(x_test.shape) 21 print(y_train.shape) 22 print(y_test.shape) 23 in_size=x_train.shape[1] 24 out_size=y_train.shape[1] 25 26 # modelを準備する 27 model = keras.models.Sequential() 28 model.add(Dense(1024, activation='relu', input_shape=(in_size,))) 29 30 31 # RELUにて計算 32 model.add(Dense(512,activation="relu")) 33 34 35 # 出力用に合計が100%となるようにsoftmaxをかませる。 36 model.add(Dense(out_size,activation="softmax")) 37 38 # 作成されたパラメタ数を表示します。 39 model.summary() 40 41 # コンパイルする(この辺はほ呪文です) 42 model.compile(optimizer="adam", 43 loss="sparse_categorical_crossentropy", 44 metrics=["accuracy"]) 45 46 # ▼ここの行でエラーが発生する。 47 history = model.fit(x_train, y_train, epochs=5, 48 validation_data=(x_test, y_test)) 49 50 51 test_loss, test_acc =model.evaluate(x_test, y_test) 52 print(f"テストデータの正解率は{test_acc:.2%}です。") 53 54
上記の出力結果
reset_index train_test_split (3177, 824) (795, 824) (3177, 3) (795, 3) 2023-12-06 15:37:18.009341: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ========================================= dense (Dense) (None, 1024) 844800 dense_1 (Dense) (None, 512) 524800 dropout (Dropout) (None, 512) 0 dense_2 (Dense) (None, 3) 1539 ========================================= Total params: 1371139 (5.23 MB) Trainable params: 1371139 (5.23 MB) Non-trainable params: 0 (0.00 Byte) _________________________________________________________________ Epoch 1/5 Backend TkAgg is interactive backend. Turning interactive mode on.
試したこと
model.compile の見直し、再入力
最初の model.add にて、 input_shape を input(824) に書き換えても状況が変わりません。
補足情報(FW/ツールのバージョンなど)
Windows 10 Pro
Python3
「logitsは[32,3]なのにlabelsは[96]」とエラーが言っているのだから、y_train(やy_test)が[32,3]ではなく[96]になっているのでは?
> print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
の実行結果は、それぞれ何て表示されたでしょうか?
ありがとうございます。
表示されたログを含めて記載しています。
なにがおかしいのか・・・いろいろ本も漁っております。
reset_index
train_test_split
(3177, 824)
(795, 824)
(3177, 3)
(795, 3)
2023-12-06 15:37:18.009341: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 1024) 844800
dense_1 (Dense) (None, 512) 524800
dropout (Dropout) (None, 512) 0
dense_2 (Dense) (None, 3) 1539
=================================================================
Total params: 1371139 (5.23 MB)
Trainable params: 1371139 (5.23 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/5
Backend TkAgg is interactive backend. Turning interactive mode on.

回答1件
あなたの回答
tips
プレビュー