前提・実現したいこと
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でした。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。