2017-11-12 13 views
0

私は、YouTubeビデオのTensorflowとMNISTデータセットのTensorflow CNNチュートリアルを使用して、畳み込みニューラルネットワークのチュートリアルに従ってきました。これらのチュートリアルを使用して、オーディオデータで独自のCNNを作成しました。目標は、CNNを使用して33人のスピーカーから音声を特定することです。データはすでに絡み合っているので、畳み込みを適用できるようにテストセットの形状は(8404,1,500,1)です。オーディオの各セグメントは500個あり、テストセットには8404個のサンプルがあります。私の問題はトレーニング段階です。私は次のエラーを取得する:テンソルフローを使用したトレーニングで不適切な形状になる

ValueError: Cannot feed value of shape (128, 1, 500, 1) for Tensor 'Placeholder:0', which has shape '(?, 500)'

私はこのとValueErrorをGoogleで検索し、人々が期待される寸法にbatch_xを再形成することによって、これを解決してきました。だから、私は次のコード行を試しました:
batch_x = np.reshape(batch_x, [-1, 500])

私はこの形には運がありませんでした。誰もこの問題を抱えていませんか?以下はコードです。

import numpy as np 
import tensorflow as tf 
npzfile = np.load('saved_data_file_33.npz') 

train_segs = npzfile['train_segs']    # Seg train data 
train_labels = npzfile['train_labels']   # train labels 
train_labels_1h = npzfile['train_labels_1h'] # One hot encoding for training data 
epochs = 1 
batch_size = 128 
learning_rate = 0.01 
classes = len(train_labels_1h[0,:]) # 33 classes 
seg_size = len(test_segs[0,0,:,0]) # 500 long 

x = tf.placeholder(tf.float32, [None, seg_size]) 
y = tf.placeholder(tf.float32) 

# This section is initializing the weights and biases of each hidden layer and output layer with random values. 
# These values are stores in a dict for easy access. 
weights = {"conv1" : tf.Variable(tf.random_normal([5, 5, 1, 32])), 
      "conv2": tf.Variable(tf.random_normal([5, 5, 32, 64])), 
      "fc_layer": tf.Variable(tf.random_normal([1*125*64, 1024])), 
      "output": tf.Variable(tf.random_normal([1024, classes])) 
      } 
biases = { "b_c1" : tf.Variable(tf.random_normal([32])), 
      "b_c2" : tf.Variable(tf.random_normal([64])), 
      "b_fc" : tf.Variable(tf.random_normal([1024])), 
      "output": tf.Variable(tf.random_normal([classes])) 
      } 

reshapedX = tf.reshape(x, [-1, 1, 500, 1]) 

conv1 = tf.nn.conv2d(reshapedX, weights["conv1"], strides = [1, 1, 1, 1], padding = "SAME") 
conv1 = tf.nn.relu(conv1 + biases["b_c1"]) 
conv1 = tf.nn.max_pool(conv1, ksize = [1, 1, 2, 1], strides = [1, 1, 2, 1], padding = "SAME") 

conv2 = tf.nn.conv2d(conv1, weights["conv2"], strides = [1, 1, 1, 1], padding = "SAME") 
conv2 = tf.nn.relu(conv2 + biases["b_c2"]) 
conv2 = tf.nn.max_pool(conv2, ksize = [1, 1, 2, 1], strides = [1, 1, 2, 1], padding = "SAME") 

fc = tf.reshape(conv2, [-1, 1*125*64]) 
fc = tf.nn.relu(tf.matmul(fc, weights["fc_layer"]) + biases["b_fc"]) 

output_layer = tf.matmul(fc, weights["output"]) + biases["output"] 

cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=output_layer)) 
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 
correct_prediction = tf.equal(tf.argmax(output_layer, 1), tf.argmax(y, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    for i in range(epochs): 
     j = 0 
     while j < len(train_segs): 
      start = i 
      end = i + batch_size 
      batch_x = np.array(train_segs[start:end]) 
      batch_y = np.array(train_labels[start:end]) 
      #batch_x = np.reshape(batch_x, [-1, 500]) # reshape for x input. s 
      train_accuracy = accuracy.eval(feed_dict={x: batch_x, y: batch_y}) 
      print('step %d, training accuracy %g' % (i, train_accuracy)) 
      train_step.run(feed_dict={x: batch_x, y: batch_y}) 

    print('test accuracy %g' % accuracy.eval(feed_dict={ 
     x: train_segs, y: train_labels})) 

答えて

1

サイズ1のサイズをtrain_segsから削除したいようです。これにはtrain_segs = np.squeeze(train_segs)を使用できます。

また、np.reshapeに間違った括弧を使用していると思いますので、np.reshape(batch_x, (-1, 500))が機能している可能性があります。一般的には、要素の順序が期待どおりに終わらない可能性があるので、reshape関数に注意する必要があります。

+0

numpyの形式を変更していただきありがとうございます。スクイズを提案してくれてありがとう。それは確かに私が行方不明だったものでした。しかし、私のコードの最大の間違いは、私は1つのホットエンコードされたラベルに合格していませんでした!ご協力いただきありがとうございます。とても感謝しています。 –

関連する問題