ImportTensorFlow as TFImportNumPy as NPImportMatplotlib.pyplot as Plt fromSklearnImportdatasetssess=TF. Session ()#loading the iris setiris=Datasets.load_iris ()#width LengthX_vals=np.array ([x[3] forXinchIris.data]) Y_vals=np.array ([x[0] forXinchIris.data]) Learning_rate=0.05batch_size=25X_data=tf.placeholder (shape=[none,1],dtype=tf.float32) Y_data=tf.placeholder (shape=[none,1],dtype=Tf.float32) A=TF. Variable (Tf.random_normal (shape=[1,1])) b=TF. Variable (Tf.random_normal (shape=[1,1]))#Add linear Model Y=ax+b X*a==>shape (none,1) +b==>shape (none,1)model_out=Tf.add (Tf.matmul (x_data,a), b)#declaring the L2 loss functionLoss=tf.reduce_mean (Tf.square (y_data-model_out))#Initialize Variablesinit=Tf.global_variables_initializer () sess.run (init)#Gradient Descentmy_opt=Tf.train.GradientDescentOptimizer (learning_rate) Train_step=my_opt.minimize (loss)#Loop IterationLoss_rec=[] forIinchRange (100): Rand_index=np.random.choice (Len (x_vals), size=batch_size)#shape (none,1)rand_x=Np.transpose ([X_vals[rand_index]]) rand_y=Np.transpose ([Y_vals[rand_index]])#RunSess.run (train_step,feed_dict={x_data:rand_x,y_data:rand_y}) Temp_loss=sess.run (loss,feed_dict={x_data:rand_x,y_data:rand_y})#Add a recordloss_rec.append (Temp_loss)#Print if(i+1)%25==0:Print('Step:%d a=%s b=%s'%(I,str (Sess.run (A)), str (Sess.run (b) )))Print('loss:%s'%str (temp_loss))#decimation Factor[slope]=Sess.run (A)Print(slope) [Intercept]=Sess.run (b) Best_fit=[] forIinchX_vals:best_fit.append (Slope*i+intercept)#x_vals shape (none,1)Plt.plot (X_vals,y_vals,'o', label='Data') Plt.plot (X_vals,best_fit,'R', label='Best fit Line', linewidth=3) plt.legend (Loc='Upper Left') Plt.xlabel ('Pedal Width') Plt.ylabel ('Pedal Length') plt.show ()#L2Plt.plot (Loss_rec,'k -', label='Loss') Plt.title ('L2 loss per Generation') Plt.xlabel ('Generation') Plt.ylabel ('L2 Loss') plt.show ()
TensorFlow (c) linear regression algorithm for L2 regular loss function with TensorFlow