Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. from numpy import genfromtxt
  4.  
  5. data = genfromtxt('cs-training.csv',delimiter=',')
  6.  
  7. x = tf.placeholder("float", [None, 11])
  8. W = tf.Variable(tf.zeros([11,2]))
  9. b = tf.Variable(tf.zeros([2]))
  10.  
  11. y = tf.nn.softmax(tf.matmul(x,W) + b)
  12. y_ = tf.placeholder("float", [None,2])
  13.  
  14. cross_entropy = -tf.reduce_sum(y_*tf.log(y))
  15.  
  16. train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
  17.  
  18. init = tf.initialize_all_variables()
  19.  
  20. sess = tf.Session()
  21. sess.run(init)
  22.  
  23. for i in range(1000):
  24. batch_xs, batch_ys = data.train.next_batch(100)
  25. sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
  26.  
  27. ---------------------------------------------------------------------------
  28. AttributeError Traceback (most recent call last)
  29. <ipython-input-128-b48741faa01b> in <module>()
  30. 1 for i in range(1000):
  31. ----> 2 batch_xs, batch_ys = data.train.next_batch(100)
  32. 3 sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
  33.  
  34. AttributeError: 'numpy.ndarray' object has no attribute 'train'
  35.  
  36. [[1,0.766126609,45,2,0.802982129,9120,13,0,6,0,2],
  37. [0,0.957151019,40,0,0.121876201,2600,4,0,0,0,1],
  38. [0,0.65818014,38,1,0.085113375,3042,2,1,0,0,0],
  39. [0,0.233809776,30,0,0.036049682,3300,5,0,0,0,0]]
  40.  
  41. #!/usr/bin/env python
  42. import tensorflow as tf
  43. import numpy as np
  44. from numpy import genfromtxt
  45.  
  46. # Build Example Data is CSV format, but use Iris data
  47. from sklearn import datasets
  48. from sklearn.cross_validation import train_test_split
  49. import sklearn
  50. def buildDataFromIris():
  51. iris = datasets.load_iris()
  52. X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.33, random_state=42)
  53. f=open('cs-training.csv','w')
  54. for i,j in enumerate(X_train):
  55. k=np.append(np.array(y_train[i]),j )
  56. f.write(",".join([str(s) for s in k]) + 'n')
  57. f.close()
  58. f=open('cs-testing.csv','w')
  59. for i,j in enumerate(X_test):
  60. k=np.append(np.array(y_test[i]),j )
  61. f.write(",".join([str(s) for s in k]) + 'n')
  62. f.close()
  63.  
  64.  
  65. # Convert to one hot
  66. def convertOneHot(data):
  67. y=np.array([int(i[0]) for i in data])
  68. y_onehot=[0]*len(y)
  69. for i,j in enumerate(y):
  70. y_onehot[i]=[0]*(y.max() + 1)
  71. y_onehot[i][j]=1
  72. return (y,y_onehot)
  73.  
  74.  
  75. buildDataFromIris()
  76.  
  77.  
  78. data = genfromtxt('cs-training.csv',delimiter=',') # Training data
  79. test_data = genfromtxt('cs-testing.csv',delimiter=',') # Test data
  80.  
  81. x_train=np.array([ i[1::] for i in data])
  82. y_train,y_train_onehot = convertOneHot(data)
  83.  
  84. x_test=np.array([ i[1::] for i in test_data])
  85. y_test,y_test_onehot = convertOneHot(test_data)
  86.  
  87.  
  88. # A number of features, 4 in this example
  89. # B = 3 species of Iris (setosa, virginica and versicolor)
  90. A=data.shape[1]-1 # Number of features, Note first is y
  91. B=len(y_train_onehot[0])
  92. tf_in = tf.placeholder("float", [None, A]) # Features
  93. tf_weight = tf.Variable(tf.zeros([A,B]))
  94. tf_bias = tf.Variable(tf.zeros([B]))
  95. tf_softmax = tf.nn.softmax(tf.matmul(tf_in,tf_weight) + tf_bias)
  96.  
  97. # Training via backpropagation
  98. tf_softmax_correct = tf.placeholder("float", [None,B])
  99. tf_cross_entropy = -tf.reduce_sum(tf_softmax_correct*tf.log(tf_softmax))
  100.  
  101. # Train using tf.train.GradientDescentOptimizer
  102. tf_train_step = tf.train.GradientDescentOptimizer(0.01).minimize(tf_cross_entropy)
  103.  
  104. # Add accuracy checking nodes
  105. tf_correct_prediction = tf.equal(tf.argmax(tf_softmax,1), tf.argmax(tf_softmax_correct,1))
  106. tf_accuracy = tf.reduce_mean(tf.cast(tf_correct_prediction, "float"))
  107.  
  108. # Initialize and run
  109. init = tf.initialize_all_variables()
  110. sess = tf.Session()
  111. sess.run(init)
  112.  
  113. print("...")
  114. # Run the training
  115. for i in range(30):
  116. sess.run(tf_train_step, feed_dict={tf_in: x_train, tf_softmax_correct: y_train_onehot})
  117.  
  118. # Print accuracy
  119. result = sess.run(tf_accuracy, feed_dict={tf_in: x_test, tf_softmax_correct: y_test_onehot})
  120. print "Run {},{}".format(i,result)
  121.  
  122.  
  123. """
  124. Below is the ouput
  125. ...
  126. Run 0,0.319999992847
  127. Run 1,0.300000011921
  128. Run 2,0.379999995232
  129. Run 3,0.319999992847
  130. Run 4,0.300000011921
  131. Run 5,0.699999988079
  132. Run 6,0.680000007153
  133. Run 7,0.699999988079
  134. Run 8,0.680000007153
  135. Run 9,0.699999988079
  136. Run 10,0.680000007153
  137. Run 11,0.680000007153
  138. Run 12,0.540000021458
  139. Run 13,0.419999986887
  140. Run 14,0.680000007153
  141. Run 15,0.699999988079
  142. Run 16,0.680000007153
  143. Run 17,0.699999988079
  144. Run 18,0.680000007153
  145. Run 19,0.699999988079
  146. Run 20,0.699999988079
  147. Run 21,0.699999988079
  148. Run 22,0.699999988079
  149. Run 23,0.699999988079
  150. Run 24,0.680000007153
  151. Run 25,0.699999988079
  152. Run 26,1.0
  153. Run 27,0.819999992847
  154. ...
  155.  
  156. Ref:
  157. https://gist.github.com/mchirico/bcc376fb336b73f24b29#file-tensorflowiriscsv-py
  158. """
  159.  
  160. x = tf.placeholder("float", [None, 11])
  161. y_ = tf.placeholder("float", [None,2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement