我正在尝试使用我自己的数据在tensorflow(此处为完整代码http://pastebin.com/m6EwakjL)中编写基本逻辑回归。问题是foreach批量运行,我的重量中的所有值都保持在nan,导致所有其他计算(成本等)(显然)失败。
我尝试将我的权重矩阵初始化为'random_normal'或'zeros',但这没有任何区别:
# neither work
batch_size = 100
W = tf.Variable(tf.random_normal([batch_size,num_features]))
W = tf.Variable(tf.zeros([batch_size,num_features]))
以下代码用于LR设置。
# Xtrain = 100k x 10 , 10 features
# Ytrain = 100k x 2, 2 classes
num_features = Xtrain.shape[1]
num_classes = Ytrain.shape[1]
# tf Graph Input
x = tf.placeholder(tf.float32, [batch_size, num_features])
y = tf.placeholder(tf.float32, [batch_size, num_classes])
# Set model weights
W = tf.Variable(tf.random_normal([num_features, num_classes]))
b = tf.Variable(tf.zeros([num_classes]))
mm = tf.matmul(x, W)
model = mm + b
# Construct model
pred = tf.nn.softmax(model)
# Minimize error using cross entropy
#cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred)))
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
下面我尝试查看W,x和其他变量的值以进行调试。但我的W总是'南'。
# Initializing the variables
init = tf.initialize_all_variables()
# Launch the graph
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(Xtrain.shape[0]/batch_size)
# Loop over all batches
for i in range(total_batch):
#s = (i - 1) * batch_size
s = i * batch_size
e = s + batch_size
batch_xs = Xtrain[s:e,:]
batch_ys = Ytrain[s:e,:]
#print (" i = %s, s = %s, e = %s" % (i,s,e))
# Fit training using batch data
_, c, m, matrix_multiply, bias, w = sess.run([optimizer, cost,model,mm,b,W], feed_dict={x: batch_xs,y: batch_ys})
print ("i = %s, start = %s, end = %s" % (i,s,e))
print ("# X shape #")
print (batch_xs.shape)
print ("w = %s" % w)
print ("matrix_multiply = %s" % matrix_multiply)
print ("bias = %s" % bias)
print ("model = %s" % m)
关于我的w矩阵的任何想法总是纳米?