关于deep-learning-with-python书籍笔记 2-1

2-1

import keras
keras.__version__
#获得版本


from keras.datasets import mnist

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
#导入数据包,并赋值

train_images.shape
#查看样式 (60000, 28, 28)

len(train_labels)
#长度 60000

train_labels
#内容 array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)

test_images.shape
#(10000, 28, 28)

from keras import models
from keras import layers

network = models.Sequential()
# 第一层定义512个神经元,采用relu作为激活函数,输入的大小为28*28,这应该是一个全连接操作
# 那么对于该层而言,样本量大小为 (28*28+1)*512。对于每一个神经元而言,权重矩阵要与输入矩阵的大小相同,再加上一个偏置项,
# 从而每一个神经元的参数量为 28*28+1,而该层有512个神经元,从而总共需要(28*28+1)*512个参数。
# 最后将得到的输入经过激活函数relu转换,获得的结果作为下一层的输入。
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))
network.summary() 

模型的构成

#Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               401920    
_________________________________________________________________
dense_2 (Dense)              (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0

# 这里是定义训练网络所需的优化器,损失函数,以及定义衡量该模型的好坏
network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])

train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
#对图片矩阵进行处理,把图片的长和宽合并化为二维,之后转换为浮点型并对数值进行归一化处理

from keras.utils import to_categorical

# to_categorical 相当于one-hot encoding,这个主要是为了后面与预测的结果来对比,来计算损失值
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
#转换为one——hot类型,电脑无法详细分辨1、2、3……具有的意义,却可以充分的理解0、1所蕴含的意义故采用one-hot方式进行对数据的处理。

network.fit(train_images, train_labels, epochs=5, batch_size=128)
#让数据进行训练 放入 处理好的图片 和图片对应的标签类型 迭代5次 每个批次为128张图片的矩阵 
Epoch 1/5
60000/60000 [==============================] - 4s 71us/step - loss: 0.2552 - accuracy: 0.9264
Epoch 2/5
60000/60000 [==============================] - 1s 18us/step - loss: 0.1039 - accuracy: 0.9693
Epoch 3/5
60000/60000 [==============================] - 1s 18us/step - loss: 0.0678 - accuracy: 0.9797
Epoch 4/5
60000/60000 [==============================] - 1s 18us/step - loss: 0.0503 - accuracy: 0.9847
Epoch 5/5
60000/60000 [==============================] - 1s 18us/step - loss: 0.0373 - accuracy: 0.9889

获取损失、准确度

test_loss, test_acc = network.evaluate(test_images, test_labels)
#把测试数据和标签放入模型中,取出损失数值与准确度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值