Machine learning 基于keras的手写数字识别

Machine learning 基于keras的手写数字识别,machine-learning,neural-network,keras,mnist,Machine Learning,Neural Network,Keras,Mnist,我正在努力学习。我看到了识别手写数字的机器学习代码(也给出了)。它似乎有前馈、SGD和反向传播方法是从头开始编写的。我只是想知道是否有可能用Keras编写这个程序?我们希望您能朝着这个方向迈出一步。您可以先用它来了解MNIST数据集是如何为MLP工作的。。当您继续时,您可以了解CNN如何在MNIST数据集上工作 我将描述您在评论中附加的keras代码的一些过程 # Step 1: Organize Data batch_size = 128 # This is split the 60k i

我正在努力学习。我看到了识别手写数字的机器学习代码(也给出了)。它似乎有前馈、SGD和反向传播方法是从头开始编写的。我只是想知道是否有可能用Keras编写这个程序?我们希望您能朝着这个方向迈出一步。

您可以先用它来了解MNIST数据集是如何为MLP工作的。。当您继续时,您可以了解CNN如何在MNIST数据集上工作

我将描述您在评论中附加的keras代码的一些过程

# Step 1: Organize Data
batch_size = 128   # This is split the 60k images into batches of 128, normally people use 100. It's up to you
num_classes = 10   # Your final layer. Basically number 0 - 9 (10 classes)
epochs = 20        # 20 'runs'. You can increase or decrease to see the change in accuracy. Normally MNIST accuracy peaks at around 10-20 epochs.

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()  #X_train - Your training images, y_train - training labels; x_test - test images, y_test - test labels. Normally people train on 50k train images, 10k test images.

x_train = x_train.reshape(60000, 784) # Each MNIST image is 28x28 pixels. So you are flattening into a 28x28 = 784 array. 60k train images
x_test = x_test.reshape(10000, 784)   # Likewise, 10k test images
x_train = x_train.astype('float32')   # For float numbers
x_test = x_test.astype('float32')
x_train /= 255                        # For normalization. Each image has a 'degree' of darkness within the range of 0-255, so you want to reduce that range to 0 - 1 for your Neural Network
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)  # One-hot encoding. So when your NN is trained, your prediction for 5(example) will look like this [0000010000] (Final layer).
y_test = keras.utils.to_categorical(y_test, num_classes)


# Step 2: Create MLP model
model = Sequential()     
model.add(Dense(512, activation='relu', input_shape=(784,)))    #First hidden layer, 512 neurons, activation relu, input 784 array
model.add(Dropout(0.2))                                         # During the training, layer has 20% probability of 'switching off' certain neurons
model.add(Dense(512, activation='relu'))                        # Same as above
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))   # Final layer, 10 neurons, softmax is a probability function to give the best probability of the input image

model.summary()


# Step 3: Create model compilation
model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])
# 10 classes - categorical_crossentropy. If 2 classes, you can use binary_crossentropy; optimizer - RMSprop, you can change this to ADAM, SGD, etc...; metrics - accuracy


# Step 4: Train model
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))
# Training happens here. Train on each batch size for 20 runs, the validate your result on the test set.

# Step 5: See results on your test data
score = model.evaluate(x_test, y_test, verbose=0)
# Prints out scores
print('Test loss:', score[0])
print('Test accuracy:', score[1])

您可以首先使用它来了解MNIST数据集如何为MLP工作。。当您继续时,您可以了解CNN如何在MNIST数据集上工作

我将描述您在评论中附加的keras代码的一些过程

# Step 1: Organize Data
batch_size = 128   # This is split the 60k images into batches of 128, normally people use 100. It's up to you
num_classes = 10   # Your final layer. Basically number 0 - 9 (10 classes)
epochs = 20        # 20 'runs'. You can increase or decrease to see the change in accuracy. Normally MNIST accuracy peaks at around 10-20 epochs.

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()  #X_train - Your training images, y_train - training labels; x_test - test images, y_test - test labels. Normally people train on 50k train images, 10k test images.

x_train = x_train.reshape(60000, 784) # Each MNIST image is 28x28 pixels. So you are flattening into a 28x28 = 784 array. 60k train images
x_test = x_test.reshape(10000, 784)   # Likewise, 10k test images
x_train = x_train.astype('float32')   # For float numbers
x_test = x_test.astype('float32')
x_train /= 255                        # For normalization. Each image has a 'degree' of darkness within the range of 0-255, so you want to reduce that range to 0 - 1 for your Neural Network
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)  # One-hot encoding. So when your NN is trained, your prediction for 5(example) will look like this [0000010000] (Final layer).
y_test = keras.utils.to_categorical(y_test, num_classes)


# Step 2: Create MLP model
model = Sequential()     
model.add(Dense(512, activation='relu', input_shape=(784,)))    #First hidden layer, 512 neurons, activation relu, input 784 array
model.add(Dropout(0.2))                                         # During the training, layer has 20% probability of 'switching off' certain neurons
model.add(Dense(512, activation='relu'))                        # Same as above
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))   # Final layer, 10 neurons, softmax is a probability function to give the best probability of the input image

model.summary()


# Step 3: Create model compilation
model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(),
              metrics=['accuracy'])
# 10 classes - categorical_crossentropy. If 2 classes, you can use binary_crossentropy; optimizer - RMSprop, you can change this to ADAM, SGD, etc...; metrics - accuracy


# Step 4: Train model
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))
# Training happens here. Train on each batch size for 20 runs, the validate your result on the test set.

# Step 5: See results on your test data
score = model.evaluate(x_test, y_test, verbose=0)
# Prints out scores
print('Test loss:', score[0])
print('Test accuracy:', score[1])

请参见MLP模型和CNN模型…这些可以用于其他彩色图像数据集吗?您对此的关键字搜索是CIFAR而不是MNIST请参见MLP模型和CNN模型…这些可以用于其他彩色图像数据集吗?您对此的关键字搜索是CIFAR而不是MNIST,说明得非常清楚。我想问两点:我问题中的第一个链接使用
net=network.network([784,30,10])
net=network.network([784,100,10])
,而你似乎在3个主要层中使用
784512,10
神经元。如何选择神经元的数量?此外,在tensorflow教程链接中,使用了您根本没有使用过的
flatte()
。它是什么?使用它很重要吗?另外,
SGD
似乎在原始链接中使用,而在tensorflow教程中使用了
RMSprop
。为什么呢?除了这些问题,我准备接受你的回答。谢谢。1)关于你的第一个问题,没有确切的规则可供使用。大多数人通常使用二进制格式,128、256、512等。。。你可以从784-100-10开始。我现在用的是100-50-25-10。到目前为止,我还没有找到任何关于“最佳”神经元数量的信息。但是最好不要使用像4096这样的太多连接,因为您有4096x10连接,这会给您带来太多的权重,并且会影响您的运行时/内存。MNIST仍然很小,但当您处理较大的数据集时,它会成为一个问题。2)Keras是x.reforme,tensorflow是flatte(),但它们都提供相同的功能3)您的第三个问题是关于优化器的。您可以阅读以了解有关具体细节的更多信息。将告诉您为什么需要优化器,并提供很好的说明。我想问两点:我问题中的第一个链接使用
net=network.network([784,30,10])
net=network.network([784,100,10])
,而你似乎在3个主要层中使用
784512,10
神经元。如何选择神经元的数量?此外,在tensorflow教程链接中,使用了您根本没有使用过的
flatte()
。它是什么?使用它很重要吗?另外,
SGD
似乎在原始链接中使用,而在tensorflow教程中使用了
RMSprop
。为什么呢?除了这些问题,我准备接受你的回答。谢谢。1)关于你的第一个问题,没有确切的规则可供使用。大多数人通常使用二进制格式,128、256、512等。。。你可以从784-100-10开始。我现在用的是100-50-25-10。到目前为止,我还没有找到任何关于“最佳”神经元数量的信息。但是最好不要使用像4096这样的太多连接,因为您有4096x10连接,这会给您带来太多的权重,并且会影响您的运行时/内存。MNIST仍然很小,但当您处理较大的数据集时,它会成为一个问题。2)Keras是x.reforme,tensorflow是flatte(),但它们都提供相同的功能3)您的第三个问题是关于优化器的。您可以阅读以了解有关具体细节的更多信息。将告诉您为什么需要优化器