오히려 좋아..

상황이 나쁘게만 흘러가는 것 같을 때 외쳐보자.. .

궁금한 마음으로 포트폴리오 보기

Data Analysis and ML, DL/Tensorflow

Tensorflow로 구현한 Lasso, Ridge

junha6316 2020. 11. 12. 11:54
def regulRegresor(y, X, RegType, Rconst):
  regression_type = RegType # LASSO, Ridge
  num_cols = X.shape[1]
  
  x_data = tf.placeholder(shape=[None, num_cols], dtype=tf.float32, name='Input') #Input(feature)
  y_target = tf.placeholder(shape=[None,1],dtype=tf.float32, name='Output') #Output(label)

  A = tf.Variable(tf.random_normal(shape=[num_cols,1]),name =Weight) # Weight Vector
  b = tf.Variable(tf.random_normal(shape=[1, 1]),name ='Bias') # bias Vector


  with tf.Session() as sess:
      fomula = tf.add(tf.matmul(x_data,A), b, name='predict') # 회귀 식(예측값)
      
      if regression_type == 'LASSO':
          lasso_params = tf.constant(0.9, dtype=tf.float32) # limit slope
          heavyside_step = tf.truediv(1., tf.add(tf.exp(tf.multiply(tf.subtract(A, lasso_params), -50)), 1))
          regularization = tf.multiply(99., heavyside_step)
          loss = tf.add(tf.reduce_mean(tf.square(y_target-fomula)), regularization)
          
      elif regression_type == 'Ridge':
          ridge_params = tf.constant(Rconst, dtype=tf.float32)
          ridge_loss = tf.reduce_mean(tf.square(A))
          loss = tf.expand_dims(tf.add(tf.reduce_mean(tf.square(y_target-fomula)), tf.multiply(ridge_params, ridge_loss)), 0) 
     
      opt = tf.train.GradientDescentOptimizer(learning_rate=0.00000001)
      train_step = opt.minimize(loss) 
      #optimizer를 이용해 loss값을 최소화 시키는 A, b를 찾는다.

      saver = tf.train.Saver()

      init = tf.global_variables_initializer()
      init.run()

      
      loss_vec = []
      preloss =0
      
      #bootstrap 형태로 샘플 추출
      for i in range(100):
          rnd_idx = np.random.choice(len(X), size = 2500000)
          rnd_x = X.iloc[rnd_idx].values.reshape(-1,num_cols)
          rnd_y = y_return.iloc[rnd_idx].values.reshape(-1,1)
          my_dict = {x_data:rnd_x, y_target:rnd_y}
          sess.run(train_step, feed_dict=my_dict)
          temp_loss = sess.run(loss, feed_dict=my_dict)
          loss_vec.append(temp_loss[0])
          if i % 50 == 0:
            print('step={}: A={}, b={}, Loss={}'.format(i+1, A.eval(), b.eval(), temp_loss))

          
      slope = A.eval()
      cept = b.eval()
      saver.save(sess, './model.ckpt')
      return slope, cept, loss_vec

 

 

모델을 나중에 불러오려면 반드시 꼭 불러올 값마다 이름을 주어야한다.

아래 주소에서 복붙함 목적은 기록

https://woolulu.tistory.com/93