Use tensorflow to implement the elastic network regression algorithm and tensorflow Algorithm

Source: Internet
Author: User

Use tensorflow to implement the elastic network regression algorithm and tensorflow Algorithm

This article provides examples of tensorflow's implementation of the elastic network Regression Algorithm for your reference. The specific content is as follows:

Python code:

# Using tensorflow to implement an elastic network algorithm (multi-variable) # using the iris dataset, the last three features are used as features to predict the first feature. #1 import necessary programming libraries, create computing graphs, and load the dataset import matplotlib. pyplot as plt import tensorflow as tf import numpy as np from sklearn import datasets from tensorflow. python. framework import ops. get_default_graph () sess = tf. session () iris = datasets. load_iris () x_vals = np. array ([[x [1], x [2], x [3] for x in iris. data]) y_vals = np. array ([y [0] for y in iris. data]) #2 declare the learning rate, batch size, placeholders and model variables, and model output learning_rate = 0.001 ba Tch_size = 50 x_data = tf. placeholder (shape = [None, 3], dtype = tf. float32) # The placeholder size is 3 y_target = tf. placeholder (shape = [None, 1], dtype = tf. float32) A = tf. variable (tf. random_normal (shape = [3, 1]) B = tf. variable (tf. random_normal (shape = [1, 1]) model_output = tf. add (tf. matmul (x_data, A), B) #3 for an elastic network regression algorithm, the loss functions include L1 regular and L2 regular elastic_param1 = tf. constant (1 .) elastic_param2 = tf. constant (1 .) l0000a_loss = tf. objective C E_mean (abs (A) l2_a_loss = tf. performance_mean (tf. square (A) e0000term = tf. multiply (elastic_param1, l0000a_loss) e2_term = tf. multiply (elastic_param2, l2_a_loss) loss = tf. expand_dims (tf. add (tf. add (tf. performance_mean (tf. square (y_target-model_output), e1_term), e2_term), 0) #4 initialize the variable, declare the optimizer, traverse the iterative run, and obtain the init = tf parameter through training fitting. global_variables_initializer () sess. run (init) my_opt = tf. train. gradientDescentOptimizer (Learning_rate) train_step = my_opt.minimize (loss) loss_vec = [] for I in range (1000): rand_index = np. random. choice (len (x_vals), size = batch_size) rand_x = x_vals [rand_index] rand_y = np. transpose ([y_vals [rand_index]) sess. run (train_step, feed_dict = {x_data: rand_x, y_target: rand_y}) temp_loss = sess. run (loss, feed_dict = {x_data: rand_x, y_target: rand_y}) loss_vec.append (temp_loss) if (I + 1) % 250 = 0: Print ('step # '+ str (I + 1) + 'a =' + str (sess. run (A) + 'B =' + str (sess. run (B) print ('oss = '+ str (temp_loss) # As we can see, the Loss function converges after training iteration. Plt. plot (loss_vec, 'K -- ') plt. title ('oss per generation') plt. xlabel ('generation') plt. ylabel ('loss') plt. show ()

This document provides a reference to the Tensorflow Machine Learning Practice Guide.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.