2016-08-08 33 views
5

このコードスニペットtrain_datasetでは、test_datasetvalid_datasetnumpy.ndarrayです。numpyでこのmemoryviewエラーを回避するにはどうすればよいですか?

def check_overlaps(images1, images2): 
    images1.flags.writeable=False 
    images2.flags.writeable=False 
    print(type(images1)) 
    print(type(images2)) 
    start = time.clock() 
    hash1 = set([hash(image1.data) for image1 in images1]) 
    hash2 = set([hash(image2.data) for image2 in images2]) 
    all_overlaps = set.intersection(hash1, hash2) 
    return all_overlaps, time.clock()-start 

r, execTime = check_overlaps(train_dataset, test_dataset)  
print("# overlaps between training and test sets:", len(r), "execution time:", execTime) 
r, execTime = check_overlaps(train_dataset, valid_dataset) 
print("# overlaps between training and validation sets:", len(r), "execution time:", execTime) 
r, execTime = check_overlaps(valid_dataset, test_dataset) 
print("# overlaps between validation and test sets:", len(r), "execution time:", execTime) 

しかし、これは次のエラー得られます(!それは読みやすくするためのコードとして書式設定) を

ValueError        Traceback (most recent call last) 
<ipython-input-14-337e73a1cb14> in <module>() 
    12  return all_overlaps, time.clock()-start 
    13 
---> 14 r, execTime = check_overlaps(train_dataset, test_dataset) 
    15 print("# overlaps between training and test sets:", len(r), "execution time:", execTime) 
    16 r, execTime = check_overlaps(train_dataset, valid_dataset) 

<ipython-input-14-337e73a1cb14> in check_overlaps(images1, images2) 
     7  print(type(images2)) 
     8  start = time.clock() 
----> 9  hash1 = set([hash(image1.data) for image1 in images1]) 
    10  hash2 = set([hash(image2.data) for image2 in images2]) 
    11  all_overlaps = set.intersection(hash1, hash2) 

<ipython-input-14-337e73a1cb14> in <listcomp>(.0) 
     7  print(type(images2)) 
     8  start = time.clock() 
----> 9  hash1 = set([hash(image1.data) for image1 in images1]) 
    10  hash2 = set([hash(image2.data) for image2 in images2]) 
    11  all_overlaps = set.intersection(hash1, hash2) 

ValueError: memoryview: hashing is restricted to formats 'B', 'b' or 'c' 

今の問題は、私もエラーはおろか考える何を意味するのか分からないですそれを訂正する。助けてください?

答えて

10

問題は、ハッシュアレイへのメソッドがpython2でのみ機能することです。したがって、hash(image1.data)を計算しようとするとすぐにコードが失敗します。このエラーメッセージは、符号なしバイト('B')、バイト('b')の単一バイト('c')のmemoryviewのフォーマットのみがサポートされていることを示しており、このようなビューをコピーすることなくnp.ndarrayから取得する方法が見つかりませんでした。私が思いついた唯一の方法は、あなたのデータ量に応じてあなたのアプリケーションでは実現不可能かもしれないアレイをコピーすることです。つまり、あなたの関数を変更しようとすることができます:

def check_overlaps(images1, images2): 
    start = time.clock() 
    hash1 = set([hash(image1.tobytes()) for image1 in images1]) 
    hash2 = set([hash(image2.tobytes()) for image2 in images2]) 
    all_overlaps = set.intersection(hash1, hash2) 
    return all_overlaps, time.clock()-start 
+2

はい私はpython3 +に取り組んでいます。私は関数 'bytes()'を 'hash(bytes(image1))')として使いました。ご協力いただきありがとうございます。 そしてそれは約200000のMNIST画像の大きなデータセットからです。 – user6692576

+0

@ user6692576うれしいことに助けてくれました。 'bytes()'は実際には 'np.tobytes()'とまったく同じ結果をもたらし、データのコピーも作成します。私はそれが内部的に関数を呼び出すと思う。あなたの目的のために、おそらくそれらを交換可能に使用することができます。 – jotasi

関連する問題