3

내가 속도 매트릭스 R이 있다고 가정하고 나는 배치 크기의 간단한 문제없이 tensorflow메모리 누수

UV 행렬 그것을 인수 분해하고 싶은 다음 코드로 해결할 수 :

# define Variables 
u = tf.Variable(np.random.rand(R_dim_1, output_dim), dtype=tf.float32, name='u') 
v = tf.Variable(np.random.rand(output_dim, R_dim_2), dtype=tf.float32, name='v') 

# predict rate by multiplication 
predicted_R = tf.matmul(tf.cast(u, tf.float32), tf.cast(v, tf.float32)) 

#cost function and train step 
cost = tf.reduce_sum(tf.reduce_sum(tf.abs(tf.sub(predicted_R, R)))) 
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cost) 

with tf.Session() as sess: 
    init = tf.initialize_all_variables() 
    sess.run(init) 
    for i in range(no_epochs): 
     _, this_cost = sess.run([train_step, cost]) 
     print 'cost: ', this_cost 

나는 배치 업데이트를 통해이 문제를 해결하기로 결정하고 내 솔루션은 내가 속도 매트릭스 R 및 업데이트 단지 그 선택들 01을 예측에 사용할 UV의 인덱스를 전송했다

# define variables 
u = tf.Variable(np.random.rand(R_dim_1, output_dim), dtype=tf.float32, name='u') 
v = tf.Variable(np.random.rand(output_dim, R_dim_2), dtype=tf.float32, name='v') 
idx1 = tf.placeholder(tf.int32, shape=batch_size1, name='idx1') 
idx2 = tf.placeholder(tf.int32, shape=batch_size2, name='idx2') 

# get current U and current V by slicing U and V 
cur_u = tf.Variable(tf.gather(u, idx1), dtype=tf.float32, name='cur_u') 
cur_v = tf.transpose(v) 
cur_v = tf.gather(cur_v, idx2) 
cur_v = tf.Variable(tf.transpose(cur_v), dtype=tf.float32, name='cur_v') 

# predict rate by multiplication 
predicted_R = tf.matmul(tf.cast(cur_u, tf.float32), tf.cast(cur_v, tf.float32)) 

# get needed rate from rate matrix by slicing it 
cur_rate = tf.gather(R, idx1) 
cur_rate = tf.transpose(cur_rate) 
cur_rate = tf.gather(cur_rate, idx2) 
cur_rate = tf.transpose(cur_rate) 

#cost function and train step 
cost = tf.reduce_sum(tf.reduce_sum(tf.abs(tf.sub(predicted_R, cur_rate)))) 
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cost) 

with tf.Session() as sess: 
    # initialize variables 

    init_new_vars_op = tf.initialize_variables([v, u]) 
    sess.run(init_new_vars_op) 

    init = tf.initialize_all_variables() 
    rand_idx = np.sort(np.random.randint(0, R_dim_1, batch_size1)) 
    rand_idx2 = np.sort(np.random.randint(0, R_dim_2, batch_size2)) 
    sess.run(init, feed_dict={idx1: rand_idx, idx2: rand_idx2}) 

    for i in range(no_epochs): 
     with tf.Graph().as_default(): 
      rand_idx1 = np.random.randint(0, R_dim_1, batch_size1) 
      rand_idx2 = np.random.randint(0, R_dim_2, batch_size2) 
      _, this_cost, tmp_u, tmp_v, tmp_cur_u, tmp_cur_v = sess.run([train_step, cost, u, v, cur_u, cur_v],feed_dict={idx1: rand_idx1, idx2: rand_idx2}) 
     print this_cost 

      #update U and V with computed current U and current V 
      tmp_u = np.array(tmp_u) 
      tmp_u[rand_idx] = tmp_cur_u 
      u = tf.assign(u, tmp_u) 

      tmp_v = np.array(tmp_v) 
      tmp_v[:, rand_idx2] = tmp_cur_v 
      v = tf.assign(v, tmp_v) 

을하지만 난 u = tf.assign(u, tmp_u)에서 메모리 누수 권리가 있으며 u = tf.assign(u, tmp_u) 나는 this을 적용하지만 아무것도 없어 : 23,516,여기 내 코드입니다 (그냥 많은 시간이 걸리는 경우 의견을 읽고).
V의 하위 집합에 업데이트를 적용하는 또 다른 해결책이 있었지만 다른 오류가 많이 발생하여 내 메모리 누수 문제를 해결하는 방법을 계속 따라주십시오.
오랜 시간 내 질문에 사과드립니다. 그것을 읽어 주셔서 감사합니다.

+0

, 여기 코드입니다 -getting-gradients-at-inputs 및 http://stackoverflow.com/questions/36230559/processing-time-gets-longer-and-longer-after-each-iteration-tensorflow/36233277#36233277 – etarion

+0

그게 내 문제인지 알아. 하지만 처음에는 그래프를 작성하지 않고 트레이닝 루프에서는 실행 만합니다. http://stackoverflow.com/questions/36230559/processing-time-gets-longer-and-longer-after- each-iteration-tensorflow/36233277 # 36233277 – Kibo

+0

실행 호출간에 그래프를 수정하면 메모리 사용량이 늘어나고 속도가 매우 느려집니다. 그래프가 수정 될 때마다 그래프 전체를 인코딩하고 복사해야하므로'for ... i. sess.run (a.assign_add) '와 같은 것은 2 차적인 복잡성을 갖습니다. 나는이 문제를 다루기 위해 긴급이라고 불리는 래퍼를 작성했다. https://github.com/yaroslavvb/imperative –

답변

1

난 그냥 자리로 UV의 업데이트 된 값을 전송하여이 문제를 해결하고 생성 된 그래프는 서로 다른 반복에 동일하게 유지되도록 다음이 전달 된 매개 변수에 UV을 지정합니다. 무엇 http://stackoverflow.com/questions/36245481/tensorflow-slow-performance-when에서 일어나는 유사 당신은 모든 루프 반복에서 새로운 할당 연산을 만드는

# define variables 
u = tf.Variable(np.random.rand(R_dim_1, output_dim), dtype=tf.float32, name='u') 
v = tf.Variable(np.random.rand(output_dim, R_dim_2), dtype=tf.float32, name='v') 
idx1 = tf.placeholder(tf.int32, shape=batch_size1, name='idx1') 
idx2 = tf.placeholder(tf.int32, shape=batch_size2, name='idx2') 

#define new place holder for changed values of U and V 
last_u = tf.placeholder(tf.float32, shape=[R_dim_1, output_dim], name='last_u') 
last_v = tf.placeholder(tf.float32, shape=[output_dim, R_dim_2], name='last_v') 

#set U and V to updated ones 
change_u = tf.assign(u, last_u) 
change_v = tf.assign(v, last_v) 

# get current U and current V by slicing U and V 
cur_u = tf.Variable(tf.gather(u, idx1), dtype=tf.float32, name='cur_u') 
cur_v = tf.transpose(v) 
cur_v = tf.gather(cur_v, idx2) 
cur_v = tf.Variable(tf.transpose(cur_v), dtype=tf.float32, name='cur_v') 

# predict rate by multiplication 
predicted_R = tf.matmul(tf.cast(cur_u, tf.float32), tf.cast(cur_v, tf.float32)) 

# get needed rate from rate matrix by slicing it 
cur_rate = tf.gather(R, idx1) 
cur_rate = tf.transpose(cur_rate) 
cur_rate = tf.gather(cur_rate, idx2) 
cur_rate = tf.transpose(cur_rate) 

#cost function and train step 
cost = tf.reduce_sum(tf.reduce_sum(tf.abs(tf.sub(predicted_R, cur_rate)))) 
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cost) 

with tf.Session() as sess: 
    tmp_u = initial_u; 
    tmp_v = initial_v; 

    # initialize variables 
    init_new_vars_op = tf.initialize_variables([v, u]) 
    sess.run(init_new_vars_op, feed_dict={last_u: tmp_u, last_v: tmp_v}) 

    init = tf.initialize_all_variables() 
    rand_idx = np.sort(np.random.randint(0, R_dim_1, batch_size1)) 
    rand_idx2 = np.sort(np.random.randint(0, R_dim_2, batch_size2)) 
    sess.run(init, feed_dict={idx1: rand_idx, idx2: rand_idx2}) 

    for i in range(no_epochs): 
     with tf.Graph().as_default(): 
      rand_idx1 = np.random.randint(0, R_dim_1, batch_size1) 
      rand_idx2 = np.random.randint(0, R_dim_2, batch_size2) 
      _, this_cost, tmp_u, tmp_v, tmp_cur_u, tmp_cur_v, _, _ = 
       sess.run([train_step, cost, u, v, cur_u, cur_v, change_u, change_v], 
       feed_dict={idx1: rand_idx1, idx2: rand_idx2, last_u: tmp_u, last_v: tmp_v}) 
     print this_cost 

      # find new values of U and current V but don't assign to them 
      tmp_u = np.array(tmp_u) 
      tmp_u[rand_idx] = tmp_cur_u 

      tmp_v = np.array(tmp_v) 
      tmp_v[:, rand_idx2] = tmp_cur_v