Tensorflow:from tensorflow.keras import layers 报错

Execution code:

from tensorflow.keras import layers

Error:

keras module does not exist

Check, there are other blogs that the keras package is in the pyhon package of the tensorflow module;

then:

from tensorflow.python.keras import layers

In other modules that call keras in this way, there is no major problem in the front, but there is a reason why the model reports an error in the end.

So I tried to do this (incorrect, although it works, it's not what you think)

First call the keras module from the tensorflow module, and then call other modules from the keras module, namely:

from tensorflow import keras
from keras import layers, optimizers

There is reason to suspect that the two (tensorflow.python.keras and the above method) should not be the same keras, or at least there are differences;

A blog pointed out that there is a keras folder in tensorflow:

 However, it still reports an error, and it can be solved by switching to tensorflow.python.keras;

And keras does not exist in my folder. Although it can be solved by switching to tensorflow.python.keras, there is a problem with saving the model in the end. It seems to be a get_config problem, but I have not customized the network layer.

So I try to do this:

from tensorflow import keras  # used to create the CNN model
from keras.layers import Dense, Conv2D, Flatten, Dropout  # used to create the CNN mode
print(keras.__version__)
# view the keras path
print(keras.__path__)

output: 

2.12.0
['D:\\Python310\\lib\\site-packages\\keras\\api\\_v2\\keras']

Hey guys, it turns out there is a separate keras module (but I remember I didn't install this, it's not clear):

 Later I learned that the keras module is independent from tensorflow, so the solution to the problem of how to call the keras model from tensorflow is not to call it from tensorflow, but to install the keras module with pip.

Guess you like

Origin blog.csdn.net/m0_63001937/article/details/131383051