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

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

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

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

機械学習

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

Python

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

Q&A

0回答

313閲覧

python:kerasで画像のCNN学習器作成でエラーが出ます。。

python_2019

総合スコア68

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

機械学習

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

Python

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

0グッド

0クリップ

投稿2019/12/05 15:42

以下のようなCNN学習器作成でエラーが出ます。原因が良く分かりません。
どなかた詳しい方、ご指導をお願いいたします。

optimizers ="Adadelta" results = {} epochs = 200 model.compile(loss='categorical_crossentropy', optimizer=optimizers, metrics=['accuracy']) #x_train, y_train, batch_size=batch_size, epochs=epochs,erbose=1, validation_data=(x_test, y_test) results= model.fit(X_train, y_train, validation_split=0.2, epochs=epochs ) model_json_str = model.to_json() open('dokugyo_mlp_weights.json', 'w').write(model_json_str) model.save_weights('dokugyo_mlp_weights.h5');

以下、エラーメッセージ

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-26-71d4f9fdc3b9> in <module> 4 model.compile(loss='categorical_crossentropy', optimizer=optimizers, metrics=['accuracy']) 5 #x_train, y_train, batch_size=batch_size, epochs=epochs,erbose=1, validation_data=(x_test, y_test) ----> 6 results= model.fit(X_train, y_train, validation_split=0.2, epochs=epochs ) 7 8 model_json_str = model.to_json() ~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs) 950 sample_weight=sample_weight, 951 class_weight=class_weight, --> 952 batch_size=batch_size) 953 # Prepare validation data. 954 do_validation = False ~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size) 807 # using improper loss fns. 808 check_loss_and_target_compatibility( --> 809 y, self._feed_loss_fns, feed_output_shapes) 810 else: 811 y = [] ~\Anaconda3\lib\site-packages\keras\engine\training_utils.py in check_loss_and_target_compatibility(targets, loss_fns, output_shapes) 271 raise ValueError( 272 'You are passing a target array of shape ' + str(y.shape) + --> 273 ' while using as loss `categorical_crossentropy`. ' 274 '`categorical_crossentropy` expects ' 275 'targets to be binary matrices (1s and 0s) ' ValueError: You are passing a target array of shape (50, 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ページで確認できます。

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

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

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

tkymtmt

2019/12/05 23:51

エラーメッセージにあるように、model.compileにおいてloss='categorical_crossentropy'ではなくloss='sparse_categorical_crossentropy'におきかえて試してみましたか?
python_2019

2019/12/06 13:28

ご連絡ありはとうございます。 ご指摘通り実行すると、以下のようなエラーが出ました。 なぜでしょうか? --------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) <ipython-input-4-c14da5f590a7> in <module> 10 model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizers, metrics=['accuracy']) 11 #x_train, y_train, batch_size=batch_size, epochs=epochs,erbose=1, validation_data=(x_test, y_test) ---> 12 results= model.fit(X_train, y_train, validation_split=0.2, epochs=epochs ) 13 14 model_json_str = model.to_json() ~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs) 1037 initial_epoch=initial_epoch, 1038 steps_per_epoch=steps_per_epoch, -> 1039 validation_steps=validation_steps) 1040 1041 def evaluate(self, x=None, y=None, ~\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps) 197 ins_batch[i] = ins_batch[i].toarray() 198 --> 199 outs = f(ins_batch) 200 outs = to_list(outs) 201 for l, o in zip(out_labels, outs): ~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in __call__(self, inputs) 2713 return self._legacy_call(inputs) 2714 -> 2715 return self._call(inputs) 2716 else: 2717 if py_any(is_tensor(x) for x in inputs): ~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in _call(self, inputs) 2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata) 2674 else: -> 2675 fetched = self._callable_fn(*array_vals) 2676 return fetched[:len(self.outputs)] 2677 ~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in __call__(self, *args, **kwargs) 1456 ret = tf_session.TF_SessionRunCallable(self._session._session, 1457 self._handle, args, -> 1458 run_metadata_ptr) 1459 if run_metadata: 1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) InvalidArgumentError: Received a label value of 1 which is outside the valid range of [0, 1). Label values: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [[{{node loss/activation_6_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問