2016-11-05 2 views
0

최근 mnist tensorflow 안내서를 작성하여 조금 변경하려고했습니다. 이 예제에서는 28 * 28 * 3 (r, g, b의 경우 3)의 입력을 얻으려고 시도하고 똑같은 출력을 돌려줍니다. 쉽게 나는 순수한 흰색을 안팎으로하고 있습니다.tensorflow 예측에 모두 0이 포함되어 있습니다.

#!/usr/bin/env python 
import tensorflow as tf 

input_layer_size = 2352 # number of pixels * number of color channels (rgb) 

white = [255] * input_layer_size # white is a square of white pixels 
white = [white] 

sess = tf.InteractiveSession() 

x = tf.placeholder(tf.float32, shape=[None, input_layer_size]) 
y_ = tf.placeholder(tf.float32, shape=[None, input_layer_size]) 

W = tf.Variable(tf.truncated_normal([input_layer_size,input_layer_size], stddev=0.1)) 
b = tf.Variable(tf.truncated_normal([input_layer_size], stddev=0.1)) 

sess.run(tf.initialize_all_variables()) 

y = tf.nn.softmax(tf.matmul(x,W) + b) 
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)), reduction_indices=[1])) 
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 

for i in range(100): 
    train_step.run(feed_dict={x: white, y_: white}) 

feed_dict = {x:white} 
classification = sess.run(y, feed_dict) 
print ("Output:", classification[0]) 

어떤 이유로이 출력은 [ 0., 0., 0., ..., 0., 0., 0.]입니다. 예상 결과가 아닌 이유는 무엇입니까 ([ 255., 255., ... ])?

나는 똑같은 코드를 mnist 데이터와 함께 시도해 보았고 정상적으로 작동하여 10 개의 출력 채널에 각각 적절한 결과를 제공했습니다.

답변

2

코드에서 x와 y가 두 개의 이미지를 나타내는 (행) 벡터 인 y에서 x까지의 선형 변환을 배우려고하는 것으로 보입니다. y = x * W + b. 이것은 회귀 문제입니다. 해는 항등 행렬이며, b는 제로 벡터입니다. 아래 코드는 최소화함으로써이 문제를 해결 | Y - (X의 * (W)를 + B) | : 그것은 한 뜨거운 : y는 다르기 때문에 당신은 mnist 데이터와 동일한 코드를 시도

#!/usr/bin/env python 
import tensorflow as tf 
tf.reset_default_graph() 

input_layer_size = 2352 # number of pixels * number of color channels (rgb) 

white = [255] * input_layer_size # white is a square of white pixels 
white = [white] 

sess = tf.InteractiveSession() 

x = tf.placeholder(tf.float32, shape=[None, input_layer_size]) 
y_ = tf.placeholder(tf.float32, shape=[None, input_layer_size]) 

W = tf.Variable(tf.truncated_normal([input_layer_size,input_layer_size], stddev=0.1)) 
b = tf.Variable(tf.truncated_normal([input_layer_size], stddev=0.1)) 

y = tf.matmul(x,W) + b 
loss = tf.reduce_sum(tf.abs(y - y_)) 
train_step = tf.train.AdagradOptimizer(0.001).minimize(loss) 

sess.run(tf.initialize_all_variables()) 

for i in range(1000): 
    loss_, _ = sess.run([loss, train_step], feed_dict={x: white, y_: white}) 

print loss_ 

feed_dict = {x:white} 
classification = sess.run(y, feed_dict) 
print ("Output:", classification[0]) 

, 그것은 일 0의 경우 0, 0, 0, ...이 될 것입니다. 1의 경우 0, 1, 0, 0, ...이됩니다. 2 - 0, 0, 1, ... 등등.

+0

그러나 원래 코드의 문제점은 무엇입니까? –

+0

여러 가지가 잘못되었습니다. –

+1

위의 사고 설명을 무시하십시오. 여러 가지가 잘못되었습니다. y는 손실을 최소화하기 위해 약 1e-10, 1 범위로 클리핑되지만, 손실 자체 (softmax의 교차 엔트로피)는 다중 클래스 분류 문제이지만 회귀 문제는 아닙니다. –

관련 문제