# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import tensorflow as tf
import numpy as np
import matplotlib.pylab as plt
# %matplotlib inline

# Create sample data
num_samples_class = 1000
positive_samples = 4 * np.random.randn(num_samples_class) + 4
negative_samples = 2 * np.random.randn(num_samples_class) - 8
x = np.concatenate((negative_samples, positive_samples), axis=0)
y = np.zeros(num_samples_class*2)
y[num_samples_class:] = 1
y_onehot = np.zeros((num_samples_class*2, 2))
y_onehot[:num_samples_class, 0] = 1
y_onehot[num_samples_class:, 1] = 1

plt.figure(figsize=(10, 10))
plt.subplot(2,1,1)
res = plt.hist(x, bins=100)
plt.subplot(2,1,2)
res = plt.hist([positive_samples, negative_samples], bins=100)

x = np.atleast_2d(x).T
print(x.shape)
print(y_onehot.shape)
print(y.shape)

# Create logistic regression model
w_init = np.random.randn(1)
print('Initial weight value = {}'.format(w_init[0]))
w = tf.Variable(w_init, dtype=tf.float32)
b = tf.Variable(1.0)
weighted_x = w * x + b
y_prob_pos = tf.nn.sigmoid(weighted_x)
y_prob_neg = 1 - y_prob_pos
y_prob = tf.concat([y_prob_neg, y_prob_pos], 1)

# Loss: MSE
loss = tf.nn.l2_loss(y_prob - y_onehot)
# Logistic accuracy
correct_prediction = tf.equal(tf.arg_max(y_prob, 1), tf.arg_max(y_onehot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# Optimizer
optimizer = tf.train.GradientDescentOptimizer(0.0001)
train = optimizer.minimize(loss)

# Main loop
accuracy_vals = []
loss_vals = []
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    init_vals = session.run([loss, w, b])
    print('Initial values: loss={}  w={}  b={}'.format(*init_vals))
    
    for step in range(150):
        print('Step {}'.format(step))
        vals = session.run([train, loss, accuracy, w, b])
        loss_val, accuracy_val = vals[1:3]
        accuracy_vals.append(accuracy_val)
        loss_vals.append(loss_val)
        print('loss={}  accuracy={}  w={}  b={}'.format(*vals[1:]))
        print()

plt.plot(accuracy_vals, label='Accuracy')
plt.legend()
plt.show()

plt.plot(loss_vals, label='Loss')
plt.legend()
plt.show()



StarcoderdataJupyterScriptsDedupFiltered