imdb.load_data () 함수에 대해 'allow_pickle = False'일 때 개체 배열을로드 할 수 없음을 수정하는 방법은 무엇입니까?
Google Colab 에서 IMDb 데이터 세트를 사용하여 이진 분류 예제를 구현하려고합니다 . 이전 에이 모델을 구현했습니다. 하지만 며칠 후 다시 시도했을 때 load_data () 함수에 오류에 대해 'allow_pickle = False 일 때 개체 배열을로드 할 수 있습니다'라는 값이 반환되었습니다.
제기 문제에 대한 기존의 답변을 참조하여 이미 해결을 시도 합니다. sketch_rnn 알고리즘에서 'object arrays cannot be loaded when allow_pickle = False'in the sketch_rnn algorithm 하지만 allow_pickle 인수를 추가하는 것만으로는 충분하지 않습니다.
내 코드 :
from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
오류 :
ValueError Traceback (most recent call last)
<ipython-input-1-2ab3902db485> in <module>()
1 from keras.datasets import imdb
----> 2 (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
2 frames
/usr/local/lib/python3.6/dist-packages/keras/datasets/imdb.py in load_data(path, num_words, skip_top, maxlen, seed, start_char, oov_char, index_from, **kwargs)
57 file_hash='599dadb1135973df5b59232a0e9a887c')
58 with np.load(path) as f:
---> 59 x_train, labels_train = f['x_train'], f['y_train']
60 x_test, labels_test = f['x_test'], f['y_test']
61
/usr/local/lib/python3.6/dist-packages/numpy/lib/npyio.py in __getitem__(self, key)
260 return format.read_array(bytes,
261 allow_pickle=self.allow_pickle,
--> 262 pickle_kwargs=self.pickle_kwargs)
263 else:
264 return self.zip.read(key)
/usr/local/lib/python3.6/dist-packages/numpy/lib/format.py in read_array(fp, allow_pickle, pickle_kwargs)
690 # The array contained Python objects. We need to unpickle the data.
691 if not allow_pickle:
--> 692 raise ValueError("Object arrays cannot be loaded when "
693 "allow_pickle=False")
694 if pickle_kwargs is None:
ValueError: Object arrays cannot be loaded when allow_pickle=False
다음 imdb.load_data
은 노트북 에서이 줄을 대체하여 피클 을 강제 로 허용 하는 방법입니다 .
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
이로 인해 :
import numpy as np
# save np.load
np_load_old = np.load
# modify the default parameters of np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
# call load_data with allow_pickle implicitly set to true
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
# restore np.load for future normal usage
np.load = np_load_old
이 문제는 여전히 keras git에 있습니다. 가능한 한 빨리 해결되기를 바랍니다. 그때까지 numpy 버전을 1.16.2로 다운 그레이드 먼저. 문제를 해결하는 것입니다.
!pip install numpy==1.16.1
import numpy as np
이 NumPy와의 기본값 버전 allow_pickle
은 True
.
GitHub 에서이 문제 에 이어 공식적인 해결책은 imdb.py 파일을 편집하는 것입니다. 이 수정은 numpy를 다운 그레이드하지 않고 잘 작동했습니다. imdb.py 파일을 찾아서 tensorflow/python/keras/datasets/imdb.py
(전체 경로 : C:\Anaconda\Lib\site-packages\tensorflow\python\keras\datasets\imdb.py
-다른 설치는 다를 수 있음) diff에 따라 85 행을 변경합니다.
- with np.load(path) as f:
+ with np.load(path, allow_pickle=True) as f:
변경 이유는 피클 파일에 SQL 주입에 해당하는 Python을 방지하기위한 보안입니다. 위의 변경 사항은 imdb 데이터에만 영향을 미치므로 다른 곳에서 보안을 유지합니다 (numpy를 다운 그레이드하지 않음).
방금 allow_pickle = True를 np.load ()에 대한 인수로 사용했고 저에게 효과적이었습니다.
cheez ( https://stackoverflow.com/users/122933/cheez ) 의 답변 이 가장 쉽고 효과적인 답변이라고 생각합니다 . 전체 세션 기간 동안 numpy 함수를 수정하지 않도록 조금 더 자세히 설명하겠습니다.
내 제안은 다음과 같습니다. 동일한 종류의 오류를 보여주는 keras에서 reuters 데이터 세트를 다운로드하는 데 사용하고 있습니다.
old = np.load
np.load = lambda *a,**k: old(*a,**k,allow_pickle=True)
from keras.datasets import reuters
(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)
np.load = old
del(old)
플래그의 값을 변경해 볼 수 있습니다.
np.load(training_image_names_array,allow_pickle=True)
예, 이전 버전의 numpy를 설치하면 문제가 해결되었습니다.
PyCharm IDE를 사용하는 경우 :
내 IDE (Pycharm), File-> Settings-> Project Interpreter : numpy가 1.16.3 인 것을 알았으므로 1.16.1로 되돌립니다. +를 클릭하고 검색에 numpy를 입력하고 "specify version": 1.16.1을 선택한 다음 패키지 설치를 선택합니다.
Tensorflow는 tf-nightly 버전에서 수정되었습니다.
!pip install tf-nightly
현재 버전은 '2.0.0-dev20190511'입니다.
내가 찾은 사실은 TensorFlow 2.0 (2.0.0-alpha0을 사용하고 있음)이 최신 버전의 Numpy 즉 v1.17.0 (및 아마도 v1.16.5 +)과 호환되지 않는다는 것입니다. TF2를 가져 오자마자 다음과 같은 거대한 FutureWarning 목록이 표시됩니다.
FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/anaconda3/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/anaconda3/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/anaconda3/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
이로 인해 keras에서 imdb 데이터 세트를로드하려고 할 때 allow_pickle 오류가 발생했습니다.
잘 작동하는 다음 솔루션을 사용하려고 시도했지만 TF2 또는 tf.keras를 가져 오는 모든 단일 프로젝트를 수행해야했습니다.
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
내가 찾은 가장 쉬운 해결책은 numpy 1.16.1을 전역 적으로 설치하거나 가상 환경에서 호환되는 버전의 tensorflow 및 numpy를 사용하는 것입니다.
이 답변에 대한 나의 목표는 imdb.load_data의 문제뿐만 아니라 TF2 및 Numpy 버전의 비 호환성으로 인해 더 큰 문제가 발생하고 다른 많은 숨겨진 버그 또는 문제가 발생할 수 있음을 지적하는 것입니다.
imdb.py의 경로를 찾은 다음 np.load (path, ... flag ...)에 플래그를 추가하십시오.
def load_data(.......):
.......................................
.......................................
- with np.load(path) as f:
+ with np.load(path,allow_pickle=True) as f:
jupyter 노트북에서
np_load_old = np.load
# modify the default parameters of np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
잘 작동했지만 스파이더에서이 방법을 사용할 때 문제가 나타납니다 (매번 커널을 다시 시작해야합니다. 그렇지 않으면 다음과 같은 오류가 발생합니다.
TypeError : () 키워드 인수 'allow_pickle'에 여러 값이 있습니다.
여기 솔루션을 사용하여이 문제를 해결했습니다 .
나를위한 일
np_load_old = np.load
np.load = lambda *a: np_load_old(*a, allow_pickle=True)
(x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=None, test_split=0.2)
np.load = np_load_old
'ProgramingTip' 카테고리의 다른 글
오류 : 각도 입력의 [ng : areq] (0) | 2020.11.19 |
---|---|
any () 함수의 반대 (0) | 2020.11.19 |
한 개체의 속성 값을 동일한 유형의 다른 개체에 자동으로 적용 하시겠습니까? (0) | 2020.11.19 |
이 C ++를 복사 할 수 있습니까? (0) | 2020.11.19 |
IllegalArgumentException : 도장 1의 바인드 값이 null입니다. (0) | 2020.11.19 |