Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Lambda层中使用VGG preprocess_输入以及Dense和KERAS.backend.clear_session()时出现KERAS错误_Python_Tensorflow_Keras_Lambda_Vgg Net - Fatal编程技术网

Python 在Lambda层中使用VGG preprocess_输入以及Dense和KERAS.backend.clear_session()时出现KERAS错误

Python 在Lambda层中使用VGG preprocess_输入以及Dense和KERAS.backend.clear_session()时出现KERAS错误,python,tensorflow,keras,lambda,vgg-net,Python,Tensorflow,Keras,Lambda,Vgg Net,我需要在一个循环中创建几个模型(因此我需要使用keras.backend.clear_session()为每个迭代清理环境),但是,如果模型包含Lambda和vgg16.preprocess_input和稠密层,在我第二次创建模型之后,我得到 ValueError:Tensor(“预处理/Const:0”,shape=(3,),dtype=float32)必须来自与Tensor(“预处理/1/跨步切片:0”,shape=(?,3),dtype=float32)相同的图形。 复制错误的代码: #

我需要在一个循环中创建几个模型(因此我
需要使用keras.backend.clear_session()
为每个迭代清理环境),但是,如果模型包含
Lambda
vgg16.preprocess_input
和稠密层,在我第二次创建模型之后,我得到
ValueError:Tensor(“预处理/Const:0”,shape=(3,),dtype=float32)必须来自与Tensor(“预处理/1/跨步切片:0”,shape=(?,3),dtype=float32)相同的图形。

复制错误的代码:

# making the model
from keras.layers import Dense, Reshape, Lambda
from keras import Sequential
f = keras.applications.vgg16.preprocess_input
d_l = Dense(3, activation='linear', input_shape=(3,), name='MYDENSE')

p_l = Lambda(f,name='PREPROCESS')

model_mod = Sequential()
model_mod.add(d_l)
model_mod.add(p_l)
model_mod.summary()
model_mod.build()
# clean the environment
keras.backend.clear_session()
# making again the same model
f = keras.applications.vgg16.preprocess_input
d_l = Dense(3, activation='linear', input_shape=(3,), name='MYDENSE')
p_l = Lambda(f,name='PREPROCESS')
model_mod = Sequential()
model_mod.add(d_l)
model_mod.add(p_l)

keras版本:“2.2.4”

以下代码适用于Tensorflow

# making the model
import tensorflow as tf
from keras.layers import Dense, Reshape, Lambda
from keras import Sequential
f = tf.keras.applications.vgg16.preprocess_input
d_l = Dense(3, activation='linear', input_shape=(3,), name='MYDENSE')

p_l = Lambda(f,name='PREPROCESS')

model_mod = Sequential()
model_mod.add(d_l)
model_mod.add(p_l)
model_mod.summary()
model_mod.build()
# clean the environment
tf.keras.backend.clear_session()
# making again the same model
f = tf.keras.applications.vgg16.preprocess_input
d_l = Dense(3, activation='linear', input_shape=(3,), name='MYDENSE')
p_l = Lambda(f,name='PREPROCESS')
model_mod = Sequential()
model_mod.add(d_l)
model_mod.add(p_l)
输出

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
MYDENSE (Dense)              (None, 3)                 12        
_________________________________________________________________
PREPROCESS (Lambda)          (None, 3)                 0         
=================================================================
Total params: 12
Trainable params: 12
Non-trainable params: 0