2017-11-18 4 views
0

내가 tensorflow와 아주 간단한 선형 회귀를 양성하려고 의도 한 포기하지 않고 손실은 감소하지 않고 tensorboard도 보이지 않는 내가 가진tensorflow - 선형 회귀는 계산 그래프

### Generate data 
w_true = np.array([1.0,2.0]) 
b_true = 0.5 

x_train = np.random.multivariate_normal(mean=[0,0], cov=[[1,0],[0,1]], size=100) 
x_test = np.random.multivariate_normal(mean=[0,0], cov=[[3,0],[0,3]], size=100) 
y_train = np.dot(x_train,w_true) + b_true 
y_test = np.dot(x_test,w_true) + b_true 

### Placeholders for data input 
x = tf.placeholder(dtype=tf.float32, shape=[None,2], name="x") 
y = tf.placeholder(dtype=tf.float32, shape=[None], name="labels") 

### Trainable parameters 
w = tf.Variable(initial_value=np.random.multivariate_normal([0,0],[[1,0],[0,1]]), dtype=tf.float32, 
       name="W") 
b = tf.Variable(initial_value=np.random.normal(1), dtype=tf.float32,name="B") 

### Computational graph 
y_pred = tf.tensordot(x,w,1)+b 
tf.summary.histogram("weights",w) 
tf.summary.histogram("bias",b) 
loss = tf.reduce_sum(tf.squared_difference(y,y_pred), name="loss") 
tf.summary.scalar("loss", loss) 
with tf.name_scope("train"): 
    train_step = tf.train.GradientDescentOptimizer(0.00001).minimize(loss) 

### Training 
sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 

# For TensorBoard 
writer = tf.summary.FileWriter("path_to_some_folder") 
writer.add_graph(sess.graph) 

for t in range(1000): 
    x_batch = x_train[np.random.choice(100, 20)] 
    y_batch = y_train[np.random.choice(100, 20)] 
    sess.run(train_step, {x:x_batch,y:y_batch}) 

print(sess.run(loss, {x:x_train,y:y_train})) 
print(sess.run(loss, {x:x_test,y:y_test})) 

못했습니다 다른 단계 크기를 시도했지만 오류는 항상 교육에서 400 이상, 테스트 세트에서 1000 이상을 유지합니다. 나는 tf.tensordot()이 내가 예상 한대로 동작하는지 테스트했습니다. 당신은 tensorboard 그냥 교체를 참조하고 싶은 path_to_some_folder와 도움 각에서

답변

1

귀하의 문제는 다음 두 줄입니다,

x_batch = x_train[np.random.choice(100, 20)] 
y_batch = y_train[np.random.choice(100, 20)] 

에 대한 tensorboard --logdir path_to_some_folder

정말 감사합니다 교육 실행 한 후 반복, np.random.choice (100, 20)x_batchy_batch에 대해 서로 다른 두 개의 인덱스 목록을 반환합니다. 따라서 x_batchy_batch은 절대로 일치하지 않습니다. 대신 해당 부분을 다음 코드로 대체하십시오.

BATCH_SIZE= 10 
N_COUNT = len(x_train) 

for t in range(1000): 
    for start, end in zip(range(0, N_COUNT, BATCH_SIZE), 
          range(BATCH_SIZE, N_COUNT + 1,BATCH_SIZE)): 
     x_batch = x_train[start:end] 
     y_batch = y_train[start:end] 
     sess.run(train_step, {x:x_batch,y:y_batch}) 

희망이 도움이됩니다.

관련 문제