Tensorflow: NaN training loss in all epoches (after second batch in the first epoch), and the training...
The original implement is correct which only includes the loss1 and loss2, so I think my input data is correct.
But the training loss is always 'nan' after I added the loss3 named 'sw_loss' which aims at minimize the L2 norm between the rows of the 'features'. The 'features' are the output of the last layer of the network.
Actually, the trainging loss became 'nan' in the second batch of the first epoch, and the loss of first batch is about 2.2.
Here are the main codes:
features, _ = mnist_net(images)
centers = func.construct_center(features, FLAGS.num_classes)
loss1 = func.dce_loss(features, labels, centers, FLAGS.temp)
loss2 = func.pl_loss(features, labels, centers)
loss3 = func.sw_loss(features, similarity_weight_batch) #loss3 is defined in the following
loss = loss1 + FLAGS.weight_pl * loss2 + FLAGS.weight_sw * loss3
eval_correct = func.evaluation(features, labels, centers)
train_op = func.training(loss, lr)
init = tf.global_variables_initializer()
# initialize the variables
sess = tf.Session()
sess.run(init)
#compute_centers(sess, add_op, count_op, average_op, images, labels, train_x, train_y)
# run the computation graph (train and test process)
epoch = 1
loss_before = np.inf
score_before = 0.0
stopping = 0
index = list(range(train_num))
np.random.shuffle(index)
batch_size = FLAGS.batch_size
batch_num = train_num//batch_size if train_num % batch_size==0 else train_num//batch_size+1
train_start= time.time()
while stopping<FLAGS.stop:
time1 = time.time()
loss_now = 0.0
score_now = 0.0
for i in range(batch_num):
batch_x = train_x[index[i*batch_size:(i+1)*batch_size]]
batch_y = train_y[index[i*batch_size:(i+1)*batch_size]]
batch_index = np.asarray( index[i*batch_size:(i+1)*batch_size])
weight_batch = np.zeros(shape=(batch_index.shape[0],batch_index.shape[0]))
for j in range(batch_index.shape[0]):
for k in range(batch_index.shape[0]):
weight_batch[j,k] = similarity_weight[[batch_index[j,]],[batch_index[k,]]]
result = sess.run([train_op, loss, eval_correct], feed_dict={images:batch_x,
labels:batch_y, lr:FLAGS.learning_rate, similarity_weight_batch:weight_batch})
loss_now += result[1]
score_now += result[2][1]
score_now /= train_num
The sw_loss is defined in the function file as follows:.
def sw_loss(features, similarity_weight_batch): #'similarity_weight_batch' is the coefficients,which is between(0,1].
sw_loss_total = 0.0
sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1)) #calculate the L2 norm between the rows of the features
sw_loss_total = tf.multiply(similarity_weight_batch,feature_matrix)
return tf.reduce_mean(sw_loss_total)
The printed logs are as follows, the training loss is 'nan' in all epoch:
epoch 1: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.074 minutes
epoch 2: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.024 minutes
epoch 3: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.073 minutes
epoch 4: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.033 minutes
epoch 5: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.021 minutes
...
python tensorflow
add a comment |
The original implement is correct which only includes the loss1 and loss2, so I think my input data is correct.
But the training loss is always 'nan' after I added the loss3 named 'sw_loss' which aims at minimize the L2 norm between the rows of the 'features'. The 'features' are the output of the last layer of the network.
Actually, the trainging loss became 'nan' in the second batch of the first epoch, and the loss of first batch is about 2.2.
Here are the main codes:
features, _ = mnist_net(images)
centers = func.construct_center(features, FLAGS.num_classes)
loss1 = func.dce_loss(features, labels, centers, FLAGS.temp)
loss2 = func.pl_loss(features, labels, centers)
loss3 = func.sw_loss(features, similarity_weight_batch) #loss3 is defined in the following
loss = loss1 + FLAGS.weight_pl * loss2 + FLAGS.weight_sw * loss3
eval_correct = func.evaluation(features, labels, centers)
train_op = func.training(loss, lr)
init = tf.global_variables_initializer()
# initialize the variables
sess = tf.Session()
sess.run(init)
#compute_centers(sess, add_op, count_op, average_op, images, labels, train_x, train_y)
# run the computation graph (train and test process)
epoch = 1
loss_before = np.inf
score_before = 0.0
stopping = 0
index = list(range(train_num))
np.random.shuffle(index)
batch_size = FLAGS.batch_size
batch_num = train_num//batch_size if train_num % batch_size==0 else train_num//batch_size+1
train_start= time.time()
while stopping<FLAGS.stop:
time1 = time.time()
loss_now = 0.0
score_now = 0.0
for i in range(batch_num):
batch_x = train_x[index[i*batch_size:(i+1)*batch_size]]
batch_y = train_y[index[i*batch_size:(i+1)*batch_size]]
batch_index = np.asarray( index[i*batch_size:(i+1)*batch_size])
weight_batch = np.zeros(shape=(batch_index.shape[0],batch_index.shape[0]))
for j in range(batch_index.shape[0]):
for k in range(batch_index.shape[0]):
weight_batch[j,k] = similarity_weight[[batch_index[j,]],[batch_index[k,]]]
result = sess.run([train_op, loss, eval_correct], feed_dict={images:batch_x,
labels:batch_y, lr:FLAGS.learning_rate, similarity_weight_batch:weight_batch})
loss_now += result[1]
score_now += result[2][1]
score_now /= train_num
The sw_loss is defined in the function file as follows:.
def sw_loss(features, similarity_weight_batch): #'similarity_weight_batch' is the coefficients,which is between(0,1].
sw_loss_total = 0.0
sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1)) #calculate the L2 norm between the rows of the features
sw_loss_total = tf.multiply(similarity_weight_batch,feature_matrix)
return tf.reduce_mean(sw_loss_total)
The printed logs are as follows, the training loss is 'nan' in all epoch:
epoch 1: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.074 minutes
epoch 2: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.024 minutes
epoch 3: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.073 minutes
epoch 4: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.033 minutes
epoch 5: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.021 minutes
...
python tensorflow
add a comment |
The original implement is correct which only includes the loss1 and loss2, so I think my input data is correct.
But the training loss is always 'nan' after I added the loss3 named 'sw_loss' which aims at minimize the L2 norm between the rows of the 'features'. The 'features' are the output of the last layer of the network.
Actually, the trainging loss became 'nan' in the second batch of the first epoch, and the loss of first batch is about 2.2.
Here are the main codes:
features, _ = mnist_net(images)
centers = func.construct_center(features, FLAGS.num_classes)
loss1 = func.dce_loss(features, labels, centers, FLAGS.temp)
loss2 = func.pl_loss(features, labels, centers)
loss3 = func.sw_loss(features, similarity_weight_batch) #loss3 is defined in the following
loss = loss1 + FLAGS.weight_pl * loss2 + FLAGS.weight_sw * loss3
eval_correct = func.evaluation(features, labels, centers)
train_op = func.training(loss, lr)
init = tf.global_variables_initializer()
# initialize the variables
sess = tf.Session()
sess.run(init)
#compute_centers(sess, add_op, count_op, average_op, images, labels, train_x, train_y)
# run the computation graph (train and test process)
epoch = 1
loss_before = np.inf
score_before = 0.0
stopping = 0
index = list(range(train_num))
np.random.shuffle(index)
batch_size = FLAGS.batch_size
batch_num = train_num//batch_size if train_num % batch_size==0 else train_num//batch_size+1
train_start= time.time()
while stopping<FLAGS.stop:
time1 = time.time()
loss_now = 0.0
score_now = 0.0
for i in range(batch_num):
batch_x = train_x[index[i*batch_size:(i+1)*batch_size]]
batch_y = train_y[index[i*batch_size:(i+1)*batch_size]]
batch_index = np.asarray( index[i*batch_size:(i+1)*batch_size])
weight_batch = np.zeros(shape=(batch_index.shape[0],batch_index.shape[0]))
for j in range(batch_index.shape[0]):
for k in range(batch_index.shape[0]):
weight_batch[j,k] = similarity_weight[[batch_index[j,]],[batch_index[k,]]]
result = sess.run([train_op, loss, eval_correct], feed_dict={images:batch_x,
labels:batch_y, lr:FLAGS.learning_rate, similarity_weight_batch:weight_batch})
loss_now += result[1]
score_now += result[2][1]
score_now /= train_num
The sw_loss is defined in the function file as follows:.
def sw_loss(features, similarity_weight_batch): #'similarity_weight_batch' is the coefficients,which is between(0,1].
sw_loss_total = 0.0
sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1)) #calculate the L2 norm between the rows of the features
sw_loss_total = tf.multiply(similarity_weight_batch,feature_matrix)
return tf.reduce_mean(sw_loss_total)
The printed logs are as follows, the training loss is 'nan' in all epoch:
epoch 1: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.074 minutes
epoch 2: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.024 minutes
epoch 3: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.073 minutes
epoch 4: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.033 minutes
epoch 5: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.021 minutes
...
python tensorflow
The original implement is correct which only includes the loss1 and loss2, so I think my input data is correct.
But the training loss is always 'nan' after I added the loss3 named 'sw_loss' which aims at minimize the L2 norm between the rows of the 'features'. The 'features' are the output of the last layer of the network.
Actually, the trainging loss became 'nan' in the second batch of the first epoch, and the loss of first batch is about 2.2.
Here are the main codes:
features, _ = mnist_net(images)
centers = func.construct_center(features, FLAGS.num_classes)
loss1 = func.dce_loss(features, labels, centers, FLAGS.temp)
loss2 = func.pl_loss(features, labels, centers)
loss3 = func.sw_loss(features, similarity_weight_batch) #loss3 is defined in the following
loss = loss1 + FLAGS.weight_pl * loss2 + FLAGS.weight_sw * loss3
eval_correct = func.evaluation(features, labels, centers)
train_op = func.training(loss, lr)
init = tf.global_variables_initializer()
# initialize the variables
sess = tf.Session()
sess.run(init)
#compute_centers(sess, add_op, count_op, average_op, images, labels, train_x, train_y)
# run the computation graph (train and test process)
epoch = 1
loss_before = np.inf
score_before = 0.0
stopping = 0
index = list(range(train_num))
np.random.shuffle(index)
batch_size = FLAGS.batch_size
batch_num = train_num//batch_size if train_num % batch_size==0 else train_num//batch_size+1
train_start= time.time()
while stopping<FLAGS.stop:
time1 = time.time()
loss_now = 0.0
score_now = 0.0
for i in range(batch_num):
batch_x = train_x[index[i*batch_size:(i+1)*batch_size]]
batch_y = train_y[index[i*batch_size:(i+1)*batch_size]]
batch_index = np.asarray( index[i*batch_size:(i+1)*batch_size])
weight_batch = np.zeros(shape=(batch_index.shape[0],batch_index.shape[0]))
for j in range(batch_index.shape[0]):
for k in range(batch_index.shape[0]):
weight_batch[j,k] = similarity_weight[[batch_index[j,]],[batch_index[k,]]]
result = sess.run([train_op, loss, eval_correct], feed_dict={images:batch_x,
labels:batch_y, lr:FLAGS.learning_rate, similarity_weight_batch:weight_batch})
loss_now += result[1]
score_now += result[2][1]
score_now /= train_num
The sw_loss is defined in the function file as follows:.
def sw_loss(features, similarity_weight_batch): #'similarity_weight_batch' is the coefficients,which is between(0,1].
sw_loss_total = 0.0
sqdiff = tf.squared_difference(features[:, tf.newaxis], features)
feature_matrix = tf.sqrt(tf.reduce_sum(sqdiff, axis=-1)) #calculate the L2 norm between the rows of the features
sw_loss_total = tf.multiply(similarity_weight_batch,feature_matrix)
return tf.reduce_mean(sw_loss_total)
The printed logs are as follows, the training loss is 'nan' in all epoch:
epoch 1: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.074 minutes
epoch 2: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.024 minutes
epoch 3: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.073 minutes
epoch 4: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.033 minutes
epoch 5: training: loss --> nan, acc --> 15.514%
time for this epoch: 0.021 minutes
...
python tensorflow
python tensorflow
edited Nov 23 '18 at 11:53
Bobo Xi
asked Nov 22 '18 at 8:47
Bobo XiBobo Xi
336
336
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426964%2ftensorflow-nan-training-loss-in-all-epoches-after-second-batch-in-the-first-ep%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426964%2ftensorflow-nan-training-loss-in-all-epoches-after-second-batch-in-the-first-ep%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown