用slim定义Lenet网络,并训练测试MNIST
1.要求
1.将Lenet单独定义到Lenet.py文件
可以定义为一个函数,例如:
def lenet(images):
2.用with slim.arg_scope …: 去管理 lenet中所有操作的
默认参数,例如activation_fn, weights_initializer,等。
3.编写mnist_train.py脚本,训练slim定义的lenet做MNIST字
符分类。
2.网络定义
(1)网络定义
定义网络lenet.
def lenet(images):
with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu,
weights_initializer=tf.truncated_normal_initializer(.0, .1),
weights_regularizer=slim.l2_regularizer(0.0005),
biases_initializer=tf.truncated_normal_initializer(.0, .1)):
net = slim.conv2d(images, 6, [5, 5], [1, 1], padding=‘VALID’, scope=‘conv1’)
net = slim.max_pool2d(net, [2, 2], [2, 2], padding=‘VALID’, scope=‘pool1’)
net = slim.conv2d(net, 16, [5, 5], [1, 1], padding=‘VALID’, scope=‘conv2’)
net = slim.max_pool2d(net, [2, 2], [2, 2], padding=‘VALID’, scope=‘pool2’)
net = slim.flatten(net, scope=‘flatten1’)
net = slim.stack(net, slim.fully_connected, [120, 84], scope=‘fc1’)
net = slim.fully_connected(net, 10, activation_fn=tf.nn.softmax, scope=‘fc2’)
return net