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つのプロットにプロットしたいと思います。現在、トレーニングセットのコストのみをプロットして印刷しています。
ええ、それは良い方法ですが、自分のコードを見ると、「コスト」という用語には、検証セットのコストを計算する際に含めてはならない正規化係数が含まれています。 –