Numpy load with `allow_pickle=true` old `keras` in `tensorflow.keras`

落爺英雄遲暮 提交于 2021-02-05 10:47:41

问题


Numpy load with allow_pickle=true old keras in tensorflow.keras

Situation

I have taken a course, where a Jupyter-notebook with a few example use-cases was provided. In order to save time and to have a clean working-environment, I've decided to use the Docker-image: jupyter/tensorflow-notebook:latest (see here).

These are the pip list results for the relevant modules:

[...]
Keras-Applications     1.0.8
Keras-Preprocessing    1.1.0
[...]
numpy                  1.17.5
[...]
tensorflow             2.1.0
[...]

(a) The following modules are loaded in the header:

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.optimizers import RMSprop, Adam

import numpy as np
import matplotlib.pyplot as plt 
%matplotlib inline

(b) For some digit recognition Tensorflow example we were given a file, which apparently contains the learning process. This is loaded as follows:

history = np.load('history_NN_40_epochs.npy',allow_pickle='TRUE').item()

Problem

The problem is, that the course supervisor did not use the same keras interface as is provided in the docker-image. Though I'd like to use the tensorflow.keras module instead of having to download other/older versions of keras.

Research done / solutions tested

Now I want to list solutions and attempts I have tried to allow for more experienced users to find possible early pitfalls.

(a)

The first problem ModuleNotFoundError: No module named 'keras' appears there, which I solved, by simply changing the import to

import tensorflow.keras as keras
from tensorflow.keras.XY import YZ # prefixed with tensorflow.*
...

(this is adviced in this post as well)

(b)

This throws ModuleNotFoundError: No module named 'keras'. I assume, that this also comes from the different way of using keras. So I tried the solution presented here, where a custom class RenamingUnpickler(pickle.Unpickler) is created.

Using it like this:

[...]
if module == "keras":
    renamed_module = "tensorflow.keras"
[...]

did not work though (same error). This slightly different solution yields the same error as well.


notes
  • Another similar problem and solution, I do not fully understand, can be found here.
  • To my understanding the accepted solution there is not applicable.

来源:https://stackoverflow.com/questions/60193908/numpy-load-with-allow-pickle-true-old-keras-in-tensorflow-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!