avr

12

Posted by : admin | On : 12 avril 2018

 

https://www.youtube.com/watch?v=vq2nnJ4g6N0&t=6787s

https://www.youtube.com/watch?v=u4alGiomYP4

https://www.youtube.com/watch?v=fTUwdXUFfI8

https://github.com/random-forests/tutorials/blob/master/ep7.ipynb

Dataset :

http://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data

https://github.com/selva86/datasets/blob/master/Sonar.csv

 

 

support material

https://www.edureka.co/blog/perceptron-learning-algorithm/

https://github.com/tensorflow/tensorflow

https://www.edureka.co/blog/deep-learning-tutorial?utm_source=youtube&utm_campaign=deep-learning-180717-wr&utm_medium=description

Sample Sonar and mine

https://www.edureka.co/blog/perceptron-learning-algorithm/

* data

https://www.kaggle.com/mattcarter865/mines-vs-rocks/data

https://www.youtube.com/watch?v=yX8KuPZCAMo

 

SOURCE  : Youtube link

https://www.youtube.com/watch?v=yX8KuPZCAMo

 

example 1

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0 , tf.float32)
#print (node1,node)

sess = tf.Session()
#print(sess.run([node1,node2]))
#sess.close()

with tf.Session() as sess :
File_Writer = tf.summary.FileWriter(‘C:\\Users\\aartaud\\pycharmproject\\Tensorflow\\graph’,sess.graph)
 output =sess.run([node1,node2])
 print(output)

 

cd C:\\Users\\aartaud\\pycharmproject\\

tensorboard -logdir= « TensorFlow »

check localhost:606

(computation graphe only no result is done, just for understanding the computation)

 

example 2

import tensorflow as tf
a = tf.placeholder(tf.float32)
b = tf.placefolder(tf.float32)

adder_node = a + b 

sess = tf.Session()

print(sess.run(adder_node,{a:[1,3],b:[2,4]})

example 3

import tensorflow as tf 

#Model Parameters.
W = tf.Variable([.3],tf.float32)
b = tf.Variable([-.3],tf.float32)

 # Inputs and outputs
x = tf.placeholder(tf.float32)

linear_model = W * x + b
y= tf.placeholder(tf.float32)

#loss function
squared_delta = tf.square(linear_model – y)
loss = tf.reduce_sum(squared_delta)

#Optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimzer.minimize(loss)

init = tf.global_variable_initializer()

sess = tf.Session()
sess.run(init)

for i in range (1000)
   sess.run(train , {x:[1,2,3,4],y:[0,-1,-2,-3]}  ) 

print(sess.run([W,b]))
#print(sess.run(loss,{x:[1,2,3,4],y:[0,-1,-2,-3]}
#build the model , calculate loss and train the model

example 4

 

 

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split

#reading the dataset
def read_dataset() :
      df = pd.read_csv(«C:\\users\aartaud\pycharmProjects\\Tensorflow\sonar.csv » )
      #print(len(df.columns))
      X = df[df.columns[0:60]].values
      y = df[df.columns[60]]

      #Encode the dependant variable
      encode = LabelEncoder()
     encoder.fit(y)
     y=encoder.transform(y)
     Y = one_hot_encode(y)
     print(X.shape)
     return(X,Y)

#define the encoder function
def one_hot_encode(labels) :
     n_labels=len(labels)
     n_unique_labels = len(np.unique(labels))
     one_hot_encode = np.zeros((n_labels , n_unique_labels ) )
     one_hot_encode[np.arrange(n_labels),labels] = 1
     return one_hot_encode

#read the dataset
X, Y = read_dataset()

#shuffle the dataset to mix up the rows
X, Y =shuffle(X,Y, random_state=1)

#convert the dataset into train and test part
train_x , test_x , train_y, test_y = train_test_split(X,Y,test_size=0.20,random_state=415)

#inspect the shape of the training and testing
print(train_x.shape)
print(train_y.shape)
print(test_x.shape)

#define the important parameters and variable to work with the tensors
learning_rate = 0.3
training_epoch =1000 # number  of iteration to minimize the error
cost_history = np.empty(shape[1], dtype=float)
n_dim = X.shape[1]

print(« n_dim »,n_dim)
n_class  = 2 #mine and rock so its 2
model_path = «C:\\users\\aartaud\\PycharmProjects\\Tensorflow\\NMI »

# define the number of hidden layers and number of neurons for each layer
n_hidden_1 = 60
n_hidden_2 = 60
n_hidden₃  = 60

x= tf.placefolder(tf.float32,[None,n_dim])
W= tf.Variable(tf.zeos[n_dim,n_class]))
 b=tf.variable(tf.zeros[n_class]))
y_ = tf.placeholder(tf.float32,[None,n_class])

# define the model
def multilayer_perceptron (x,weights, biases ) :
      #hidden layer with RELU activaction fct
      layer₁  = tf.add(tf.matmul(x,weight[‘h1’] , biases[‘b1’])
      layer₁ = tf.nn.sigmoid (layer₁) #actiation function 

#hidden layer with sigmoid activation
layer₂ = tf.add(tf.matmul(layer₁,weights[‘h2’],biases[‘b2’])
layer₂ = tf.nn.sigmoid(layer₂) 

#hidden layer with sigmoid activation
layer₃ = tf.add(tf.matmut(layer₂,weights[‘h3’]),biases[‘b3’])
layer₃ = tf.nn.sigmoid(layer₃)

#hidden layer with RELU activation
layer4 = tf.add(tf.matmul(layer₃,weight[‘h4’],biases[‘b4’])
layer₄ = tf.nn.relu(layer₄)

#outpur layer with the linear activation
out_layer   = tf.matmul(layer₄  , weight[‘out’] + biases[‘out’]
return out_layer

# Define the weights and biases for each layer 

weights = {
 ‘h1’ : tf.Variable(tf.truncated_normal([n_dim,n_hidden₁])
‘h2’ : tf.Variable(tf.truncated_normal([n_hidden₁,n_hidden2])),
‘h3’ : tf.Variable(tf.truncated_normal([n_hidden₂,n_hidden₃])),
‘h4’:tf.Variable(tf.truncated_normal([n_hidden₃,n_hidden4])),
‘out’:tf.Variable(tf.truncated_normal([n_hidden₄,n_class]))
}

biases = {
  ‘b1’ : tf.Variable(tf.truncated_normal([n_hidden₁])),
  ‘b2’ : tf.Variable(tf.truncated_normal(n_hidden₂])),
  ‘b3’ : tf.Variable(tf.truncated_normal(n_hidden3])),
  ‘b4’ : tf.Variable(tf.truncated_normal(n_hidden4])),
 ‘out’ : tf.Variable(tf.truncated_normal([n_class]))
}
#initialize all variable
init = tf.global_variable_initializer()

saver  = tf.train.Saver()

#call your model deined
y = multilayer_perceptron(x,weights,biases)

#define the cost function and optimizer
cost_function = tf.reduce.mean(tf.softmax_cross_entropy_with_logits(logit=y,labels =y_))
training_step  = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)

sess = tf.Session()
sess.run(init)

#calculate the cost and the accuracy for each epoch

mse_history = []
accuracy_history = []

for epoch in range(training_epochs) :
      sess.run(training_step,feed_dict{x:  train_x ,  y_ = train_y})
      cost = sess.run(cost_function , feed_dict{x:  train_x ,  y_ = train_y})
      cost_history = np.append(cost_history,cost)
      correct_prediction = tf.equal(tf.argmax(y,1) , tf.argmax(y_,1))
      #difference between correct output and the model one 

      accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
      #print(«Accuracy :  », (sess.run(accuracy,feed_dict={x : test_x , y_:test_y})))

     pred_y= sess.run(y,feed_dict={x:test_x})
     mse  = tf.reduce_mean(tf.square(pred_y -test_y))
     mse_ =sess.run(mse)
     mse_history.append(mse₎
     accuracy = (sess.run(accuracy, feed_dict={x:train_x, y:train_y} ))
     accuracy_history.append(accuracy)

     print(‘epoch : ’,epoch, ‘ - ’ , ‘cost : ‘ , cost , « -MSE : »,  mse_ , «  - Train Accuracy »,accuracy)

save_path = saver.save(sess, model_path)
print(«Model saved in file %s » % save_path)

#plot mse and accuracy graph 

plt.plot(mse_history, ‘r’)
plt.show()
plt.plot(accuracy_history)  

#print the final accuracy 

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_ , 1 ))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(« Test accuracy : » , (sess.run(accuracy,feed_dict={x:test_x, y_: test_y})))

# Print the final mean square error
pred_y = sess.run(y,feed_dict={x, test_x})
mse=tf.reduce_mean(tf.square(pred_y, test_y))
print(« MSE : %.4f »% sess.run(mse))

 

 

Restore the model

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import random 

### READ CSV Function ####

X,Y,y1  = read_dataset()
model_path = » C:\\users\\aartaud\\PycharmProjects\\Tensorflow\\NMI»
learning_rate = 0.3
training_epoch = 1000
cost_history = np.empty (shape=[1],dtype =float )
n_dim = 60
n_class = 2  

init=tf.global_variable_initializer()
saver = tf.train.Saver()
sess = tf.Session()
sess.run(init)
saver.restore(sess,model_path) #HERE IT IS !!!

prediction = tf.argmax(y,1)
correct_prediction = tf.equal(prediction, tf.argmax(y_,1))
accuract = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#print (accuracy_run)
print (‘ *************************************’)
print(« 0 stands for a M i.e. Mine & 1 stand for a rock » )
print(«***************************** »)
for i in range (93,101)
     prediction_run=sess.run(prediction,feed_dict={x : X[i].reshape(1,60}
     accuracy_run = sess.run(accuracy, feed_dict={x : X[i].reshape(1,60),y_ :}
     print(«original class : » , y1[i] , « predicted values : » , prediction_run)