среда, 24 июля 2019 г.

Build Logistic Regression Model with TensorFlow

Build Logistic Regression Model with TensorFlow.

Build Logistic Regression Model with TensorFlow
Logistic Regression. The goal of Logistic Regression is to predict the likelihood of a given data belongs to a particular class. $$ h(\mathbf ) = \theta \left ( \sum^ _ w_i x_i \right ) = \theta \left( \mathbf ^\mathtt \mathbf \right ) $$ where the \( \mathbf ^\mathtt \mathbf \) is a hyperplane, \( \theta \) is a softmax function. Sigmoid function is used for two-class logistic regression, whereas the softmax function is used for the multiclass logistic regression. Build Model. We modularize the training model for easy replacement. First of all, the input features x and the truth label y_ are tf.placeholder() , telling tensorflow that we will supply values for those two nodes. The weight matrix W and the bias b are tf.Variable() . They will be updated after each iteration. The prediction likelihood y is calculated using a softmax function. Train Function. We use categorical cross-entropy as the loss function. TensorFlow provides an easy method to calculate the softmax between logits and labels, what only lefts is the mean value, which can be computed using the tf.reduce_mean() function. Note that a small value 1e-50 is added to the y to avoid the numerical instability problem.
Build Logistic Regression Model with TensorFlow
The weight matrix W and the bias b are updated using gradient descent. Finally, train the model by running the train_step . train_x.reshape is not necessary. It depends on how you treat your training features. Evaluate Function. The evaluate function calculates and print the accuracy. Predict Class. Besides evaluting the models, we also need a function to predict the classes of the given input. Again, the reshape is optional. Full code with working example: The data_with_labels file can be downloaded from here.

Комментариев нет:

Отправить комментарий