2017-12-12 9 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のエポック後のパラメータにアクセスします。私は、検証プロットのコストとトレーニングセットのコストを両方とも1つのプロットにプロットしたいと思います。現在、トレーニングセットのコストのみをプロットして印刷しています。 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 
関連する問題