2017-12-12 7 views
0

Tensorflow를 사용하여 신경망을 훈련 중입니다. 나는 100 에포크마다 교육비를 계산하고 비용을 인쇄하고있다. 내 모델에 다음과 같은 교육 코드를 사용하고 있습니다.훈련 과정 중 특정 신기원 이후에 신경망의 가중치 (매개 변수)에 액세스하는 방법

def model(X_train, Y_train, layers_dims,learning_rate = 0.0001, 
     num_epochs = 100, minibatch_size = 32, print_cost = True): 



ops.reset_default_graph()       
tf.set_random_seed(1)        
seed = 3           
(n_x, m) = X_train.shape       
n_y = Y_train.shape[0]        
costs = [] 
beta = 0 



X, Y,keep_prob= create_placeholders(n_x, n_y) 


# Initialize parameters 

parameters = initialize_parameters(layers_dims) 


# Forward propagation: Build the forward propagation in the tensorflow graph 

Z = forward_propagation(X, parameters, keep_prob) 




cost = compute_cost(Z, Y) 
L3=len(layers_dims) 
regularizers=tf.constant(0,dtype=tf.float64) 
for l3 in range(1,L3): 
    regularizers = regularizers+tf.nn.l2_loss(parameters['W' + str(l3)]) 
cost = tf.reduce_mean(cost + beta * regularizers) 




with tf.device('/gpu:0'): 
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost) 





init = tf.global_variables_initializer() 


with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: 


    sess.run(init) 


    for epoch in range(num_epochs): 

     epoch_cost = 0.      # Defines a cost related to an epoch 
     num_minibatches = int(m/minibatch_size) # number of minibatches of size minibatch_size in the train set 
     seed = seed + 1 
     minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed) 

     for minibatch in minibatches: 


      (minibatch_X, minibatch_Y) = minibatch 




      _ , minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y,keep_prob:1}) 


      epoch_cost += minibatch_cost/num_minibatches 


     if print_cost == True and epoch % 100 == 0: 
      print ("Cost after epoch %i: %f" % (epoch, epoch_cost)) 
     if print_cost == True and epoch % 5 == 0: 
      costs.append(epoch_cost) 


    plt.plot(np.squeeze(costs)) 
    plt.ylabel('cost') 
    plt.xlabel('iterations (per tens)') 
    plt.title("Learning rate =" + str(learning_rate)) 
    plt.show() 


    parameters = sess.run(parameters) 
    print ("Parameters have been trained!") 


    return parameters 

은 내가 매 100 신 (新) 시대 이후에 검증 설정 비용을 인쇄하려면이 매개 변수를 사용할 수 있도록 훈련 기간 동안 매 100 신 (新) 시대 이후의 매개 변수에 액세스하려면. 유효성 검사 세트 비용과 훈련 세트 비용을 모두 단일 플롯에 플로팅하고 싶습니다. 현재 나는 훈련 세트 비용만을 계획하고 인쇄하고 있습니다. enter image description here

답변

1

쉬운 일이 검증 세트 모델을 실행하는 것입니다 :

val_minibatch_cost = sess.run(cost, feed_dict={X: val_minibatch_X, Y: val_minibatch_Y, keep_prob:1} 
+0

그래, 내 코드를 보면 "비용"이라는 용어는 유효성 검사 집합의 비용 계산에 포함되어서는 안되는 정규화 계수를 포함합니다. –

0

나는 추가 코드 다음을 포함하여 해결했다. 그래도 내 코드는 나에게 비효율적 인 것처럼 보입니다.

#Additional blocks of code for getting validation set cost. 
cv_costs_post_train=[] 
beta = 0.5 
(n_x_cv, m_cv) = cv_x.shape       
n_y_cv = cv_y.shape[0] 
............ 
............. 
........... 
........... 
if print_cost == True and epoch % 100 == 0: 
      print ("Train Cost after epoch %i: %f" % (epoch, epoch_cost)) 
      X_cv, Y_cv,keep_prob_cv= create_placeholders(n_x_cv, n_y_cv) 
      Z_cv = forward_propagation(X_cv, parameters, keep_prob_cv) 
      cv_cost_post_train = compute_cost(Z_cv,Y_cv) 
      cv_cost_post_train = sess.run([cv_cost_post_train ], feed_dict={X_cv: cv_x, Y_cv: cv_y, keep_prob_cv:1}) 
      print (cv_cost_post_train) 




if print_cost == True and epoch % 5 == 0: 
      costs.append(epoch_cost) 
      cv_costs_post_train.append(cv_cost_post_train) 
.... 
.... 
.... 
.... 

전체 코드는 아래에 나와 있습니다.

def model(X_train, Y_train,cv_x, cv_y, layers_dims,learning_rate = 0.0001, 
     num_epochs = 2000, minibatch_size = 32, print_cost = True): 



ops.reset_default_graph()       
tf.set_random_seed(1)        
seed = 3           
(n_x, m) = X_train.shape       
n_y = Y_train.shape[0]        
costs = [] 
cv_costs_post_train=[] 
beta = 0.5 
(n_x_cv, m_cv) = cv_x.shape       
n_y_cv = cv_y.shape[0] 


########################################################################### 


############################################################################################### 

# Create Placeholders of shape (n_x, n_y) 
### START CODE HERE ### (1 line) 
X, Y,keep_prob= create_placeholders(n_x, n_y) 
### END CODE HERE ### 

# Initialize parameters 
### START CODE HERE ### (1 line) 
parameters = initialize_parameters(layers_dims) 
### END CODE HERE ### 

# Forward propagation: Build the forward propagation in the tensorflow graph 
### START CODE HERE ### (1 line) 
Z = forward_propagation(X, parameters, keep_prob) 
### END CODE HERE ### 

# Cost function: Add cost function to tensorflow graph 
### START CODE HERE ### (1 line) 
cost = compute_cost(Z, Y) 
L3=len(layers_dims) 
regularizers=tf.constant(0,dtype=tf.float64) 
for l3 in range(1,L3): 
    regularizers = regularizers+tf.nn.l2_loss(parameters['W' + str(l3)]) 
loss = tf.reduce_mean(cost + beta * regularizers) 

### END CODE HERE ### 

# Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer. 
### START CODE HERE ### (1 line) 
with tf.device('/gpu:0'): 
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(loss) 


### END CODE HERE ### 

# Initialize all the variables 
init = tf.global_variables_initializer() 

# Start the session to compute the tensorflow graph 
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: 

    # Run the initialization 
    sess.run(init) 

    # Do the training loop 
    for epoch in range(num_epochs): 

     epoch_cost = 0.      # Defines a cost related to an epoch 
     num_minibatches = int(m/minibatch_size) # number of minibatches of size minibatch_size in the train set 
     seed = seed + 1 
     minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed) 

     for minibatch in minibatches: 

      # Select a minibatch 
      (minibatch_X, minibatch_Y) = minibatch 

      # IMPORTANT: The line that runs the graph on a minibatch. 
      # Run the session to execute the "optimizer" and the "cost", the feedict should contain a minibatch for (X,Y). 
      ### START CODE HERE ### (1 line) 
      _ , minibatch_cost = sess.run([optimizer, loss], feed_dict={X: minibatch_X, Y: minibatch_Y,keep_prob:0.5}) 
      ### END CODE HERE ### 

      epoch_cost += minibatch_cost/num_minibatches 

     # Print the cost every epoch 
     if print_cost == True and epoch % 100 == 0: 
      print ("Train Cost after epoch %i: %f" % (epoch, epoch_cost)) 


      X_cv, Y_cv,keep_prob_cv= create_placeholders(n_x_cv, n_y_cv) 
      Z_cv = forward_propagation(X_cv, parameters, keep_prob_cv) 
      cv_cost_post_train = compute_cost(Z_cv,Y_cv) 
      cv_cost_post_train = sess.run([cv_cost_post_train ], feed_dict={X_cv: cv_x, Y_cv: cv_y, keep_prob_cv:1}) 
      print (cv_cost_post_train) 


     if print_cost == True and epoch % 5 == 0: 
      costs.append(epoch_cost) 
      cv_costs_post_train.append(cv_cost_post_train) 

    # plot the cost 
    plt.plot(np.squeeze(costs)) 
    plt.ylabel('train cost') 
    plt.xlabel('iterations (per tens)') 
    plt.title("Learning rate =" + str(learning_rate)) 
    plt.show() 




    plt.plot(np.squeeze(cv_costs_post_train)) 
    plt.ylabel('CV cost') 
    plt.xlabel('iterations (per tens)') 
    plt.title("Learning rate =" + str(learning_rate)) 
    plt.show() 

    # lets save the parameters in a variable 
    parameters = sess.run(parameters) 
    print ("Parameters have been trained!") 


    return parameters