我想在Python上使用Keras运行一个神经网络样本程序。我的数据以Matlab .mat文件的形式存在。
我该如何使用Keras在Python中加载上述.mat数据以替换MNIST数据集?
如何将它重塑为(3L,2L,2L),这意味着三个形状为(2L,2L)的数据? 答案
train_data.mat (size: 32x32x10,000 single)
train_label.mat (size: 1x10,000 single)
test_data.mat (size: 32x32x2,000 single)
test_label.mat (size: 1x2,000 single)
我该如何使用Keras在Python中加载上述.mat数据以替换MNIST数据集?
from keras.datasets import mnist
(train_data, train_label), (test_data, test_label) = mnist.load_data()
编辑(仅供说明)
假设我的.train_data文件是.mat格式,其中包含三个数据,大小为2x2x3。
val(:,:,1) =
1 1
1 1
val(:,:,2) =
2 2
2 2
val(:,:,3) =
3 3
3 3
使用scipy.io.loadmat加载后,它的大小为(2L,2L,3L)。
>>> A
array([[[1, 2, 3],
[1, 2, 3]],
[[1, 2, 3],
[1, 2, 3]]], dtype=uint8)
如何将它重塑为(3L,2L,2L),这意味着三个形状为(2L,2L)的数据? 答案
>>> import scipy.io
>>> A = scipy.io.loadmat('train_data')
>>> B = A.flatten(1) # flatten to vector
>>> C = B.reshape(3,2,2) # reshape
>>> C
array([[[1, 1],
[1, 1]],
[[2, 2],
[2, 2]],
[[3, 3],
[3, 3]]], dtype=uint8)