在Neural Network (Dense layer)中使用2d矩阵作为输入



我正在尝试使用2D输入来适应一个简单的网络。

每个样本是一个大小为(69,11)的二维矩阵,在下面的例子中有100个样本。

在阅读了大量的文档和示例之后,我仍然无法理解应该如何重塑输入以使其工作。

下面是我试图调试的一个完整的玩具示例


from random import randint, choice
import random
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D
import numpy as np
########## Create dummy data
n = 100 # Number of sample
y = np.array([randint(0,1) for i in range(n)])
dim1 = 69
dim2 = 11
X = []
for i in range(n):
m = [ np.array([random.uniform(0, 1) for z in range(dim2)])
for j in range(dim1)]
X.append(m)
print(np.shape(X))
print(np.shape(y))
########## Build model
# define model
model = Sequential()
model.add(Dense(units=11, 
input_shape =(dim1, dim2), # The input for each sample if matrix of size (dim1, dim2)
activation='relu'))
model.add(Dense(units=11, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.build()
model.summary()

模型简介:

Model: "sequential_16"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_42 (Dense)             (None, 11)                770       
_________________________________________________________________
dense_43 (Dense)             (None, 1)                 12        
_________________________________________________________________
dense_44 (Dense)             (None, 1)                 2         
=================================================================
Total params: 784
Trainable params: 784
Non-trainable params: 0
_________________________________________________________________
######## Fit model
model.fit(X, y)

我尝试过不同的形状/重塑,但我总是同样的错误:

ValueError: Data cardinality is ambiguous: 
'Make sure all arrays contain the same number of samples'

任何帮助了解我做错了什么将是非常感激。谢谢!

试试这个

model.fit(np.asarray(X),y)

原因是你正在传递一个单独数组列表的列表(其中每个X行由多个1维数组组成),即:

X[0] = array([0.45123387, 0.87071843, 0.69511518, 0.26712839, 0.188283  ,
0.08668649, 0.74399605, 0.86054076, 0.57857035, 0.84557558,
0.89003992]),
array([0.2674208 , 0.57081607, 0.40440186, 0.61519382, 0.31803269,
0.50105886, 0.23100507, 0.39379568, 0.93477072, 0.44786942,
0.83466262]),
array([0.80509187, 0.89998605, 0.05668083, 0.23385173, 0.16405601,
0.09092941, 0.30496969, 0.15214225, 0.66206999, 0.74718661,
0.41340515]),
array([0.89004127, 0.58602522, 0.27362169, 0.62423875, 0.24323739,
0.3250751 , 0.93245436, 0.73747682, 0.01293388, 0.57064239,
0.83064691]),
array([0.14388339, 0.28463899, 0.19126419, 0.04180422, 0.22636071,
0.54998281, 0.18228747, 0.46457567, 0.73606774, 0.11796856,
0.87206122]),
array([0.15999211, 0.52850844, 0.07298599, 0.34844709, 0.98739649,
0.93398197, 0.87649617, 0.34047258, 0.4279549 , 0.2019767 ,
0.98478869])]

而执行np.asarray(X)将正确地将它们连接在一起,每行2个dim数组:

np.asarray(X)[0] = array([[4.86305839e-01, 2.56291260e-01, 7.29621764e-01, 2.06063874e-01,
5.83952341e-01, 2.53645318e-01, 9.04828007e-02, 3.64097487e-01,
7.82886665e-01, 1.01666319e-01, 3.10953920e-01],
[9.42449100e-01, 9.16760076e-01, 3.53338416e-01, 8.50019096e-01,
6.11333665e-01, 1.48142485e-01, 4.51559551e-01, 5.05683894e-01,
7.10124178e-01, 2.50465115e-02, 8.04407535e-01],
[1.77165127e-01, 6.62900151e-01, 8.54372391e-01, 2.63979924e-01,
5.49613031e-01, 8.01454545e-01, 9.55028259e-01, 6.99782063e-01,
2.32189729e-02, 1.56046184e-01, 8.07771437e-01],
[4.43235102e-01, 3.76750360e-01, 8.59820103e-02, 7.04480200e-01,
4.27619788e-01, 6.30606778e-01, 5.55465460e-01, 2.05762014e-01,
6.08090365e-01, 5.62663484e-01, 8.21047097e-01],

最新更新