提交 104c4078 编写于 作者: X xiaoting 提交者: lvmengsi

update 02 book for 1.6 (#817)

上级 db3cdc05
...@@ -195,7 +195,7 @@ import paddle.fluid as fluid ...@@ -195,7 +195,7 @@ import paddle.fluid as fluid
### Program Functions 配置 ### Program Functions 配置
我们需要设置 `inference_program` 函数。我们想用这个程序来演示三个不同的分类器,每个分类器都定义为 Python 函数。 我们需要设置 `inference_program` 函数。我们想用这个程序来演示三个不同的分类器,每个分类器都定义为 Python 函数。
我们需要将图像数据输入到分类器中。Paddle 为读取数据提供了一个特殊的层 `layer.data` 层。 我们需要将图像数据输入到分类器中。Paddle 为读取数据提供了一个特殊的层 `fluid.data` 层。
让我们创建一个数据层来读取图像并将其连接到分类网络。 让我们创建一个数据层来读取图像并将其连接到分类网络。
- Softmax回归:只通过一层简单的以softmax为激活函数的全连接层,就可以得到分类的结果。 - Softmax回归:只通过一层简单的以softmax为激活函数的全连接层,就可以得到分类的结果。
...@@ -209,7 +209,7 @@ def softmax_regression(): ...@@ -209,7 +209,7 @@ def softmax_regression():
predict_image -- 分类的结果 predict_image -- 分类的结果
""" """
# 输入的原始图像数据,大小为28*28*1 # 输入的原始图像数据,大小为28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# 以softmax为激活函数的全连接层,输出层的大小必须为数字的个数10 # 以softmax为激活函数的全连接层,输出层的大小必须为数字的个数10
predict = fluid.layers.fc( predict = fluid.layers.fc(
input=img, size=10, act='softmax') input=img, size=10, act='softmax')
...@@ -229,7 +229,7 @@ def multilayer_perceptron(): ...@@ -229,7 +229,7 @@ def multilayer_perceptron():
predict_image -- 分类的结果 predict_image -- 分类的结果
""" """
# 输入的原始图像数据,大小为28*28*1 # 输入的原始图像数据,大小为28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# 第一个全连接层,激活函数为ReLU # 第一个全连接层,激活函数为ReLU
hidden = fluid.layers.fc(input=img, size=200, act='relu') hidden = fluid.layers.fc(input=img, size=200, act='relu')
# 第二个全连接层,激活函数为ReLU # 第二个全连接层,激活函数为ReLU
...@@ -251,7 +251,7 @@ def convolutional_neural_network(): ...@@ -251,7 +251,7 @@ def convolutional_neural_network():
predict -- 分类的结果 predict -- 分类的结果
""" """
# 输入的原始图像数据,大小为28*28*1 # 输入的原始图像数据,大小为28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# 第一个卷积-池化层 # 第一个卷积-池化层
# 使用20个5*5的滤波器,池化大小为2,池化步长为2,激活函数为Relu # 使用20个5*5的滤波器,池化大小为2,池化步长为2,激活函数为Relu
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = fluid.nets.simple_img_conv_pool(
...@@ -296,7 +296,7 @@ def train_program(): ...@@ -296,7 +296,7 @@ def train_program():
""" """
# 标签层,名称为label,对应输入图片的类别标签 # 标签层,名称为label,对应输入图片的类别标签
label = fluid.layers.data(name='label', shape=[1], dtype='int64') label = fluid.data(name='label', shape=[-1, 1], dtype='int64')
# predict = softmax_regression() # 取消注释将使用 Softmax回归 # predict = softmax_regression() # 取消注释将使用 Softmax回归
# predict = multilayer_perceptron() # 取消注释将使用 多层感知器 # predict = multilayer_perceptron() # 取消注释将使用 多层感知器
......
...@@ -174,7 +174,7 @@ import paddle.fluid as fluid ...@@ -174,7 +174,7 @@ import paddle.fluid as fluid
### Program Functions Configuration ### Program Functions Configuration
We need to configure `inference_program` function. We want to use this program to show three different classifiers, each of which is defined as a Python function. We need to configure `inference_program` function. We want to use this program to show three different classifiers, each of which is defined as a Python function.
We need to input the image data into the classifier. Paddle provides a special layer `layer.data` for reading data. We need to input the image data into the classifier. Paddle provides a special layer `fluid.data` for reading data.
Let's create a data layer to read the image and connect it to the network of classification. Let's create a data layer to read the image and connect it to the network of classification.
-Softmax regression: The results of the classification can be obtained only through a simple layer of simple fully connected layer with softmax as the activation function. -Softmax regression: The results of the classification can be obtained only through a simple layer of simple fully connected layer with softmax as the activation function.
...@@ -188,7 +188,7 @@ def softmax_regression(): ...@@ -188,7 +188,7 @@ def softmax_regression():
predict_image -- result of classification predict_image -- result of classification
""" """
# input original image data in size of 28*28*1 # input original image data in size of 28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# With softmax as the fully connected layer of the activation function, the size of the output layer must be 10 # With softmax as the fully connected layer of the activation function, the size of the output layer must be 10
predict = fluid.layers.fc( predict = fluid.layers.fc(
input=img, size=10, act='softmax') input=img, size=10, act='softmax')
...@@ -208,7 +208,7 @@ def multilayer_perceptron(): ...@@ -208,7 +208,7 @@ def multilayer_perceptron():
predict_image -- result of classification predict_image -- result of classification
""" """
# input raw image data in size of 28*28*1 # input raw image data in size of 28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# the first fully connected layer, whose activation function is ReLU # the first fully connected layer, whose activation function is ReLU
hidden = fluid.layers.fc(input=img, size=200, act='relu') hidden = fluid.layers.fc(input=img, size=200, act='relu')
# the second fully connected layer, whose activation function is ReLU # the second fully connected layer, whose activation function is ReLU
...@@ -230,7 +230,7 @@ def convolutional_neural_network(): ...@@ -230,7 +230,7 @@ def convolutional_neural_network():
predict -- result of classification predict -- result of classification
""" """
# input raw image data in size of 28*28*1 # input raw image data in size of 28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# the first convolution-pooling layer # the first convolution-pooling layer
# Use 20 5*5 filters, the pooling size is 2, the pooling step is 2, and the activation function is Relu. # Use 20 5*5 filters, the pooling size is 2, the pooling step is 2, and the activation function is Relu.
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = fluid.nets.simple_img_conv_pool(
...@@ -275,7 +275,7 @@ def train_program(): ...@@ -275,7 +275,7 @@ def train_program():
""" """
# label layer, called label, correspondent with label category of input picture # label layer, called label, correspondent with label category of input picture
label = fluid.layers.data(name='label', shape=[1], dtype='int64') label = fluid.data(name='label', shape=[-1, 1], dtype='int64')
# predict = softmax_regression() # cancel note and run Softmax regression # predict = softmax_regression() # cancel note and run Softmax regression
# predict = multilayer_perceptron() # cancel note and run multiple perceptron # predict = multilayer_perceptron() # cancel note and run multiple perceptron
......
...@@ -237,7 +237,7 @@ import paddle.fluid as fluid ...@@ -237,7 +237,7 @@ import paddle.fluid as fluid
### Program Functions 配置 ### Program Functions 配置
我们需要设置 `inference_program` 函数。我们想用这个程序来演示三个不同的分类器,每个分类器都定义为 Python 函数。 我们需要设置 `inference_program` 函数。我们想用这个程序来演示三个不同的分类器,每个分类器都定义为 Python 函数。
我们需要将图像数据输入到分类器中。Paddle 为读取数据提供了一个特殊的层 `layer.data` 层。 我们需要将图像数据输入到分类器中。Paddle 为读取数据提供了一个特殊的层 `fluid.data` 层。
让我们创建一个数据层来读取图像并将其连接到分类网络。 让我们创建一个数据层来读取图像并将其连接到分类网络。
- Softmax回归:只通过一层简单的以softmax为激活函数的全连接层,就可以得到分类的结果。 - Softmax回归:只通过一层简单的以softmax为激活函数的全连接层,就可以得到分类的结果。
...@@ -251,7 +251,7 @@ def softmax_regression(): ...@@ -251,7 +251,7 @@ def softmax_regression():
predict_image -- 分类的结果 predict_image -- 分类的结果
""" """
# 输入的原始图像数据,大小为28*28*1 # 输入的原始图像数据,大小为28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# 以softmax为激活函数的全连接层,输出层的大小必须为数字的个数10 # 以softmax为激活函数的全连接层,输出层的大小必须为数字的个数10
predict = fluid.layers.fc( predict = fluid.layers.fc(
input=img, size=10, act='softmax') input=img, size=10, act='softmax')
...@@ -271,7 +271,7 @@ def multilayer_perceptron(): ...@@ -271,7 +271,7 @@ def multilayer_perceptron():
predict_image -- 分类的结果 predict_image -- 分类的结果
""" """
# 输入的原始图像数据,大小为28*28*1 # 输入的原始图像数据,大小为28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# 第一个全连接层,激活函数为ReLU # 第一个全连接层,激活函数为ReLU
hidden = fluid.layers.fc(input=img, size=200, act='relu') hidden = fluid.layers.fc(input=img, size=200, act='relu')
# 第二个全连接层,激活函数为ReLU # 第二个全连接层,激活函数为ReLU
...@@ -293,7 +293,7 @@ def convolutional_neural_network(): ...@@ -293,7 +293,7 @@ def convolutional_neural_network():
predict -- 分类的结果 predict -- 分类的结果
""" """
# 输入的原始图像数据,大小为28*28*1 # 输入的原始图像数据,大小为28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# 第一个卷积-池化层 # 第一个卷积-池化层
# 使用20个5*5的滤波器,池化大小为2,池化步长为2,激活函数为Relu # 使用20个5*5的滤波器,池化大小为2,池化步长为2,激活函数为Relu
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = fluid.nets.simple_img_conv_pool(
...@@ -338,7 +338,7 @@ def train_program(): ...@@ -338,7 +338,7 @@ def train_program():
""" """
# 标签层,名称为label,对应输入图片的类别标签 # 标签层,名称为label,对应输入图片的类别标签
label = fluid.layers.data(name='label', shape=[1], dtype='int64') label = fluid.data(name='label', shape=[-1, 1], dtype='int64')
# predict = softmax_regression() # 取消注释将使用 Softmax回归 # predict = softmax_regression() # 取消注释将使用 Softmax回归
# predict = multilayer_perceptron() # 取消注释将使用 多层感知器 # predict = multilayer_perceptron() # 取消注释将使用 多层感知器
......
...@@ -216,7 +216,7 @@ import paddle.fluid as fluid ...@@ -216,7 +216,7 @@ import paddle.fluid as fluid
### Program Functions Configuration ### Program Functions Configuration
We need to configure `inference_program` function. We want to use this program to show three different classifiers, each of which is defined as a Python function. We need to configure `inference_program` function. We want to use this program to show three different classifiers, each of which is defined as a Python function.
We need to input the image data into the classifier. Paddle provides a special layer `layer.data` for reading data. We need to input the image data into the classifier. Paddle provides a special layer `fluid.data` for reading data.
Let's create a data layer to read the image and connect it to the network of classification. Let's create a data layer to read the image and connect it to the network of classification.
-Softmax regression: The results of the classification can be obtained only through a simple layer of simple fully connected layer with softmax as the activation function. -Softmax regression: The results of the classification can be obtained only through a simple layer of simple fully connected layer with softmax as the activation function.
...@@ -230,7 +230,7 @@ def softmax_regression(): ...@@ -230,7 +230,7 @@ def softmax_regression():
predict_image -- result of classification predict_image -- result of classification
""" """
# input original image data in size of 28*28*1 # input original image data in size of 28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# With softmax as the fully connected layer of the activation function, the size of the output layer must be 10 # With softmax as the fully connected layer of the activation function, the size of the output layer must be 10
predict = fluid.layers.fc( predict = fluid.layers.fc(
input=img, size=10, act='softmax') input=img, size=10, act='softmax')
...@@ -250,7 +250,7 @@ def multilayer_perceptron(): ...@@ -250,7 +250,7 @@ def multilayer_perceptron():
predict_image -- result of classification predict_image -- result of classification
""" """
# input raw image data in size of 28*28*1 # input raw image data in size of 28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# the first fully connected layer, whose activation function is ReLU # the first fully connected layer, whose activation function is ReLU
hidden = fluid.layers.fc(input=img, size=200, act='relu') hidden = fluid.layers.fc(input=img, size=200, act='relu')
# the second fully connected layer, whose activation function is ReLU # the second fully connected layer, whose activation function is ReLU
...@@ -272,7 +272,7 @@ def convolutional_neural_network(): ...@@ -272,7 +272,7 @@ def convolutional_neural_network():
predict -- result of classification predict -- result of classification
""" """
# input raw image data in size of 28*28*1 # input raw image data in size of 28*28*1
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
# the first convolution-pooling layer # the first convolution-pooling layer
# Use 20 5*5 filters, the pooling size is 2, the pooling step is 2, and the activation function is Relu. # Use 20 5*5 filters, the pooling size is 2, the pooling step is 2, and the activation function is Relu.
conv_pool_1 = fluid.nets.simple_img_conv_pool( conv_pool_1 = fluid.nets.simple_img_conv_pool(
...@@ -317,7 +317,7 @@ def train_program(): ...@@ -317,7 +317,7 @@ def train_program():
""" """
# label layer, called label, correspondent with label category of input picture # label layer, called label, correspondent with label category of input picture
label = fluid.layers.data(name='label', shape=[1], dtype='int64') label = fluid.data(name='label', shape=[-1, 1], dtype='int64')
# predict = softmax_regression() # cancel note and run Softmax regression # predict = softmax_regression() # cancel note and run Softmax regression
# predict = multilayer_perceptron() # cancel note and run multiple perceptron # predict = multilayer_perceptron() # cancel note and run multiple perceptron
......
...@@ -101,8 +101,8 @@ def train(nn_type, ...@@ -101,8 +101,8 @@ def train(nn_type,
test_reader = paddle.batch( test_reader = paddle.batch(
paddle.dataset.mnist.test(), batch_size=BATCH_SIZE) paddle.dataset.mnist.test(), batch_size=BATCH_SIZE)
img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32') img = fluid.data(name='img', shape=[-1, 1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64') label = fluid.data(name='label', shape=[-1, 1], dtype='int64')
if nn_type == 'softmax_regression': if nn_type == 'softmax_regression':
net_conf = softmax_regression net_conf = softmax_regression
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册