Skip to content

Setting large batch_size in logistic regression method will result output nan #307

Closed

Description

@chenghuige

import tensorflow as tf
import numpy as np
import melt_dataset
import sys
from sklearn.metrics import roc_auc_score

def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))

def model(X, w):
return 1.0/(1.0 + tf.exp(-(tf.matmul(X, w))))

./logistic_regression.py corpus/feature.normed.rand.12000.0_2.txt corpus/feature.normed.rand.12000.1_2.txt

notice if setting batch_size too big here 500 will result in learning turn output nan... why? @todo

batch_size = 100
learning_rate = 0.01
num_iters = 100

argv = sys.argv
trainset = argv[1]
testset = argv[2]

trX, trY = melt_dataset.load_dense_data(trainset)
print "finish loading train set ",trainset
teX, teY = melt_dataset.load_dense_data(testset)
print "finish loading test set ", testset

num_features = trX[0].shape[0]
print 'num_features: ',num_features
print 'trainSet size: ', len(trX)
print 'testSet size: ', len(teX)
print 'batch_size:', batch_size, ' learning_rate:', learning_rate, ' num_iters:', num_iters

X = tf.placeholder("float", [None, num_features]) # create symbolic variables
Y = tf.placeholder("float", [None, 1])

w = init_weights([num_features, 1]) # like in linear regression, we need a shared variable weight matrix for logistic regression

py_x = model(X, w)

cost = -tf.reduce_sum(Y*tf.log(py_x) + (1 - Y) * tf.log(1 - py_x))
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # construct optimizer

predict_op = py_x

sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)

for i in range(num_iters):
predicts, cost_ = sess.run([predict_op, cost], feed_dict={X: teX, Y: teY})
print i, 'auc:', roc_auc_score(teY, predicts), 'cost:', cost_
for start, end in zip(range(0, len(trX), batch_size), range(batch_size, len(trX), batch_size)):
sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})

predicts, cost_ = sess.run([predict_op, cost], feed_dict={X: teX, Y: teY})
print 'final ', 'auc:', roc_auc_score(teY, predicts),'cost:', cost_

if setting batch_size to 100
0 auc: 0.595120586861 cost: 1422.4
1 auc: 0.911648704913 cost: 477.082
2 auc: 0.916009327839 cost: 459.99
3 auc: 0.918605639188 cost: 460.72
4 auc: 0.919915596278 cost: 474.58
5 auc: 0.920871510912 cost: 487.933
6 auc: 0.921381332049 cost: 500.054
7 auc: 0.921853388658 cost: 510.762

if setting batch_size to 500
0 auc: 0.615236099113 cost: 1354.38
1 auc: 0.560017277272 cost: nan
2 auc: 0.560017277272 cost: nan
3 auc: 0.560017277272 cost: nan
4 auc: 0.560017277272 cost: nan
5 auc: 0.560017277272 cost: nan
6 auc: 0.560017277272 cost: nan
7 auc: 0.560017277272 cost: nan
8 auc: 0.560017277272 cost: nan

I am a bit curious as I will not face nan for the same train,test data using thenao implemented logistic regression.

Activity

  1. chenghuige commented on Nov 21, 2015

    @chenghuige
    Author

    Well, I find it is due to too large learning rate, for large batch size , need low learning rate, not an issue, close it.

  2. mxrguspxrt commented on Feb 13, 2016

    @mxrguspxrt

    "tensorflow.python.framework.errors.OutOfRangeError: Nan in summary histogram for: HistogramSummary"

    This is really hard to understand error. If you want to increase developer happiness using TensorFlow, a more clearer error message could help.

  3. vrv commented on Feb 13, 2016

    @vrv

    @mxrguspxrt: so we can understand what specific improvements would help, what is your current understanding of that error message?

  4. mxrguspxrt commented on Feb 13, 2016

    @mxrguspxrt
  5. added a commit that references this issue on Dec 10, 2019
    90d7101
  6. added a commit that references this issue on May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions