Last active
September 24, 2021 17:35
-
-
Save tomokishii/21cce5100452b58fcbaae079a79844c6 to your computer and use it in GitHub Desktop.
TensorFlow Logistic Regression tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# tf_logistic_reg.py | |
# date. 11/18/2015 | |
# date. 1/29/2017 - checked under tensorflow 0.12.1 | |
# | |
# The baseline is the code for 'Theano'. | |
from __future__ import print_function | |
import numpy as np | |
import tensorflow as tf | |
def prep_dataset(): | |
n_betl = np.array([59, 60, 62, 56, 63, 59, 62, 60]) | |
y_betl = np.array([6, 13, 18, 28, 52, 53, 61, 60]) | |
x1 = np.array([1.6907, 1.7242, 1.7552, 1.7842, | |
1.8113, 1.8369, 1.8610, 1.8839]) | |
# prep data arrays by expanding tabulated data | |
resp_expand = np.array([]) | |
x1_expand = np.array([]) | |
for i in range(len(n_betl)): | |
ni = n_betl[i] ; yi = y_betl[i] | |
resp_expand = np.append(resp_expand, np.repeat(0, (ni-yi))) | |
resp_expand = np.append(resp_expand, np.repeat(1, yi)) | |
x1_expand = np.append(x1_expand, np.repeat(x1[i], ni)) | |
return x1_expand, resp_expand | |
trX, trY = prep_dataset() | |
mlen = len(trY) | |
def linear_model(X, w, b): | |
return X * w + b | |
w = tf.Variable([0.]) | |
b = tf.Variable([0.]) | |
x = tf.placeholder(tf.float32, shape=(mlen)) | |
y = tf.placeholder(tf.float32, shape=(mlen)) | |
p_1 = linear_model(x, w, b) | |
x_entropy = tf.nn.sigmoid_cross_entropy_with_logits(p_1, y, name='xentropy') | |
loss = tf.reduce_mean(x_entropy, name='xentropy_mean') | |
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss) | |
init_op = tf.global_variables_initializer() | |
# Train | |
with tf.Session() as sess: | |
sess.run(init_op) | |
for i in range(100001): | |
sess.run(train_step, feed_dict={x: trX, y: trY}) | |
if i % 10000 == 0: | |
print("%8d:(w, b)=(%8.4f, %8.4f)" % (i, sess.run(w), sess.run(b))) | |
# Regression parameter will be | |
# (w, b) = 34.37, -60.71 | |
# Step = 0 | |
# 0:(w, b)=( 0.1048, 0.0525) | |
# . . . | |
# Step = 100,000 | |
# 100000:(w, b)=( 33.4332, -59.2280) | |
# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment