Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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 ValueError:预期输入具有形状(2),但获得具有形状(16)的数组_Python_Tensorflow_Keras - Fatal编程技术网

Python Keras ValueError:预期输入具有形状(2),但获得具有形状(16)的数组

Python Keras ValueError:预期输入具有形状(2),但获得具有形状(16)的数组,python,tensorflow,keras,Python,Tensorflow,Keras,我已经编写了以下代码,并试图从可变自动编码器模型预测图像: 编码器: input_img = Input(shape=(28, 28, 3)) x = Conv2D(32, 3, padding='same', activation='relu')(input_img) x = Conv2D(64, 3, padding='same', activ

我已经编写了以下代码,并试图从可变自动编码器模型预测图像:

编码器:

input_img = Input(shape=(28, 28, 3))

x = Conv2D(32, 3,
                  padding='same', 
                  activation='relu')(input_img)
x = Conv2D(64, 3,
                  padding='same', 
                  activation='relu',
                  strides=(2, 2))(x)
x = Conv2D(64, 3,
                  padding='same', 
                  activation='relu')(x)
x = Conv2D(64, 3,
                  padding='same', 
                  activation='relu')(x)

x = Flatten()(x)
x = Dense(16, activation='relu')(x)

# Two outputs, latent mean and (log)variance
z_mu = Dense(latent_dim)(x)
z_log_sigma = Dense(latent_dim)(x)

encoder = Model(inputs = input_img, outputs = x)
解码器:

# decoder takes the latent distribution sample as input
decoder_input = Input(K.int_shape(z)[1:])

# Expand to 784 total pixels
x = Dense(np.prod(shape_before_flattening[1:]),
                 activation='relu')(decoder_input)

# reshape
x = Reshape(shape_before_flattening[1:])(x)

# use Conv2DTranspose to reverse the conv layers 
x = Conv2DTranspose(32, 3,
                           padding='same', 
                           activation='relu',
                           strides=(2, 2))(x)
x = Conv2D(3, 3,
                  padding='same', 
                  activation='sigmoid')(x)

# decoder model statement
decoder = Model(decoder_input, x)

# apply the decoder to the sample from the latent distribution
z_decoded = decoder(z)
编码器如下所示:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_13 (InputLayer)        (None, 28, 28, 3)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 28, 28, 32)        896       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 14, 14, 64)        18496     
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 14, 14, 64)        36928     
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 14, 14, 64)        36928     
_________________________________________________________________
flatten_1 (Flatten)          (None, 12544)             0         
_________________________________________________________________
dense_10 (Dense)             (None, 16)                200720    
=================================================================
Total params: 293,968
Trainable params: 293,968
Non-trainable params: 0
以及解码器本身:

Layer (type)                 Output Shape              Param #   
=================================================================
input_15 (InputLayer)        (None, 2)                 0         
_________________________________________________________________
dense_14 (Dense)             (None, 12544)             37632     
_________________________________________________________________
reshape_3 (Reshape)          (None, 14, 14, 64)        0         
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 28, 28, 32)        18464     
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 28, 28, 3)         867       
=================================================================
Total params: 56,963
Trainable params: 56,963
Non-trainable params: 0
_________________________________________________________________
它运行得很好。以下是完整的模型:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_13 (InputLayer)           (None, 28, 28, 3)    0                                            
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 28, 28, 32)   896         input_13[0][0]                   
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 14, 14, 64)   18496       conv2d_1[0][0]                   
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 14, 14, 64)   36928       conv2d_2[0][0]                   
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, 14, 14, 64)   36928       conv2d_3[0][0]                   
__________________________________________________________________________________________________
flatten_1 (Flatten)             (None, 12544)        0           conv2d_4[0][0]                   
__________________________________________________________________________________________________
dense_10 (Dense)                (None, 16)           200720      flatten_1[0][0]                  
__________________________________________________________________________________________________
dense_11 (Dense)                (None, 2)            34          dense_10[0][0]                   
__________________________________________________________________________________________________
dense_12 (Dense)                (None, 2)            34          dense_10[0][0]                   
__________________________________________________________________________________________________
lambda_5 (Lambda)               (None, 2)            0           dense_11[0][0]                   
                                                                 dense_12[0][0]                   
__________________________________________________________________________________________________
model_16 (Model)                (None, 28, 28, 3)    56963       lambda_5[0][0]                   
__________________________________________________________________________________________________
custom_variational_layer_3 (Cus [(None, 28, 28, 3),  0           input_13[0][0]                   
                                                                 model_16[1][0]                   
==================================================================================================
Total params: 350,999
Trainable params: 350,999
Non-trainable params: 0
__________________________________________________________________________________________________
问题是当我试图基于现有图像创建图像时。这将显示训练集中的图像:

rnd_file = np.random.choice(files)
file_id = os.path.basename(rnd_file)
img = imread(rnd_file)
plt.imshow(img)
plt.show()
然后,我将图像添加到编码器以获得图像的潜在表示:

z = encoder.predict(img)
当我有潜在的表征时,我根据给定的表征将其解码成一幅图像:

decoder.predict(z)
这会产生以下错误:

ValueError:检查输入时出错:预期输入_15具有形状(2),但获得具有形状(16)的数组

z看起来像这样:

[0.         0.         0.         0.         0.         0.03668813
 0.10211123 0.08731555 0.         0.01327576 0.         0.
 0.         0.         0.03561973 0.02009114]

编码器的输出为(无,16),与我的z相同。它作为一个模型运行。我怎样才能解决这个问题?提前感谢

有些代码缺失,无法准确理解您想要实现的目标,但至少存在两个问题:

  • 在此示例中,z的大小不是
    (无,16)
    ,而是
    (16,)
    。您需要添加一个维度,例如:
    z=encoder.predict(img[np.newaxis,:])
  • 解码器的输入大小与编码器的输出大小不匹配

有些代码缺失,无法准确理解您想要实现的目标,但至少存在两个问题:

  • 在此示例中,z的大小不是
    (无,16)
    ,而是
    (16,)
    。您需要添加一个维度,例如:
    z=encoder.predict(img[np.newaxis,:])
  • 解码器的输入大小与编码器的输出大小不匹配

错误消息向我表明,它预期的元组长度为2

例如,在这篇介绍文章中:

他们这样做:

    output_tokens, h, c = decoder_model.predict(
        [target_seq] + states_value)

您的代码只传递了
目标_seq
,而不是
状态_值
,这让我明白了为什么会出现该错误。

错误消息向我表明,它需要长度为2的元组

例如,在这篇介绍文章中:

他们这样做:

    output_tokens, h, c = decoder_model.predict(
        [target_seq] + states_value)

您的代码只传递了
目标_seq
,而不是
状态_值
,在我看来,这就是为什么会出现该错误。

编码器工作正常。这是当我尝试解码器,它给出了问题。如果我尝试decoder.predict(z[np.newaxis,:]),我会得到ValueError:检查输入时出错:预期输入为2维,但得到形状为(1,16,16)的数组编码器将一批28x28x3图像作为输入,并返回一批16维张量。解码器具有维度2的输入,因此不能将编码器的输出作为其输入。您确定编码器模型定义吗?在第二行的解码器代码中,z没有定义,但可能与编码器输出不匹配。它对编码器工作正常。这是当我尝试解码器,它给出了问题。如果我尝试decoder.predict(z[np.newaxis,:]),我会得到ValueError:检查输入时出错:预期输入为2维,但得到形状为(1,16,16)的数组编码器将一批28x28x3图像作为输入,并返回一批16维张量。解码器具有维度2的输入,因此不能将编码器的输出作为其输入。您确定编码器模型定义吗?在第2行的解码器代码中,z未定义,但可能与编码器输出不匹配。