質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

2053閲覧

機械学習のディープラーニングでエラー

Rondon7251

総合スコア89

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

機械学習

機械学習は、データからパターンを自動的に発見し、そこから知能的な判断を下すためのコンピューターアルゴリズムを指します。人工知能における課題のひとつです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/11/30 06:14

機械学習でエラーが起きます。
エラー内容やどこを直せばいいかわかる方いましたら教えてください

ソース

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.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fiveHundred

2019/11/30 11:13

エラーの内容にそのまま解決方法が記載されていますが、読んで試しましたか?
Rondon7251

2019/12/02 03:58

どの部分のエラー内容ですか?
guest

回答1

0

ベストアンサー

どの部分のエラー内容ですか?

以下の通りです。

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.

要するに、to_categorical()で変換するか、sparse_categorical_crossentropyを使ってくれ、ということです。

投稿2019/12/02 04:28

fiveHundred

総合スコア9778

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Rondon7251

2019/12/02 10:16

loss='categorical_crossentropy',の部分を loss=='sparse_categorical_crossentropy' に変更するという意味であっていますでしょうか
fiveHundred

2019/12/02 10:27

それで試してみてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問