Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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 获取Keras模型的一部分_Python_Tensorflow_Keras_Autoencoder - Fatal编程技术网

Python 获取Keras模型的一部分

Python 获取Keras模型的一部分,python,tensorflow,keras,autoencoder,Python,Tensorflow,Keras,Autoencoder,我有一个自动编码器的模型,如下所示: height, width = 28, 28 input_img = Input(shape=(height * width,)) encoding_dim = height * width//256 x = Dense(height * width, activation='relu')(input_img) encoded1 = Dense(height * width//2, activation='relu')(x) encoded2 = De

我有一个
自动编码器的模型,如下所示:

height, width = 28, 28

input_img = Input(shape=(height * width,))
encoding_dim = height * width//256

x = Dense(height * width, activation='relu')(input_img)

encoded1 = Dense(height * width//2, activation='relu')(x)
encoded2 = Dense(height * width//8, activation='relu')(encoded1)

y = Dense(encoding_dim, activation='relu')(encoded2)

decoded2 = Dense(height * width//8, activation='relu')(y)
decoded1 = Dense(height * width//2, activation='relu')(decoded2)

z = Dense(height * width, activation='sigmoid')(decoded1)
autoencoder = Model(input_img, z)

#encoder is the model of the autoencoder slice in the middle 
encoder = Model(input_img, y)
decoder = Model(y, z)
print(encoder)
print(decoder)
使用上面的代码检索编码器部分,但是我无法使用上面添加的代码获取解码器部分:

我收到以下错误:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_39:0", shape=(?, 784), dtype=float32) at layer "input_39". The following previous layers were accessed without issue: []

你能告诉我如何得到那个部件吗?

解码器需要有一个输入层。例如,此处的
解码器输入

高度,宽度=28,28
#编码器层
输入=输入(形状=(高度*宽度)
编码尺寸=高度*宽度//256
x=密度(高度*宽度,激活='relu')(输入\u img)
encoded1=密集(高度*宽度//2,激活='relu')(x)
encoded2=密集(高度*宽度//8,激活='relu')(encoded1)
y=密集(编码尺寸,激活='relu')(编码尺寸2)
#解码器层
解码器输入=输入(形状=(编码尺寸)
decoded2=密集(高度*宽度//8,激活='relu')(解码器输入)
decoded1=密集(高度*宽度//2,激活='relu')(decoded2)
z=密度(高度*宽度,激活='sigmoid')(解码d1)
#建立编码器和解码器模型
编码器=型号(输入信号,y)
解码器=型号(解码器输入,z)
#最后,通过输入编码器将编码器和解码器粘合在一起
#输出到解码器
自动编码器=型号(输入信号、解码器(y))

解码器
模型需要有一个输入层。例如,此处的
解码器输入

高度,宽度=28,28
#编码器层
输入=输入(形状=(高度*宽度)
编码尺寸=高度*宽度//256
x=密度(高度*宽度,激活='relu')(输入\u img)
encoded1=密集(高度*宽度//2,激活='relu')(x)
encoded2=密集(高度*宽度//8,激活='relu')(encoded1)
y=密集(编码尺寸,激活='relu')(编码尺寸2)
#解码器层
解码器输入=输入(形状=(编码尺寸)
decoded2=密集(高度*宽度//8,激活='relu')(解码器输入)
decoded1=密集(高度*宽度//2,激活='relu')(decoded2)
z=密度(高度*宽度,激活='sigmoid')(解码d1)
#建立编码器和解码器模型
编码器=型号(输入信号,y)
解码器=型号(解码器输入,z)
#最后,通过输入编码器将编码器和解码器粘合在一起
#输出到解码器
自动编码器=型号(输入信号、解码器(y))

谢谢,但是对于行
decoded2=density(…)(decoder\u input
,它给出了错误
float()参数必须是字符串或数字,而不是“维度”
对不起,我是用
tf.keras
而不是
keras
测试代码的。我更新了我的答案,现在应该可以了。谢谢,但是对于行
decoded2=density(…)(decoder\u input
,它给出了错误
float()参数必须是字符串或数字,而不是“维度”
对不起,我是用
tf.keras
而不是
keras来测试代码的。我更新了答案,现在应该可以了。