首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >基于keras的手写体数字识别

基于keras的手写体数字识别
EN

Stack Overflow用户
提问于 2018-09-02 16:55:08
回答 1查看 303关注 0票数 0

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-03 07:56:47

您可以使用它来了解MNIST数据集如何为MLP first.Keras MNIST tutorial工作。随着您的学习,您可以了解CNN是如何处理MNIST数据集的。

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

代码语言:javascript
复制
# 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])
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52135407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档