길쌈 신경망 식별 데이터

코드 구현 :

오기 AS tensorflow의 TF
 에서 tensorflow.examples.tutorials.mnist 오기 Input_Data
 오기 OS의 

os.environ [ ' TF_CPP_MIN_LOG_LEVEL ' = 된 ' 2 '의 

#을 초기화하는 가중 함수 정의 
DEF의 weight_varibles을 (형상) : 
    W = tf.Variable을 (tf.random_normal (= 형상, 평균 = 0.0, STDDEV = 1.0 형상 ))
     W 

#를 오프셋 초기화 함수 정의 
데프 : bias_varibles (형상) 
    B = tf.Variable (tf.constant (0.0 모양 = ) 형상)
     반환 B 

DEF를 모델 () :
     "" "
    컨벌루션 이후 모델 정의 
    : 창 
    "" " 
    # 1 [없음, 784 1. 제조 데이터 y_true [없음 10] X가없는 자리 표시 
    (tf.variable_scope으로 " 데이터 " :) 
        X (= TF를 tf.placeholder .float32 [없음, 784 ]) 
        y_true = tf.placeholder (tf.int32 [없음, 10 ]) 

    #을 2 회선을 5,5,1,32 층은 컨벌루션 세포 활성화 tf.nn. 활성화 relu 모으고 
    tf.variable_scope (함께 " CONV1 " ) :
         # 임의로 초기화 가중치 
        w_conv1 weight_varibles = ([5,5,1,32 ]) 

        #가 무작위 오프셋 생성 
        b_conv1 bias_varibles에게 = ([32 ]) 

        중 #X [없음, (784)]의 형상 변화는 어레이 [없음, 28,28,1] 생각으로 변경 
        - x_reshape tf.reshape = ([1,28,28,1 X, ]) 

        x_relu = tf.nn.relu (tf.nn.conv2d (x_reshape, w_conv1, 걷기 = [1,1,1,1], 패딩 = " SAME " ) + b_conv1) 

        #의 풀링 2 * 2 strides2 [없음, 28,28,32 ] - > [없음, 14,14,32] 
        x_pool1 = tf.nn.max_pool (x_relu, ksize = [1,2,2,1], 당뇨병 = [1,2,2,1], 패딩 = " SAME " ) 

    # 3 계층 컨벌루션 
    tf.variable_scope (함께 " CONV2 " ) :
         # 임의로 초기화 중량 중량 [5,5,32,64 오프셋 [64] 
        w_conv2 weight_varibles = ([5,5,32,64  ])
        b_conv2 = bias_varibles ([64 ]) 

        #의 컨벌루션 활성화 풀링 
        #의 컨벌루션 [없음, 14,14,32] -----> [없음, 14,14,64] 
        x_relu2 = tf.nn .relu (tf.nn.conv2d (x_pool1, w_conv2, 걷기 = [1,1,1,1], 패딩 = " SAME " ) + b_conv2) 

        #의 풀링 걸음 2 * 2 2 [없음 , 14,14, 64] ---> [없음, 7,7,64] 
        x_pool2 = tf.nn.max_pool (x_relu2, ksize = [1,2,2,1], 당뇨병은 = [1,2,2,1], 패딩 = " SAME " ) 

    # . 4 완전 연결 층 [없음, 7,7,64] ---> [ 없음 7 * 7 * 64] * 7 * 7 * 64,10] + [10] = 없음, 10] 
    tf.variable_scope (로 "" ) :
         # 임의로 초기화 가중치 및 바이어스 
        (. [제 7 * 64 = 10 weight_varibles w_fc ]) 
        b_fc = bias_varibles ([10 ]) 

        #는 형상을 변경 [없음, 7,7,64] ---> [없음 ,. 제 7 * 64] 
        x_fc_reshape = tf.reshape - (x_pool2 [L은 7 * 64 7 ]) 

        # 1 (10) 각각의 샘플에 대해 얻어진 행렬 계산 결과를 행한다 
        y_predict = tf.matmul (x_fc_reshape, w_fc) + b_fc의 

    복귀 X, y_true, y_predict 

DEF : cunv_fc ()
     # 획득 된 실제 데이터 
    MNIST = input_data.read_data_sets ( " ./mnist/input_data/ " , one_hot = TRUE)
     #가 출력 파생 모델 정의 
    X를 y_true, y_predict =는 모델 () 

    # 1 의 모든 샘플이 손실 평균 취득
    (함께 tf.compat.v1.variable_scope " soft_cross " :)
         #는 평균 크로스 엔트로피 손실 계산 
        손실 = tf.reduce_mean (tf.nn.softmax_cross_entropy_with_logits (= y_true 레이블, logits = y_predict)) 

    #에 결정된 기울기 하강 손실 
    TF로한다. compat.v1.variable_scope ( " 최적화 " ) 
        train_op = tf.compat.v1.train.GradientDescentOptimizer (0.1 ) .minimize 손익 

    #의 계산 정확도 
    tf.compat.v1.variable_scope 함께 ( " 누산기 " ) 
        equal_list = tf.equal (tf.argmax (y_true ,. 1) tf.argmax (y_predict ,. 1 ))
         # 전환 유형 샘플과 평균 
        정확도 =tf.reduce_mean (tf.cast (equal_list, tf.float32)) 

    #의   방공호 가변 정의 초기화 
    init_op = tf.global_variables_initializer () 

    # 오픈 세션 실행 
    SESS AS tf.compat.v1.Session ()로 :
         # 1 변수 초기화 
        SESS을 .RUN (init_op) 

        # 트레이닝의 반복 회수는 파라미터 예측 업데이트하기 
        위한 I  범위 (1000 ) :
             #가 실제 고유 값 및 타겟 추출 
            mnist_x, mnist_y mnist.train.next_batch = (50 ) 

            #의 실행 train_op 훈련 
            sess.run을 (train_op, feed_dict = {X : mnist_x, y_true : mnist_y}) 


            인쇄 ( " % d의 정확도 속도 F %의 훈련 "% (I, sess.run (정확성 feed_dict = {X : mnist_x, y_true : mnist_y}))) 


경우  __name__ == ' __main__ ' : 
    cunv_fc ()

 

추천

출처www.cnblogs.com/wen-kang/p/11127772.html