主頁 >  其他 > 詳細:tensorflow構建神經網路基礎概念和梳理

詳細:tensorflow構建神經網路基礎概念和梳理

2021-10-21 07:05:24 其他

1#: placeholder
x = tf.compat.v1.placeholder(tf.float32, shape=[None, w, h, c], name='x')
y_ = tf.compat.v1.placeholder(tf.int32, shape=[None, ], name='y_')

placeholder函式定義如下:

tf.placeholder(dtype, shape=None, name=None),placeholder是占位符,在tensorflow中類似于函式引數,運行時必須傳入值,

  • dtype:資料型別,常用的是tf.float32,tf.float64等數值型別,

  • shape:資料形狀,默認是None,就是一維值,也可以是多維,比如[2,3], [None, 3]表示列是3,行不定, 此引數可以根據提供的資料推導得到,不一定要給出,,

  • name:名稱, 比如常在上邊的x, y_,

  • 比如計算3*4=12
    
    import tensorflow as tf
    import numpy as np
    input1 = tf.placeholder(tf.float32)
    input2 = tf.placeholder(tf.float32)
    output = tf.multiply(input1, input2)
    with tf.Session() as sess:
        print sess.run(output, feed_dict = {input1:[3.], input2: [4.]})
    計算矩陣相乘x*y
    
    import tensorflow as tf
    import numpy as np
    x = tf.placeholder(tf.float32, shape=(1024, 1024))
    y = tf.matmul(x, x)
    with tf.Session() as sess:
    #  print(sess.run(y))  # ERROR: x is none now
      rand_array = np.random.rand(1024, 1024)
      print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.
    使用庫函式進行矩陣運算
    
    import tensorflow as tf
    # 定義placeholder
    input1 = tf.placeholder(tf.float32,shape=(1, 2),name="input-1")
    input2 = tf.placeholder(tf.float32,shape=(2, 1),name="input-2")
    # 定義矩陣乘法運算(注意區分matmul和multiply的區別:matmul是矩陣乘法,multiply是點乘)
    output = tf.matmul(input1, input2)
    # 通過session執行乘法運行
    with tf.Session() as sess:
        # 執行時要傳入placeholder的值
        print sess.run(output, feed_dict = {input1:[1,2], input2:[3,4]})
        # 最終執行結果 [11]

    2#:卷積和池化

  • 卷積層
    
    tf.nn.conv2d(input, filter, strides=, padding=, name=None)
    計算給定4-D input和filter張量的2維卷積
    	* 
    input:給定的輸入張量,具有[batch, heigth, width, channel],型別為float32, 64
    	* 
    filter:指定過濾器的大小,[filter_height, filter_width, in_channels, out_channels]. out_channels:視窗數量
    	* 
    strides:strides = [1, stride, stride, 1],步長
    	* 
    padding:“SAME”, “VALID”,使用的填充演算法的型別,使用“SAME”,其中”VALID”表示滑動超出部分舍棄,“SAME”表示填充,使得變化后height, width一樣大
    
    
    新的激活函式-Reluf(x) = max(0, x)
    tf.nn.relu(features, name=None)
    features: 卷積后加上偏置的結果
    return: 結果
    	1. 
    采用sigmoid等函式,反向傳播求誤差梯度時,計算量相對大,而采用Relu激活函式,整個程序的計算量節省很多
    
    
    	1. 
    對于深層網路,sigmoid函式反向傳播時,很容易就會出現梯度消失的情況(求不出權重和偏置)
    
    

    池化層(Pooling)計算

  • Pooling層主要的作用是特征提取,通過去掉Feature Map中不重要的樣本,(這里如何確定什么引數不重要是個很難的問題,哪些樣本不重要,這個是很不好判斷的,)進一步減少引數數量,Pooling的方法很多,最常用的是Max Pooling,
    
    
    tf.nn.max_pool(value, ksize=, strides=, padding=,name=None)
    
    輸入上執行最大池數
    	* 
    value: 4-D Tensor形狀[batch, height, width, channels]
    	* 
    ksize: 池化視窗大小,[1, ksize, ksize, 1]
    	* 
    strides:步長大小,[1, strides, strides, 1]
    	* 
    padding: “SAME”, “VALID”,使用的填充演算法的型別,使用“SAME”
    
    

    Full Connected層(全連接層)

  • 前面的卷積和池化相當于做特征工程,后面的全連接相當于做特征加權,最后的全連接層在整個卷積神經網路中起到“分類器”的作用,

    函式的作用是將tensor變換為引數shape的形式,

    其中shape為一個串列形式,特殊的一點是串列中可以存在-1,-1代表的含義是不用我們自己指定這一維的大小,函式會自動計算,但串列中只能存在一個-1,(當然如果存在多個-1,就是一個存在多解的方程了)

  • import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    
    # 輸入層# 準備占位符with tf.variable_scope('data'):
        x = tf.placeholder(tf.float32, [None, 784])
        y_true = tf.placeholder(tf.float32, [None, 10])
    
    # 卷積層# 卷積1with tf.variable_scope('conv1'):
        # 初始化權重 視窗3*3  步長1  32個視窗
        weight1 = tf.Variable(tf.random_normal(shape=[3, 3, 1, 32]))
        bias1 = tf.Variable(tf.constant(1.0, shape=[32]))
        x_reshaped = tf.reshape(x, [-1, 28, 28, 1])
        # x [None, 28, 28, 1] ----> [None, 28, 28, 32]
        conved1 = tf.nn.conv2d(input=x_reshaped, filter=weight1, strides=[1, 1, 1, 1], padding='SAME')
        print(conved1)
        relu1 = tf.nn.relu(conved1) + bias1
        print(relu1)# 池化1with tf.variable_scope('pool'):
        # 視窗2*2  步長2     x [None, 28, 28, 32] ----> [None, 14, 14, 32]
        pool1 = tf.nn.max_pool(value=relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
        print(pool1)
    
    # 卷積2with tf.variable_scope('conv2'):
        # 視窗3*3  步長1   64個視窗
        weight2 = tf.Variable(tf.random_normal([3, 3, 32, 64]))
        bias2 = tf.Variable(tf.constant(1.0, shape=[64]))
        # x [None, 14, 14, 32] ----> [None, 14, 14, 64]
        conved2 = tf.nn.conv2d(input=pool1, filter=weight2, strides=[1, 1, 1, 1], padding='SAME')
        print(conved2)
        relu2 = tf.nn.relu(conved2) + bias2
    
    # 池化2with tf.variable_scope('pool2'):
        # 視窗2*2  步長2     x [None, 14, 14, 64] ----> [None, 7, 7, 64]
        pool2 = tf.nn.max_pool(value=relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    
    # 全連接層  x[None, 7, 7, 64] -> x[None, 7*7*64] * ([7*7*64, 10]) ----> y[None, 10]with tf.variable_scope('full_coon'):
        x_fc = tf.reshape(pool2, [-1, 7*7*64])
        weight_fc = tf.Variable(tf.random_normal([7*7*64, 10]))
        bias_fc = tf.Variable(tf.constant(1.0, shape=[10]))
        y_predict = tf.matmul(x_fc, weight_fc) + bias_fc
    
    # 交叉熵損失函式with tf.variable_scope('loss'):
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
        train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
    
    # 計算準確度with tf.variable_scope('acc'):
        equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))
        accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))
    
    init_var = tf.global_variables_initializer()
    
    with tf.Session() as sess:
        sess.run(init_var)
        mnist = input_data.read_data_sets('./input_data', one_hot=True)
        for i in range(1000):
            image, label = mnist.train.next_batch(100)
            sess.run(train_op, feed_dict={x: image, y_true: label})
            print('第%s步,準確率為: %s' % (i, sess.run(accuracy, feed_dict={x: image, y_true: label})))
    一個模型例子:::
    
    def model(input_tensor, train,
              regularizer):  
        with tf.variable_scope('layer1-conv1'):  # 定義一個作用域:layer1-conv1,在該作用域下面可以定義相同名稱的變數(用于變數)
            conv1_weights = tf.get_variable("weight", [5, 5, 3, 32],
                                            initializer=tf.truncated_normal_initializer(stddev=0.1))
            # 定義變數權重:weight,名稱是weight;5,5代表卷積核的大小,3代表輸入的信道數目,32代表輸出的信道數目;initializer代表神經網路權重和卷積核的推薦初始值,生成截斷正態分布亂數,服從標準差為0.1
            conv1_biases = tf.get_variable("bias", [32], initializer=tf.constant_initializer(0.0))
            # 定義變數偏置:bias,名稱bias,[32]代表當前層的深度;initializer代表偏置的初始化,用函式tf.constant_initializer將其初始化為0,也可以初始化為tf.zeros_initializer或者tf.ones_initializer
            conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
            # 上面為定義卷積層:input_tensor為當前層的節點矩陣;conv1_weights代表卷積層的權重;strides為不同方向上面的步長;padding標識填充,有兩種方式,SAME表示用0填充,“VALID”表示不填充,
            relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))  # 定義激活函式:利用bias_add給每個節點都加上偏置項,然后利用relu函式去線性化
        with tf.name_scope("layer2-pool1"):  # 定義一個:layer2-pool1(用于op)
            # 池化層可以優先縮小矩陣的尺寸,從而減小最后全連接層當中的引數;池化層既可以加快計算速度,也可以防止過擬合,
            pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")
            # ksize代表pool視窗的尺寸,首尾兩個數必須是1,ksize最常用[1,2,2,1]和[1,3,3,1];strides代表filter的步長,首尾兩個數必須為1;padding代表填充方式;
        with tf.variable_scope("layer3-conv2"):  # 定義作用域(用于變數)
            # 定義權重
            conv2_weights = tf.get_variable("weight", [5, 5, 32, 64],
                                            initializer=tf.truncated_normal_initializer(stddev=0.1))
            conv2_biases = tf.get_variable("bias", [64], initializer=tf.constant_initializer(0.0))  # 定義偏置
            conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')  # 定義卷積層
            relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))  # 定義激活函式
        with tf.name_scope("layer4-pool2"):  # 定義命名空間(用于op)
            pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')  # 定義池化層
        with tf.variable_scope("layer5-conv3"):  # 定義作用域 (用于變數)
            # 定義權重
            conv3_weights = tf.get_variable("weight", [3, 3, 64, 128],
                                            initializer=tf.truncated_normal_initializer(stddev=0.1))
            conv3_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0))  # 定義偏置
            conv3 = tf.nn.conv2d(pool2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME')  # 定義卷積層
            relu3 = tf.nn.relu(tf.nn.bias_add(conv3, conv3_biases))  # 定義激活函式
        with tf.name_scope("layer6-pool3"):  # 定義命名空間(用于op)
            pool3 = tf.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')  # 定義池化層
        with tf.variable_scope("layer7-conv4"):  # 定義作用域(用于變數)
            # 定義權重
            conv4_weights = tf.get_variable("weight", [3, 3, 128, 128],
                                            initializer=tf.truncated_normal_initializer(stddev=0.1))
            conv4_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0))  # 定義偏置
            conv4 = tf.nn.conv2d(pool3, conv4_weights, strides=[1, 1, 1, 1], padding='SAME')  # 定義卷積層
            relu4 = tf.nn.relu(tf.nn.bias_add(conv4, conv4_biases))  # 定義激活函式
        with tf.name_scope("layer8-pool4"):  # 定義命名空間(用于op)
            pool4 = tf.nn.max_pool(relu4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')  # 定義池化層
          
            nodes = 6 * 6 * 128  
            reshaped = tf.reshape(pool4, [-1, nodes])
            print("shape of reshaped:", reshaped.shape)  # reshape函式將pool4的輸出轉化成向量
        # 定義作用域:
        with tf.variable_scope('layer9-fc1'):
            # 定義全連接層的權重:
            fc1_weights = tf.get_variable("weight", [nodes, 1024], initializer=tf.truncated_normal_initializer(stddev=0.1))
            if regularizer != None: tf.add_to_collection('losses', regularizer(fc1_weights))
            # 給全連接層的權重添加正則項,tf.add_to_collection函式可以把變數放入一個集合,把很多變數變成一個串列
            fc1_biases = tf.get_variable("bias", [1024], initializer=tf.constant_initializer(0.1))  # 定義全連接層的偏置:
            fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases)  # 定義激活函式:
            if train: fc1 = tf.nn.dropout(fc1, 0.5)  # 針對訓練資料,在全連接層添加dropout層,防止過擬合
        with tf.variable_scope('layer10-fc2'):
            fc2_weights = tf.get_variable("weight", [1024, 512], initializer=tf.truncated_normal_initializer(stddev=0.1))
            if regularizer != None: tf.add_to_collection('losses', regularizer(fc2_weights))
            fc2_biases = tf.get_variable("bias", [512], initializer=tf.constant_initializer(0.1))
            fc2 = tf.nn.relu(tf.matmul(fc1, fc2_weights) + fc2_biases)
            if train: fc2 = tf.nn.dropout(fc2, 0.5)
        with tf.variable_scope('layer11-fc3'):
            fc3_weights = tf.get_variable("weight", [512, 5],
                                          initializer=tf.truncated_normal_initializer(stddev=0.1))
            if regularizer != None: tf.add_to_collection('losses', regularizer(fc3_weights))
            fc3_biases = tf.get_variable("bias", [5], initializer=tf.constant_initializer(0.1))
            logit = tf.matmul(fc2, fc3_weights) + fc3_biases
        return logit
    def inference(input_tensor, train, regularizer):
        with tf.compat.v1.variable_scope('layer1-conv1'):
            # 定義變數權重:weight,名稱是weight;5,5代表卷積核的大小,3代表輸入的信道數目,32代表輸出的信道數目;
            # initializer代表神經網路權重和卷積核的推薦初始值,生成截斷正態分布亂數,服從標準差為0.1
            conv1_weights = tf.compat.v1.get_variable("weight", [5, 5, 3, 32],
                                                      initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1))
            # 初始化偏置值為0
            conv1_biases = tf.compat.v1.get_variable("bias", [32], initializer=tf.constant_initializer(0.0))
            conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
            relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
    
    
        with tf.name_scope("layer2-pool1"):
            pool1 = tf.compat.v1.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")
            logging.info(f"First convolutional layer:{pool1}")
    
    
        with tf.compat.v1.variable_scope("layer3-conv2"):
            conv2_weights = tf.compat.v1.get_variable("weight", [5, 5, 32, 64],
                                                      initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1))
            conv2_biases = tf.compat.v1.get_variable("bias", [64], initializer=tf.compat.v1.constant_initializer(0.0))
            conv2 = tf.compat.v1.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
            relu2 = tf.compat.v1.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
    
    
        with tf.name_scope("layer4-pool2"):
            pool2 = tf.compat.v1.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
            logging.info(f"Second convolutional layer:{pool2}")
    
    
        with tf.compat.v1.variable_scope("layer5-conv3"):
            conv3_weights = tf.compat.v1.get_variable("weight", [3, 3, 64, 128],
                                                      initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1))
            conv3_biases = tf.compat.v1.get_variable("bias", [128], initializer=tf.compat.v1.constant_initializer(0.0))
            conv3 = tf.compat.v1.nn.conv2d(pool2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME')
            relu3 = tf.compat.v1.nn.relu(tf.nn.bias_add(conv3, conv3_biases))
    
    
        with tf.compat.v1.name_scope("layer6-pool3"):
            pool3 = tf.compat.v1.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
            logging.info(f"Third convolutional layer:{pool3}")
    
    
        with tf.compat.v1.variable_scope("layer7-conv4"):
            conv4_weights = tf.compat.v1.get_variable("weight", [3, 3, 128, 128],
                                                      initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1))
            conv4_biases = tf.compat.v1.get_variable("bias", [128], initializer=tf.compat.v1.constant_initializer(0.0))
            conv4 = tf.compat.v1.nn.conv2d(pool3, conv4_weights, strides=[1, 1, 1, 1], padding='SAME')
            relu4 = tf.compat.v1.nn.relu(tf.nn.bias_add(conv4, conv4_biases))
    
    
        with tf.compat.v1.name_scope("layer8-pool4"):
            pool4 = tf.compat.v1.nn.max_pool(relu4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
            logging.info(f"The fourth convolutional layer:{pool4}")
            nodes = 6 * 6 * 128
            # 展開
            reshaped = tf.compat.v1.reshape(pool4, [-1, nodes])
    
    
        with tf.compat.v1.variable_scope('layer9-fc1'):
            fc1_weights = tf.compat.v1.get_variable("weight", [nodes, 1024],
                                                    initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1))
            if regularizer != None: tf.compat.v1.add_to_collection('losses', regularizer * tf.nn.l2_loss(fc1_weights))
            fc1_biases = tf.compat.v1.get_variable("bias", [1024], initializer=tf.compat.v1.constant_initializer(0.1))
    
    
            fc1 = tf.compat.v1.nn.relu(tf.compat.v1.matmul(reshaped, fc1_weights) + fc1_biases)
            logging.info(f"The first fully connected layer:{fc1}")
            if train: fc1 = tf.compat.v1.nn.dropout(fc1, 0.5)
    
    
        with tf.compat.v1.variable_scope('layer10-fc2'):
            fc2_weights = tf.compat.v1.get_variable("weight", [1024, 512],
                                                    initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1))
            if regularizer != None: tf.compat.v1.add_to_collection('losses', regularizer * tf.nn.l2_loss(fc2_weights))
            fc2_biases = tf.compat.v1.get_variable("bias", [512], initializer=tf.compat.v1.constant_initializer(0.1))
    
    
            fc2 = tf.compat.v1.nn.relu(tf.matmul(fc1, fc2_weights) + fc2_biases)
            if train: fc2 = tf.compat.v1.nn.dropout(fc2, 0.5)
            logging.info(f"The second fully connected layer:{fc2}")
    
    
        with tf.compat.v1.variable_scope('layer11-fc3'):
            fc3_weights = tf.compat.v1.get_variable("weight", [512, 5],
                                                    initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.1))
            if regularizer != None: tf.compat.v1.add_to_collection('losses', regularizer * tf.nn.l2_loss(fc3_weights))
            fc3_biases = tf.compat.v1.get_variable("bias", [5], initializer=tf.compat.v1.constant_initializer(0.1))
            logit = tf.compat.v1.matmul(fc2, fc3_weights) + fc3_biases
        return logit

    conv1 = tf.nn.conv2d(input_tensor,conv1_weights,strides=[1,1,1,1],padding='SAME')

    這是一個常見的卷積操作,其中strides=【1,1,1,1】表示滑動步長為1,padding=‘SAME’表示填0操作

    當我們要設定步長為2時,strides=【1,2,2,1】,很多同學可能不理解了,這四個引數分別代表了什么,

    strides在官方定義中是一個一維具有四個元素的張量,其規定前后必須為1,所以我們可以改的是中間兩個數,中間兩個數分別代表了水平滑動和垂直滑動步長值,于是就很好理解了,在卷積核移動逐漸掃描整體圖時候,因為步長的設定問題,可能導致剩下未掃描的空間不足以提供給卷積核的,大小掃描 比如有圖大小為5*5,卷積核為2*2,步長為2,卷積核掃描了兩次后,剩下一個元素,不夠卷積核掃描了,這個時候就在后面補零,補完后滿足卷積核的掃描,這種方式就是same,如果說把剛才不足以掃描的元素位置拋棄掉,就是valid方式,

  • 函式引數的解釋:

    tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)

    除去name引數用以指定該操作的name,與方法有關的一共五個引數:

    input:

    指需要做卷積的輸入影像,它要求是一個Tensor,具有[batch, in_height, in_width, in_channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 影像通道數],注意這是一個4維的Tensor,要求型別為float32和float64其中之一

    filter:

    相當于CNN中的卷積核,它要求是一個Tensor,具有[filter_height, filter_width, in_channels, out_channels]這樣的shape,具體含義是[卷積核的高度,卷積核的寬度,影像通道數,卷積核個數],要求型別與引數input相同,有一個地方需要注意,第三維in_channels,就是引數input的第四維,【有時候也叫ksize或卷積核】

    * strides:卷積時在影像每一維的步長,這是一個一維的向量,長度4,【注意:一般兩邊為1.形如[1,height,weight,1]】,

    padding:

    string型別的量,只能是”SAME”,”VALID”其中之一,這個值決定了不同的卷積方式【same:不夠還加,valid:不夠舍棄,】

    use_cudnn_on_gpu:

    bool型別,是否使用cudnn加速,默認為true

    batch和Eposh

    神經網路中Batch和Epoch之間的區別是什么? 隨機梯度下降法是一種具有大量超引數的學習演算法,兩個超引數: Batch大小和Epoch數量,它們都是整數值,batch字面上是批量的意思,在深度學習中指的是計算一次cost需要的輸入資料個數, 這意味著資料集將分為40個Batch,每個Batch有5個樣本,每批五個樣品后,模型權重將更新, 這也意味著一個epoch將涉及40個Batch或40個模型更新, 有1000個Epoch,模型將暴露或傳遞整個資料集1,000次, 一個 batch 的樣本通常比單個輸入更接近于總體輸入資料的分布,batch 越大就越近似,然而,每個 batch 將花費更長的時間來處理,并且仍然只更新模型一次,

  • Sample: 樣本,資料集中的一個元素,一條資料,

    例1: 在卷積神經網路中,一張影像是一個樣本,

    例2: 在語音識別模型中,一段音頻是一個樣本,

  • Batch: 批,含有 N 個樣本的集合,每一個 batch 的樣本都是獨立并行處理的,在訓練時,一個 batch 的結果只會用來更新一次模型,

    一個 batch 的樣本通常比單個輸入更接近于總體輸入資料的分布,batch 越大就越近似,然而,每個 batch 將花費更長的時間來處理,并且仍然只更新模型一次,在推理(評估/預測)時,建議條件允許的情況下選擇一個盡可能大的 batch,(因為較大的 batch 通常評估/預測的速度會更快),

  • Epoch: 輪次,通常被定義為 「在整個資料集上的一輪迭代」,用于訓練的不同的階段,這有利于記錄和定期評估,

    當在 Keras 模型的 fit 方法中使用 validation_data 或 validation_split 時,評估將在每個 epoch 結束時運行,

    在 Keras 中,可以添加專門的用于在 epoch 結束時運行的 callbacks 回呼,例如學習率變化和模型檢查點(保存),

  • 這也意味著一個epoch將涉及40個Batch或40個模型更新,

    有1000個Epoch,模型將暴露或傳遞整個資料集1,000次,在整個培訓程序中,總共有40,000Batch,

    用一個小例子來說明這一點,

    假設您有一個包含200個樣本(資料行)的資料集,并且您選擇的Batch大小為5和1,000個Epoch,

    這意味著資料集將分為40個Batch,每個Batch有5個樣本,每批五個樣品后,模型權重將更新,

  • 池化

    池化程序在一般卷積程序后,池化(pooling) 的本質,其實就是采樣,Pooling 對于輸入的 Feature Map,選擇某種方式對其進行降維壓縮,以加快運算速度,

  • 池化的作用:

    (1)保留主要特征的同時減少引數和計算量,防止過擬合,

    (2)invariance(不變性),這種不變性包括translation(平移),rotation(旋轉),scale(尺度),

    Pooling 層說到底還是一個特征選擇,資訊過濾的程序,也就是說我們損失了一部分資訊,這是一個和計算性能的一個妥協,隨著運算速度的不斷提高,我認為這個妥協會越來越小,

    現在有些網路都開始少用或者不用pooling層了,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/327858.html

標籤:AI

上一篇:python機器學習《機器學習Python實踐》整理,sklearn庫應用詳解

下一篇:Python 計算機視覺(五)—— OpenCV 進行影像幾何變換

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more