Advertisement
Guest User

logistic

a guest
May 25th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. import numpy as np
  2. import tensorflow as tf
  3.  
  4.  
  5. def file_len(fname):
  6.     with open(fname) as f:
  7.         for i, l in enumerate(f):
  8.             pass
  9.  
  10.     return i + 1
  11.  
  12.  
  13. def convertData(digit):
  14.     one_hot = np.zeros([10])
  15.     one_hot[int(digit)] = 1
  16.  
  17.     return one_hot
  18.  
  19.  
  20. def getData(filename):
  21.     file_name_queue = tf.train.string_input_producer([filename])
  22.     reader = tf.TextLineReader()
  23.     key, value = reader.read(file_name_queue)
  24.     record_defaults = np.zeros([401, 1]).tolist()
  25.     data = tf.decode_csv(value, record_defaults=record_defaults)
  26.     features = tf.stack(data[:400])
  27.     labels = tf.stack(data[400])
  28.  
  29.     x = np.array([])
  30.     y = np.array([])
  31.     with tf.Session() as sess:
  32.         coord = tf.train.Coordinator()
  33.         threads = tf.train.start_queue_runners(coord=coord)
  34.  
  35.         for i in range(file_len(filename)):
  36.             tx, ty = sess.run([features, labels])
  37.             if ty == -1:
  38.                 continue
  39.             ty = convertData(ty)
  40.             if len(x) == 0:
  41.                 x = np.array([tx])
  42.                 y = np.array([ty])
  43.             else:
  44.                 x = np.vstack((x, tx))
  45.                 y = np.vstack((y, ty))
  46.  
  47.         coord.request_stop()
  48.         coord.join(threads)
  49.  
  50.     return (x, y)
  51.  
  52.  
  53. def sigmoid(z):
  54.     exp_node = tf.exp(z)
  55.     adder_node = 1 + exp_node
  56.     result_node = 1.0 / adder_node
  57.  
  58.     return result_node
  59.  
  60.  
  61. def logistic():
  62.     W = tf.Variable(tf.zeros([40,40]), tf.float32)
  63.     b = tf.Variable(tf.zeros([40]), tf.float32)
  64.     x = tf.placeholder(tf.float32, [None, 40], name="x")
  65.  
  66.     linear_model = W * x + b
  67.     hypothesis = sigmoid(linear_model)
  68.  
  69.     y = tf.placeholder(tf.float32)
  70.  
  71.     cost = -tf.reduce_mean(y * tf.log(hypothesis) +
  72.                            (1 - y) * tf.log(1 - hypothesis))
  73.  
  74.     optimizer = tf.train.GradientDescentOptimizer(0.1)
  75.     train = optimizer.minimize(cost)
  76.  
  77.     x_train = [3, 3.5, 4.1, 4.3, 5.0, 5.2, 5.6, 6.0, 6.1, 6.5, 7.6, 7.9, 9.2, 9.9, 10.2]
  78.     y_train = [0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1]
  79.  
  80.     tf.summary.FileWriter("logisticGraph", tf.InteractiveSession().graph)
  81.  
  82.     init = tf.global_variables_initializer()
  83.     sess = tf.Session()
  84.     sess.run(init)
  85.  
  86.     for i in range(20000):
  87.         sess.run(train, {x: x_train, y: y_train})
  88.  
  89.         # evaluate training accuracy
  90.         if i % 1000 == 0:
  91.             curr_W, curr_b, curr_loss = sess.run([W, b, cost], {x: x_train, y: y_train})
  92.             print("W: %s b: %s loss: %s" % (curr_W, curr_b, curr_loss))
  93.  
  94.     check = sess.run(hypothesis, {x: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.5, 7.0, 8.0, 9.0, 10.0]})
  95.     print(check)
  96.  
  97.  
  98. if __name__ == "__main__":
  99.     a, b = getData("data.csv")
  100.     print(a.shape)
  101.     print(b.shape)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement