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



我以将CITEseq数据与深度学习集成为例。该代码一直工作到该示例的第三部分,在那里它应该训练自动编码器。由于我是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都是数字阵列,形状分别为(362808617(和(138617(。模型摘要为:

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)

相关内容

  • 没有找到相关文章

最新更新