手记

机器学习/深度学习入门:搭建神经网络流程

搭建神经网络流程:

1.加载训练数据,并预处理(对于图像等数据,可以直接转化为矩阵,或者通过tf.convert_to_tensor()将其转换为tensor数据类型处理);

2.构建网络层,如conv,pool,relu,lrn,fc等,在此处需要设置相应层的权重和偏置;

比较喜欢的两种定义方式如下(以定义AlexNet为例):

import tensorflow as tf

BATCH_SIZE = 200def bias(name, shape, bias_start=0.0, trainable=True):
    dtype = tf.float32
    var = tf.get_variable(name, shape, tf.float32, trainable=trainable,                          initializer=tf.constant_initializer(
                              bias_start, dtype=dtype))    return vardef weight(name, shape, stddev=0.02, trainable=True):
    dtype = tf.float32
    var = tf.get_variable(name, shape, tf.float32, trainable=trainable,                          initializer=tf.random_normal_initializer(                              stddev=stddev, dtype=dtype))    return vardef fully_connected(value, output_shape, name='fully_connected', with_w=False):
    value = tf.reshape(value, [BATCH_SIZE, -1])
    shape = value.get_shape().as_list()    with tf.variable_scope(name):
        weights = weight('weights', [shape[1], output_shape], 0.02)
        biases = bias('biases', [output_shape], 0.0)    if with_w:        return tf.matmul(value, weights) + biases, weights, biases    else:        return tf.matmul(value, weights) + biasesdef relu(value, name='relu'):    with tf.variable_scope(name):        return tf.nn.relu(value)def conv2d(value, output_dim, k_h=5, k_w=5,           strides=[1, 1, 1, 1], name='conv2d'):    with tf.variable_scope(name):
        weights = weight('weights',                         [k_h, k_w, value.get_shape()[-1], output_dim])
        conv = tf.nn.conv2d(value, weights, strides=strides, padding='SAME')
        biases = bias('biases', [output_dim])
        conv = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())        return convdef pool(value, k_size=[1, 3, 3, 1],strides=[1, 2, 2, 1], name='pool1'):    with tf.variable_scope(name):
        pool = tf.nn.max_pool(value, ksize=k_size, strides=strides, padding='VALID')        return pooldef pool_avg(value, k_size=[1, 3, 3, 1],strides=[1, 2, 2, 1], name='pool1'):    with tf.variable_scope(name):
        pool = tf.nn.avg_pool(value, ksize=k_size, strides=strides, padding='VALID')        return pooldef lrn(value, depth_radius=1, alpha=5e-05, beta=0.75, name='lrn1'):    with tf.variable_scope(name):
        norm1 = tf.nn.lrn(value, depth_radius=depth_radius, bias=1.0, alpha=alpha, beta=beta)        return norm1def discriminator(image, hashing_bits, reuse=False, name='discriminator'):    with tf.name_scope(name):        if reuse:
            tf.get_variable_scope().reuse_variables()
        conv1 = conv2d(image, output_dim=32, name='d_conv1')
        relu1 = relu(pool(conv1, name='d_pool1'), name='d_relu1')
        conv2 = conv2d(lrn(relu1, name='d_lrn1'), output_dim=32, name='d_conv2')
        relu2 = relu(pool_avg(conv2, name='d_pool2'), name='d_relu2')
        conv3 = conv2d(lrn(relu2, name='d_lrn2'), output_dim=64, name='d_conv3')
        pool3 = pool_avg(relu(conv3, name='d_relu3'), name='d_pool3')
        relu_ip1 = relu(fully_connected(pool3, output_shape=500, name='d_ip1'), name='d_relu4')
        ip2 = fully_connected(relu_ip1, output_shape=hashing_bits, name='d_ip2')        return ip2

import tensorflow as tfimport numpy as npclass AlexNet(object):    """Implementation of the AlexNet."""    def __init__(self, x, keep_prob, num_classes, skip_layer,                 weights_path='DEFAULT'):        # Parse input arguments into class variables        self.X = x        self.NUM_CLASSES = num_classes        self.KEEP_PROB = keep_prob        self.SKIP_LAYER = skip_layer        if weights_path == 'DEFAULT':            self.WEIGHTS_PATH = 'bvlc_alexnet.npy'        else:            self.WEIGHTS_PATH = weights_path        # Call the create function to build the computational graph of AlexNet        self.create()    def create(self):        """Create the network graph."""        # 1st Layer: Conv (w ReLu) -> Lrn -> Pool        conv1 = conv(self.X, 11, 11, 96, 4, 4, padding='VALID', name='conv1')
        norm1 = lrn(conv1, 2, 2e-05, 0.75, name='norm1')
        pool1 = max_pool(norm1, 3, 3, 2, 2, padding='VALID', name='pool1')        
        # 2nd Layer: Conv (w ReLu)  -> Lrn -> Pool with 2 groups        conv2 = conv(pool1, 5, 5, 256, 1, 1, groups=2, name='conv2')
        norm2 = lrn(conv2, 2, 2e-05, 0.75, name='norm2')
        pool2 = max_pool(norm2, 3, 3, 2, 2, padding='VALID', name='pool2')        
        # 3rd Layer: Conv (w ReLu)        conv3 = conv(pool2, 3, 3, 384, 1, 1, name='conv3')        # 4th Layer: Conv (w ReLu) splitted into two groups        conv4 = conv(conv3, 3, 3, 384, 1, 1, groups=2, name='conv4')        # 5th Layer: Conv (w ReLu) -> Pool splitted into two groups        conv5 = conv(conv4, 3, 3, 256, 1, 1, groups=2, name='conv5')
        pool5 = max_pool(conv5, 3, 3, 2, 2, padding='VALID', name='pool5')        # 6th Layer: Flatten -> FC (w ReLu) -> Dropout        flattened = tf.reshape(pool5, [-1, 6*6*256])
        fc6 = fc(flattened, 6*6*256, 4096, name='fc6')
        dropout6 = dropout(fc6, self.KEEP_PROB)        # 7th Layer: FC (w ReLu) -> Dropout        fc7 = fc(dropout6, 4096, 4096, name='fc7')        self.dropout7 = dropout(fc7, self.KEEP_PROB)        #H layer:sigmoid        H = get_H(self.dropout7,4096,128,name='H')        # 8th Layer: FC and return unscaled activations        self.fc8 = fc(self.dropout7, 4096, self.NUM_CLASSES, relu=False, name='fc8')    def load_initial_weights(self, session):        """Load weights from file into network."""        # Load the weights into memory        weights_dict = np.load(self.WEIGHTS_PATH, encoding='bytes').item()        # Loop over all layer names stored in the weights dict        for op_name in weights_dict:            # Check if layer should be trained from scratch            if op_name not in self.SKIP_LAYER:                with tf.variable_scope(op_name, reuse=True):                    # Assign weights/biases to their corresponding tf variable                    for data in weights_dict[op_name]:                        # Biases                        if len(data.shape) == 1:
                            var = tf.get_variable('biases', trainable=False)
                            session.run(var.assign(data))                        # Weights                        else:
                            var = tf.get_variable('weights', trainable=False)
                            session.run(var.assign(data))def conv(x, filter_height, filter_width, num_filters, stride_y, stride_x, name,         padding='SAME', groups=1):    """Create a convolution layer."""    # Get number of input channels    input_channels = int(x.get_shape()[-1])    # Create lambda function for the convolution    convolve = lambda i, k: tf.nn.conv2d(i, k,                                         strides=[1, stride_y, stride_x, 1],                                         padding=padding)    with tf.variable_scope(name) as scope:        # Create tf variables for the weights and biases of the conv layer        weights = tf.get_variable('weights', shape=[filter_height,                                                    filter_width,                                                    input_channels/groups,                                                    num_filters])
        biases = tf.get_variable('biases', shape=[num_filters])    if groups == 1:
        conv = convolve(x, weights)    # In the cases of multiple groups, split inputs & weights and    else:        # Split input and weights and convolve them separately        input_groups = tf.split(axis=3, num_or_size_splits=groups, value=x)
        weight_groups = tf.split(axis=3, num_or_size_splits=groups,                                 value=weights)
        output_groups = [convolve(i, k) for i, k in zip(input_groups, weight_groups)]        # Concat the convolved output together again        conv = tf.concat(axis=3, values=output_groups)    # Add biases    bias = tf.reshape(tf.nn.bias_add(conv, biases), tf.shape(conv))    # Apply relu function    relu = tf.nn.relu(bias, name=scope.name)    return reludef fc(x, num_in, num_out, name, relu=True):    """Create a fully connected layer."""    with tf.variable_scope(name) as scope:        if relu:            # Create tf variables for the weights and biases            weights = tf.get_variable('weights', shape=[num_in, num_out],                                      trainable=True)
            biases = tf.get_variable('biases', [num_out], trainable=True)            # Matrix multiply weights and inputs and add bias        else:
            weights = tf.get_variable('weights', shape=[num_in, num_out],                                      initializer=tf.truncated_normal_initializer(stddev=0.005), trainable=True)

            biases = tf.get_variable('biases', shape=[num_out],                                     initializer=tf.constant_initializer(1.0), trainable=True)
        act = tf.nn.xw_plus_b(x, weights, biases, name=scope.name)    if relu:        # Apply ReLu non linearity        relu = tf.nn.relu(act)        return relu    else:        return actdef get_H(x,num_in, num_out,name):    with tf.variable_scope(name) as scope:
        weightd=tf.get_variable('weights',shape=[num_in, num_out],                                initializer=tf.truncated_normal_initializer(stddev=0.005),trainable=True)

        biases=tf.get_variable('biases',shape=[num_out],                               initializer=tf.constant_initializer(1.0),trainable=True)

        act=tf.nn.xw_plus_b(x,weightd,biases,name=scope.name)    return tf.nn.sigmoid(act)def max_pool(x, filter_height, filter_width, stride_y, stride_x, name,             padding='SAME'):    """Create a max pooling layer."""    return tf.nn.max_pool(x, ksize=[1, filter_height, filter_width, 1],                          strides=[1, stride_y, stride_x, 1],                          padding=padding, name=name)def lrn(x, radius, alpha, beta, name, bias=1.0):    """Create a local response normalization layer."""    return tf.nn.local_response_normalization(x, depth_radius=radius,                                              alpha=alpha, beta=beta,                                              bias=bias, name=name)def dropout(x, keep_prob):    """Create a dropout layer."""    return tf.nn.dropout(x, keep_prob)

3.定义节点,准备接收数据:

x = tf.placeholder(tf.float32, [batch_size, 227, 227, 3])
y = tf.placeholder(tf.float32, [batch_size, num_classes])
keep_prob = tf.placeholder(tf.float32)

4.定义网络层;

5.定义损失函数;

6.选择optimizer是的损失函数最小;

7.初始化所有变量,通过sess.run(optimizer)来迭代学习。

在整个过程中可以设置CPU/GPU,模型持久化和可视化等。

原文出处


0人推荐
随时随地看视频
慕课网APP