如何解决keras fit函数错误"All input arrays (x) should have the same number of samples"?

How to solve keras fit function error "All input arrays (x) should have the same number of samples"?

我正在效仿 Integrating CITEseq data with Deep Learning 的例子。 该代码一直运行到示例的第三部分,它应该在该部分训练自动编码器。由于我是keras模型的新手,我基本上只是复制和粘贴代码,所以我不知道网站上的如何工作而我的不是。

我试过从

更改拟合函数
estimator = autoencoder.fit([X_scRNAseq, X_scProteomics],
                            [X_scRNAseq, X_scProteomics],
                            epochs = 100, batch_size = 128,
                            validation_split = 0.2, shuffle = True, verbose = 1)

estimator = autoencoder.fit([X_scRNAseq, X_scRNAseq],
                            [X_scRNAseq, X_scRNAseq],
                            epochs = 100, batch_size = 128,
                            validation_split = 0.2, shuffle = True, verbose = 1)

为了解决相同数量的样本问题并且它起作用了,但这并没有按照它应该的方式训练自动编码器。

X_scRNAseq和X_scProteomics都是numpy数组,形状分别为(36280, 8617)和(13, 8617)。 模型总结为:

Model: "model_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
scRNAseq (InputLayer)           (None, 8617)         0                                            
__________________________________________________________________________________________________
scProteomics (InputLayer)       (None, 8617)         0                                            
__________________________________________________________________________________________________
Encoder_scRNAseq (Dense)        (None, 50)           430900      scRNAseq[0][0]                   
__________________________________________________________________________________________________
Encoder_scProteomics (Dense)    (None, 10)           86180       scProteomics[0][0]               
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 60)           0           Encoder_scRNAseq[0][0]           
                                                                 Encoder_scProteomics[0][0]       
__________________________________________________________________________________________________
Bottleneck (Dense)              (None, 50)           3050        concatenate_1[0][0]              
__________________________________________________________________________________________________
Concatenate_Inverse (Dense)     (None, 60)           3060        Bottleneck[0][0]                 
__________________________________________________________________________________________________
Decoder_scRNAseq (Dense)        (None, 8617)         525637      Concatenate_Inverse[0][0]        
__________________________________________________________________________________________________
Decoder_scProteomics (Dense)    (None, 8617)         525637      Concatenate_Inverse[0][0]        
==================================================================================================
Total params: 1,574,464
Trainable params: 1,574,464
Non-trainable params: 0
__________________________________________________________________________________________________

我在尝试应用拟合函数时遇到的错误是:

ValueError: All input arrays (x) should have the same number of samples. Got array shapes: [(36280, 8617), (13, 8617)]

谢谢!

Keras 期望输入数据的第一个轴是样本数。如你所说,X_scRNAseq的形状是(36280, 8617)X_scProteomics的形状是(13, 8617)。 Keras 期望第一个轴是样本数,但在这种情况下并非如此。

我认为,解决方案是重塑 X_scRNAseqX_scProteomics,如下所示:

X_scRNAseq = np.swapaxes(X_scRNAseq, 0, 1)   #(8617, 36280)
X_scProteomics = np.swapaxes(X_scProteomics, 0, 1)  #(8617, 13)

然后,拟合您的模型:

estimator = autoencoder.fit([X_scRNAseq, X_scProteomics],
                            [X_scRNAseq, X_scProteomics],
                            epochs = 100, batch_size = 128,
                            validation_split = 0.2, shuffle = True, verbose = 1)