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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

Q&A

解決済

2回答

2460閲覧

CNNのエラーを解決したい

wonwon

総合スコア13

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

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

0グッド

0クリップ

投稿2018/06/21 06:12

前提・実現したいこと

Pythonを使ってTensorflowでCNNを作ろうとしてましたが、エラーが発生してしまいました。どこのエラーでどういうエラーなのかも分かりません。
CNN自体初心者です。

発生している問題・エラーメッセージ

--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) ~/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1321 try: -> 1322 return fn(*args) 1323 except errors.OpError as e: ~/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata) 1306 return self._call_tf_sessionrun( -> 1307 options, feed_dict, fetch_list, target_list, run_metadata) 1308 ~/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata) 1408 self._session, options, feed_dict, fetch_list, target_list, -> 1409 run_metadata) 1410 else: InvalidArgumentError: Input to reshape is a tensor with 2556800 values, but the requested shape requires a multiple of 29920 [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](MaxPool, Reshape_1/shape)]] During handling of the above exception, another exception occurred: InvalidArgumentError Traceback (most recent call last) <ipython-input-6-d874a992b217> in <module>() 2 for _ in range(4000): 3 i += 1 ----> 4 sess.run(train_step, feed_dict={x:mfccs, t:labels}) 5 if i % 1 == 0: 6 loss_val, acc_val = sess.run([loss, accuracy], ~/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 898 try: 899 result = self._run(None, fetches, feed_dict, options_ptr, --> 900 run_metadata_ptr) 901 if run_metadata: 902 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) ~/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 1133 if final_fetches or final_targets or (handle and feed_dict_tensor): 1134 results = self._do_run(handle, final_targets, final_fetches, -> 1135 feed_dict_tensor, options, run_metadata) 1136 else: 1137 results = [] ~/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1314 if handle is None: 1315 return self._do_call(_run_fn, feeds, fetches, targets, options, -> 1316 run_metadata) 1317 else: 1318 return self._do_call(_prun_fn, handle, feeds, fetches) ~/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args) 1333 except KeyError: 1334 pass -> 1335 raise type(e)(node_def, op, message) 1336 1337 def _extend_graph(self): InvalidArgumentError: Input to reshape is a tensor with 2556800 values, but the requested shape requires a multiple of 29920 [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](MaxPool, Reshape_1/shape)]] Caused by op 'Reshape_1', defined at: File "/home/kMIYASAKA/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) ---------------この間はteratailの文字制限で省略しています------------------ File "/home/kMIYASAKA/.pyenv/versions/anaconda3-5.2.0/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1718, in __init__ self._traceback = self._graph._extract_stack() # pylint: disable=protected-access InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 2556800 values, but the requested shape requires a multiple of 29920 [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](MaxPool, Reshape_1/shape)]]

該当のソースコード

python

1#以下CNN 2num_filters = 16 3 4x = tf.placeholder(tf.float32, [None, 3740]) 5 6x_image = tf.reshape(x, [-1,20,187,1]) 7 8W_conv = tf.Variable(tf.truncated_normal([5,5,1,num_filters], 9 stddev=0.1)) 10h_conv = tf.nn.conv2d(x_image, W_conv, 11 strides=[1,1,1,1], padding='SAME') 12h_pool =tf.nn.max_pool(h_conv, ksize=[1,2,2,1], 13 strides=[1,2,2,1], padding='SAME') 14 15h_pool_flat = tf.reshape(h_pool, [-1, 1870*num_filters]) 16 17 18num_units1 = 1870*num_filters 19num_units2 = 1024 20 21w2 = tf.Variable(tf.truncated_normal([num_units1, num_units2])) 22b2 = tf.Variable(tf.zeros([num_units2])) 23hidden2 = tf.nn.relu(tf.matmul(h_pool_flat, w2) + b2) 24 25w0 = tf.Variable(tf.zeros([num_units2, 3])) 26b0 = tf.Variable(tf.zeros([3])) 27p = tf.nn.softmax(tf.matmul(hidden2, w0) + b0) 28 29 30t = tf.placeholder(tf.float32, [None, 3]) 31loss = -tf.reduce_sum(t * tf.log(p)) 32train_step = tf.train.GradientDescentOptimizer(0.00005).minimize(loss) 33correct_prediction = tf.equal(tf.argmax(p, 1), tf.argmax(t, 1)) 34accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 35 36 37sess = tf.Session() 38sess.run(tf.initialize_all_variables()) 39 40 41i = 0 42for _ in range(4000): 43 i += 1 44 sess.run(train_step, feed_dict={x:mfccs, t:labels}) 45 if i % 1 == 0: 46 loss_val, acc_val = sess.run([loss, accuracy], 47 feed_dict={x:mfccs, t:labels}) 48 print ('Step: %d, Loss: %f, Accuracy: %f' 49 % (i, loss_val, acc_val)) 50 51#--モデルの評価-- 52loss_val, acc_val = sess.run( 53 [loss, accuracy], feed_dict = {x:mfccs, t:labels}) 54print('テスト結果: Loss: %f, Accuracy: %f' %(loss_val, acc_val))

補足情報

入力は波形のファイルをmfccという処理をして2次元配列で取り出し、サイズを揃えてそれを一次元配列にして入力しています。それぞれのデータの、配列の要素数は3740です。2次元配列の時の要素数は20*187でした。

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

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

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

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

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

guest

回答2

0

ベストアンサー

strideすると画素数が変化したりするので、以後の配列の要素数でバグを出しやすいです。

初心者で楽をしたいのなら、Kerasを使えば細かいつじつま合わせをライブラリが代行してくれます。

どうしてもtensorflowを直で使えるようになりたいのなら、個人的にはtf.shapeを使ってつじつま合わせを自動的に処理します。

というのも、ネットワーク内のパラメータをチューニングすることは珍しくなく、このままだと今後パラメータをいじるたびに各層のパラメータの整合性を人力で確かめる必要があるからです。


ちなみに理由ですが、

Poolingすると半分になります。
縦も横も。padding="SAME"で
20x187 -> 10x94
になるはずです。

コードを見ると1870なので、ずれています。
正しくは940です。

普通にやると(20//2)(187//2)=930になるので、注意が必要です。
20
187//4=935なので、これも正しくありません。


EagerExecutionするとデバッグが楽です。
Tensorflowのバージョンにお気をつけください。

python

1import numpy as np 2import tensorflow as tf 3import tensorflow.contrib.eager as tfe 4 5tf.enable_eager_execution() 6 7mmm = 10 8labels = tf.constant(np.random.random(size=(mmm, 3)), dtype=tf.float32) 9mfccs = tf.constant(np.random.random(size=(mmm, 3740)), dtype=tf.float32) 10 11num_filters = 16 12 13#x = tf.placeholder(tf.float32, [None, 3740]) 14x = mfccs 15 16x_image = tf.reshape(x, [-1,20,187,1]) 17 18W_conv = tfe.Variable(tf.truncated_normal([5,5,1,num_filters], stddev=0.1)) 19h_conv = tf.nn.conv2d(x_image, W_conv, strides=[1,1,1,1], padding='SAME') 20h_pool =tf.nn.max_pool(h_conv, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') 21print(tf.shape(h_pool))

投稿2018/06/21 09:57

編集2018/06/21 11:13
mkgrei

総合スコア8560

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

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

0

エラーメッセージ
『InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 2556800 values, but the requested shape requires a multiple of 29920
[[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](MaxPool, Reshape_1/shape)]]』
(直訳)
reshapeへの入力は、255万6800個の値から成るテンソルですが、求められる形(Shape)は29920の倍数です。

「テンソルのデータ個数2556800が、29920の倍数になっていない」というのが、エラーの直接の原因のようですが、これらの数値、テンソルの形状に心当たりはありませんか?

29920が補足情報に書かれた配列の要素数 3740 に8を乗じた数であることは判りますから、テンソルを作るところに問題がありそうな気はします。

投稿2018/06/21 07:11

coco_bauer

総合スコア6915

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問