我有一个看起来像的ndarray
[[array([0. , 0. ... 0. ])],
...
[array([0. , 0. ... 0. ])]]
其中每个子数组都是784个np.float64值的一维数组
当我尝试运行看起来像'''Dense(32,input_shape =(784,))'的第一层的Keras时,training_utils引发ValueError,因为它认为我的数组的形状为(1,)预计何时(784,)
如果我将数组下标,则会得到array[0].shape == (784,)
我在这里做错了什么?我尝试使用keras.np_utils.to_categorical()函数等进行转置。
我确定这很简单,但我想念它
class netModel(object):
def __init__(self, model, TrainingData, Labels):
self.TrainingData = TrainingData
self.Labels = Labels
self.model = model
self.model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
def train(self):
self.model.fit(self.TrainingData, self.Labels)
y = mnConv.mnistConverter.convert("train-images.idx3-ubyte",
"train-labels.idx1-ubyte", 60000)
md = Sequential([
Dense(32, input_shape=(784,)),
Activation('relu'),
Dense(28),
Activation('sigmoid'),
Dense(10),
Activation('softmax'),
])
mod = netModel(md,td,lbl)
mod.train()
和mnistCoverter函数如下:
class mnistConverter(object):
def convert(imgf, labelf, n):
f = open(imgf, "rb")
l = open(labelf, "rb")
f.read(16)
l.read(8)
images = []
for i in range(n):
label, image = [np.ndarray(10,),np.ndarray(784,)]#shhh, Ignore this
lbl = ord(l.read(1))
np1 = np.float64(1)
np0 = np.float64(0)
for i in range(10):
if lbl == i:
label[i] = np1
else:
label[i] = np0
for j in range(28*28):
image[j] = fit(ord(f.read(1)))
images.append((lbl, image))
f.close()
l.close()
return np.array(images)