- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我问这个是因为我觉得我缺少一些基本的东西。
现在大多数人都知道 MNIST 图像是 28X28 像素。 keras documentation告诉我有关 Dense 的信息:
Input shape nD tensor with shape: (batch_size, ..., input_dim). The most common situation would be a 2D input with shape (batch_size, input_dim).
所以像我这样的新手会假设图像可以作为 28*28 矩阵输入到模型中。然而,我找到的每个教程都经过各种体操,将图像转换为一个 784 长的特征。
有时是
num_pixels = X_train.shape[1] * X_train.shape[2]
model.add(Dense(num_pixels, input_dim=num_pixels, activation='...'))
或
num_pixels = np.prod(X_train.shape[1:])
model.add(Dense(512, activation='...', input_shape=(num_pixels,)))
或
model.add(Dense(units=10, input_dim=28*28, activation='...'))
history = model.fit(X_train.reshape((-1,28*28)), ...)
甚至:
model = Sequential([Dense(32, input_shape=(784,)), ...),])
所以我的问题很简单 - 为什么?难道 Dense 不能按原样接受图像,或者在必要时“在幕后”处理它吗?如果像我怀疑的那样必须进行此处理,那么这些方法(或其他方法)中的任何一种在本质上是否更可取?
最佳答案
应 OP(即 Original Poster)的要求,我将在评论中提及我给出的答案并详细说明。
Can't Dense just accept an image as-is or, if necessary, just process it "behind the scenes", as it were?
根本不!那是因为 currently the Dense layer is applied on the last axis .因此,如果您为其提供形状为 (height, width)
或 (height, width, channels)
的图像,Dense 层将仅应用于最后一个轴(即宽度或 channel )。然而,当图像被扁平化时,Dense 层中的所有单元将应用于整个图像,并且每个单元连接到具有不同权重的所有像素。为了进一步阐明这一点,请考虑以下模型:
model = models.Sequential()
model.add(layers.Dense(10, input_shape=(28*28,)))
model.summary()
模型总结:
Layer (type) Output Shape Param #
=================================================================
dense_2 (Dense) (None, 10) 7850
=================================================================
Total params: 7,850
Trainable params: 7,850
Non-trainable params: 0
_________________________________________________________________
如您所见,Dense 层中有 7850 个参数:每个单元连接到所有像素(28*28*10 + 10 个偏置参数 = 7850)。现在考虑这个模型:
model = models.Sequential()
model.add(layers.Dense(10, input_shape=(28,28)))
model.summary()
模型总结:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_3 (Dense) (None, 28, 10) 290
=================================================================
Total params: 290
Trainable params: 290
Non-trainable params: 0
_________________________________________________________________
在这种情况下,Dense 层中只有 290 个参数。这里 Dense 层中的每个单元也连接到所有像素,但不同之处在于权重在第一个轴上共享(28*10 + 10 偏置参数 = 290)。与之前在整个图像中提取特征的模型相比,就好像是从图像的每一行中提取特征一样。因此,这(即权重共享)可能对您的应用有用,也可能没有用。
关于python - Keras 顺序密集输入层 - 和 MNIST : why do images need to be reshaped?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184142/
我的机器学习算法已经学习了 MNIST 数据库中的 70000 张图像。我想在 MNIST 数据集中未包含的图像上对其进行测试。但是,我的预测函数无法读取我的测试图像的数组表示。 如何在外部图像上测试
我正在尝试创建我自己的 MNIST 数据版本。我已将训练和测试数据转换为以下文件; test-images-idx3-ubyte.gz test-labels-idx1-ubyte.gz train-
我通过 pip 在我的 Windows 设备上安装了 python-mnist 包,正如 Github 文档中所述,方法是在我的 Anaconda 终端中输入以下命令: pip install pyt
描述 Fashion Mnist 是一个类似于 Mnist 的图像数据集. 涵盖 10 种类别的 7 万 (6 万训练集 + 1 万测试集) 个不同商品的图片. Tensor
该模型现在只能使用 tf. 识别单个字母。我怎样才能让它识别连续的字母单词? 最佳答案 手写数字识别。 ... MNIST 是一个广泛用于手写数字分类任务的数据集。它由 70,000 个标记为 28x
我已经从 MNIST 训练集中解压了第一张图像,并且可以访问 (28,28) 矩阵。 [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0
这是我学习的一部分。我知道标准化确实有助于提高准确性,因此将 mnist 值除以 255。这会将所有像素除以 255,因此 28*28 的所有像素的值将在 0.0 到 1.0 范围内. 现在我厌倦了将
我已成功将 MNIST 数据下载到扩展名为 .npy 的文件中。当我打印第一张图像的几列时。我得到以下结果。这里每个数字代表什么? a= np.load("training_set.npy") pri
我用tensorflow写了一个程序来处理Kaggle的数字识别问题。程序可以正常运行,但训练准确率总是很低,大约10%,如下: step 0, training accuracy 0.11 step
在 cnn_mnist.py例如,脚本首先加载训练和测试数据,如您从 120 行到 124 行中看到的那样。当我打印 print(train_data.shape) 时,我得到 (55000, 784
我研究神经网络有一段时间了,用python和numpy做了一个实现。我用 XOR 做了一个非常简单的例子,它运行良好。所以我想我更进一步尝试 MNIST 数据库。 这是我的问题。我正在使用具有 784
我目前正在研究手写数字识别问题。 首先,我针对 MNIST 数据集测试了示例手写数字。 我的准确率为 53%,我需要 90% 以上的准确率。 以下是我迄今为止为提高准确性所做的尝试。 创建了我自己的数
我正在尝试使用我自己的数字图像数据集测试 mnist。 我为此写了一个 python 脚本,但它给出了一个错误。错误在代码的第 16 行。实际上我无法发送图像进行测试。给我一些建议。提前致谢。 imp
我知道这可能是一个愚蠢的问题,但我真的不明白为什么。下面是我尝试从训练数据中打印单个图像和具有相同索引的标签的代码 import matplotlib.pyplot as plt from tenso
我尝试使用以下数据集在 python 中制作一个能够识别手写数字的脚本:http://deeplearning.net/data/mnist/mnist.pkl.gz . 关于这个问题和我试图实现的算
我正在尝试解决 Android 设备上的 MNIST 分类问题。我已经有一个经过训练的模型,现在我希望能够识别照片上的单个数字。 拍完照片后,我会进行一些预处理,然后再将图像传递给模型。这是原始图像的
MNIST 数据集介绍 MNIST 包含 0~9 的手写数字, 共有 60000 个训练集和 10000 个测试集. 数据的格式为单通道 28*28 的灰度图. LeNet 模型
我想导入 mnist digits 数字以在一个图中显示,并编写这样的代码, import keras from keras.datasets import mnist import matplotl
我目前正在研究数字手写识别问题。我发现很多state-of-art算法对mnist dateset采用了一些预处理方法,比如deskewing和jittering(我不知道'jittering'是什么
我到处找,但找不到我想要的。基本上,MNIST 数据集具有像素值在范围 [0, 255] 内的图像。 .人们说,一般来说,最好做到以下几点: 将数据缩放到 [0,1]范围。 将数据标准化为具有零均值和
我是一名优秀的程序员,十分优秀!