daily pastebin goal
31%
SHARE
TWEET

Untitled

redmage123 Oct 12th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. import tensorflow as tf
  3. import numpy as np
  4. import pandas as pd
  5. import matplotlib.pyplot as plt
  6. from sklearn import model_selection
  7. import sys
  8.  
  9. gender_df = pd.read_csv('data/binary_data.csv')
  10.  
  11. # Shuffle our data
  12. gender_df = gender_df.sample(frac=1).reset_index(drop=True)
  13.  
  14. # We'll go ahead and split the data set into training and testing parts.  
  15. # 70 per cent will go to training, the rest to testing.
  16. train_x,test_x, train_y, test_y = model_selection.train_test_split(gender_df['HEIGHT'],gender_df['GENDER'],test_size = 0.3)
  17.  
  18. n_samples = train_x.shape[0]
  19.  
  20. # These will be the placeholders for the testing and training data
  21. x = tf.placeholder(tf.float32)
  22. y = tf.placeholder(tf.float32)
  23.  
  24. # Variables for the weight and bias.
  25. W = tf.Variable(0,dtype = tf.float32)
  26. b = tf.Variable(0,dtype = tf.float32)
  27.  
  28. # This is our activation function to determine the probability
  29. # of our gender based on height.
  30. activation = tf.nn.sigmoid((W * x) + b)
  31.  
  32. # Set our alpha value for the optimizer.
  33. learning_rate = 0.001
  34.  
  35. # cross_entropy is our cost function.
  36. cross_entropy = tf.reduce_mean(-(y*tf.log(activation) + (1 - y) * tf.log(1-activation)))
  37.  
  38. # We'll use a standard gradient descent optimizer.  
  39. train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)
  40.  
  41. with tf.Session() as sess:
  42.     sess.run(tf.global_variables_initializer())
  43.  
  44. # Now train our jodel.
  45.     for epoch in range(1000):
  46.         _,l = sess.run([train_step, cross_entropy], feed_dict = {x: train_x, y:train_y})
  47.     if epoch % 50 == 0:
  48.             print ('loss = %f' %(l))
  49.  
  50. # Now let's see how our model performed on the test data.
  51.     correct = tf.equal(tf.argmax(activation,1), tf.argmax(y,1))
  52.     accuracy = tf.reduce_mean(tf.cast(correct,'float'))
  53.     print ('Accuracy: ', sess.run(accuracy,feed_dict = {x: test_x, y:test_y}))
RAW Paste Data
Top