機械学習でエラーが起きます。
エラー内容やどこを直せばいいかわかる方いましたら教えてください
ソース
python
1import tensorflow as tf 2import tensorflow.keras as keras###変更 3from sklearn.model_selection import train_test_split 4import pandas as pd 5import numpy as np 6 7# データの読み込み --- (*1) 8analysisresults_data = pd.read_csv("analysis_resultstableZZZ.csv",encoding="utf-8") 9 10# データをラベルと入力データに分離する 11y = analysisresults_data.loc[:,"analysis_result"] 12x = analysisresults_data.loc[:,["id","signatures_id","hit_count"]] 13 14 15# 学習用とテスト用に分割する --- (*2) 16x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, train_size = 0.8, shuffle = True) 17 18# モデル構造を定義 --- (*3) 19Dense = keras.layers.Dense 20model = keras.models.Sequential() 21model.add(Dense(10, activation='relu', input_shape=(3,))) 22model.add(Dense(2, activation='softmax')) # ---(*3a) 23 24# モデルを構築 --- (*4) 25model.compile( 26 loss='categorical_crossentropy', 27 optimizer='adam', 28 metrics=['accuracy']) 29 30# 学習を実行 --- (*5) 31model.fit(x_train, y_train, 32 batch_size=20, 33 epochs=300) 34 35# モデルを評価 --- (*6) 36score = model.evaluate(x_test, y_test, verbose=1) 37print('正解率=', score[1], 'loss=', score[0])
2019-11-30 15:12:46.613774: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-11-30 15:12:46.625817: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fa07afcb160 executing computations on platform Host. Devices:
2019-11-30 15:12:46.625834: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version
WARNING:tensorflow:Falling back from v2 loop because of error: Failed to find data adapter that can handle input: <class 'pandas.core.frame.DataFrame'>, <class 'NoneType'>
Traceback (most recent call last):
File "deep2.py", line 33, in <module>
epochs=300)
File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 728, in fit
use_multiprocessing=use_multiprocessing)
File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 642, in fit
shuffle=shuffle)
File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 2538, in _standardize_user_data
y, self._feed_loss_fns, feed_output_shapes)
File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 717, in check_loss_and_target_compatibility
' while using as loss categorical_crossentropy
. '
ValueError: You are passing a target array of shape (7999, 1) while using as loss categorical_crossentropy
. categorical_crossentropy
expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:
from keras.utils import to_categorical y_binary = to_categorical(y_int)
Alternatively, you can use the loss function sparse_categorical_crossentropy
instead, which does expect integer targets.
回答1件
あなたの回答
tips
プレビュー