2017-04-01 8 views
0
Iは、次の変数に mnist画像値のアレイを割り当てる必要

...numpyアレイを特定の形状に連結するにはどうすればいいですか?

x = tf.get_variable("input_image", shape=[10,784], dtype=tf.float32) 

問題は、Iセットmnistデータを取捨選択し、番号2の10枚の画像を抽出し、それを割り当てる必要がありますx

これは、問題は、私は私のロジックは欠陥があると信じている

while mnist.test.next_batch(FLAGS.batch_size): 
    sample_image, sample_label = mnist.test.next_batch(10) 
    # get number 2 
    itemindex = np.where(sample_label == 1) 

    if itemindex[1][0] == 1: 
     # append image to numpy 
     np.append(labels_of_2, sample_image) 
    # if the numpy array has 10 images then we stop 
    if labels_of_2.size == 10: 
     break 

# assign to variable 
sess.run(tf.assign(x, labels_of_2)) 
...データセットをふるい及び数2の抽出での私のアプローチです。私は私が望むものを達成するための簡単な方法が存在しなければならない

np.append(labels_of_2, sample_image) 

...変数xを満たし、明確に次の行がそれを行うための方法ではありませんする形状[10, 784]の配列を必要とするが、私はそれを把握することはできませんでる。

答えて

1

忘れましたnp.append;全てalistで配列を仮定リスト

alist = [] 
while mnist.test.next_batch(FLAGS.batch_size): 
    sample_image, sample_label = mnist.test.next_batch(10) 
    # get number 2 
    itemindex = np.where(sample_label == 1) 

    if itemindex[1][0] == 1: 
     alist.append(sample_image) 
    # if the list has 10 images then we stop 
    if len(alist) == 10: 
     break 

    labels_of_2 = np.array(alist) 

で画像を収集同じサイズを有し、例えば(784、)の場合、array関数はshape(10、784)の新しい配列を生成します。画像が(1,784)の場合は、代わりにnp.concatenate(alist, axis=0)を使用できます。

List appendは、より高速で使いやすくなっています。

関連する問題