Below you will find neural network code for blog post Artificial Intelligence – Neural Networks Applications in Seismic Prospecting. This post is showing how seismic process can be automated using deep neural network such as simple Multi Layer perceptron.
import numpy as np import tensorflow as tf import pandas as pd import matplotlib.pyplot as plt import pickle import math ##############################FUNCTIONS USED############################################ def data_analysis(): X = pd.read_csv('X_new.csv') Y = pd.read_csv('Y_new.csv') X_train=X.values X_train=X_train[0:119,:] Y_train=Y.values Y_train=Y_train[0:119,:] return X_train.T,Y_train.T def random_mini_batches(X_train,Y_train,minibatch_size): m=X_train.shape[1] mini_batches=[] num_complete_minibatches=int(math.floor(m/minibatch_size)) for k in range(0,num_complete_minibatches): minibatch_X=X_train[:,k*minibatch_size:(k+1)*minibatch_size] minibatch_Y=Y_train[:,k*minibatch_size:(k+1)*minibatch_size] mini_batch=(minibatch_X,minibatch_Y) mini_batches.append(mini_batch) if m % minibatch_size !=0: minibatch_X=X_train[:,num_complete_minibatches*minibatch_size:] minibatch_Y=Y_train[:,num_complete_minibatches*minibatch_size:] mini_batch=(minibatch_X,minibatch_Y) mini_batches.append(mini_batch) return mini_batches def init_par(layer_dims): L=len(layer_dims) parameters={} for l in range(1,L): parameters["W"+str(l)]=tf.get_variable("W"+str(l),[layer_dims[l],layer_dims[l-1]],initializer=tf.contrib.layers.xavier_initializer()) parameters["b"+str(l)]=tf.get_variable("b"+str(l),[layer_dims[l],1],initializer=tf.zeros_initializer()) return parameters def forward(X,parameters,layer_dims): outputs={} outputs["A0"]=X L=len(layer_dims) for l in range(1,L-1): outputs["Z"+str(l)] =tf.add(tf.matmul(parameters["W"+str(l)],outputs["A"+str(l-1)]),parameters["b"+str(l)]) outputs["A"+str(l)]=tf.nn.relu(outputs["Z"+str(l)]) outputs["Z"+str(L-1)]=tf.add(tf.matmul(parameters["W"+str(L-1)],outputs["A"+str(L-2)]),parameters["b"+str(L-1)]) outputs["A"+str(L-1)]=tf.nn.relu(outputs["Z"+str(L-1)]) return outputs["A"+str(L-1)] def model(X_train, Y_train, layer_dims, learning_rate, num_epochs, minibatch_size,print_cost): n_x=X_train.shape[0] m=X_train.shape[1] n_y=Y_train.shape[0] costs=[] X=tf.placeholder(tf.float32,[n_x,None],name="X") Y=tf.placeholder(tf.float32,[n_y,None],name="Y") parameters=init_par(layer_dims) Z=forward(X,parameters,layer_dims) cost=tf.reduce_sum(tf.square(Z-Y))/m optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) init=tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for epoch in range(num_epochs): epoch_cost=0. num_minibatches=int(m/minibatch_size) minibatches=random_mini_batches(X_train,Y_train,minibatch_size) for minibatch in minibatches: (minibatch_X,minibatch_Y)=minibatch _,minibatch_cost = sess.run([optimizer,cost], feed_dict={X:minibatch_X,Y:minibatch_Y}) epoch_cost+=minibatch_cost/num_minibatches if print_cost == True and epoch % 100 == 0: print ("Cost after epoch %i: %f" % (epoch, epoch_cost)) if print_cost == True and epoch % 5 == 0: costs.append(epoch_cost) plt.plot(np.squeeze(costs)) plt.ylabel('cost') plt.xlabel('iterations (per tens)') plt.title("Learning rate =" + str(learning_rate)) plt.show() parameters=sess.run(parameters) print("TRAINED!!") #print("Train Accuracy : ", cost.eval({X: X_train, Y: Y_train}) return parameters ########################################################################################### ########################################################################################### ########################################################################################### X_train,Y_train=data_analysis() #file from which data is input n_x=X_train.shape[0] m=X_train.shape[1] n_y=Y_train.shape[0] layer_dims=[n_x,150,100,n_y] learning_rate=0.00001 num_epochs = 50000 minibatch_size = m parameters=model(X_train, Y_train, layer_dims, learning_rate, num_epochs, minibatch_size, print_cost=True) with open("parameters.txt", "wb") as myFile: pickle.dump(parameters, myFile)
References
1. Artificial Intelligence – Neural Networks Applications in Seismic Prospecting