我已经包装了一些可以用try / except块的内存不足的代码.但是,虽然生成了MemoryError,但它并没有被捕获.
我有以下代码:
while True: try: self.create_indexed_vocab( vocab ) self.reset_weights() break; except MemoryError: # Stuff to reduce size of vocabulary self.vocab,self.index2word = None,None self.syn0,self.syn1 = None,None self.min_count += 1 logger.info( ...format string here... )
我得到以下追溯:
File "./make_model_tagged_wmt11.py",line 39,in <module> model.build_vocab(sentences) File "/root/CustomCompiledSoftware/gensim/gensim/models/word2vec.py",line 236,in build_vocab self.reset_weights() File "/root/CustomCompiledSoftware/gensim/gensim/models/word2vec.py",line 347,in reset_weights self.syn0 += (random.rand(len(self.vocab),self.layer1_size) - 0.5) / self.layer1_size File "mtrand.pyx",line 1044,in mtrand.RandomState.rand (numpy/random/mtrand/mtrand.c:6523) File "mtrand.pyx",line 760,in mtrand.RandomState.random_sample (numpy/random/mtrand/mtrand.c:5713) File "mtrand.pyx",line 137,in mtrand.cont0_array (numpy/random/mtrand/mtrand.c:1300) MemoryError
我在Ubuntu 12.04下运行Python 2.7.3
reset_weights行self.syn0正是我期望引发异常的行(它分配一个大数组).令人困惑的是,我无法捕获内存错误,并且会使数组的大小变小.
是否有特殊情况导致MemoryError无法被捕获?