<jupyter_start><jupyter_text>linear regression<jupyter_code>import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>At the beginning, we can compute the following equation: $Y = WX + b$, where $W$ and $X$ are random matrices and b is a random vector.<jupyter_code># Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 25
# Training Data
train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]
# define tf Graph Input
X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")

# set model weights and bais
W = tf.Variable(np.random.randn(), name = "weight")
b = tf.Variable(np.random.randn(), name = "bais")<jupyter_output><empty_output><jupyter_text>Then we will use equation: $Y = WX + b$ to construct a linear model<jupyter_code>Y_pre = tf.add(tf.multiply(X, W), b)<jupyter_output><empty_output><jupyter_text>We can use several Algorithms to compute cost$$ J = - \frac{1}{m}  \sum_{i = 1}^m  \large ( \small y^{(i)} \log a^{ [2] (i)} + (1-y^{(i)})\log (1-a^{ [2] (i)} )\large )\small\tag{2}$$<jupyter_code>logits = tf.sigmoid(Y_pre)
cost_coss = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=Y)<jupyter_output><empty_output><jupyter_text>we can use mean squared error to compute cost$$ cost = \frac{1}{2*n}  \sum_{i = 1}^m  ( \small y^{(i)} - y)^{2}\small\tag{2}$$<jupyter_code>cost_mean = tf.reduce_mean(tf.pow((Y_pre - Y), 2))/(2 * n_samples)
# Gradient descent, we use cost_mean for example
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_mean)
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
# start training
with tf.Session() as sess:
    sess.run(init)
    
    # feed all train data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X:x, Y:y})
            
            # display logs per epoch step
            if((epoch + 1) % display_step == 0):
                c = sess.run(cost_mean, feed_dict={X:train_X, Y:train_Y})
                print "Epoch:", "%04d" % (epoch + 1), "cost = ", "{:.9f}".format(c),\
                      "W = ", sess.run(W), "b = ", sess.run(b)
                    
    print "Optimization Finished!"
    train_cost = sess.run(cost_mean, feed_dict={X:train_X, Y:train_Y})
    print "train_cost = ", train_cost, "W = ", sess.run(W), "b = ", sess.run(b), '\n'
    
    #Graphic display
    plt.plot(train_X, train_Y, 'ro', label = 'train data')
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label = 'fitted line')
    plt.legend()
    plt.show()<jupyter_output>Epoch: 0050 cost =  0.004997436 W =  0.300368 b =  0.440139
Epoch: 0050 cost =  0.004993225 W =  0.302951 b =  0.440727
Epoch: 0050 cost =  0.004993116 W =  0.302896 b =  0.440717
Epoch: 0050 cost =  0.005009590 W =  0.305726 b =  0.441138
Epoch: 0050 cost =  0.004992351 W =  0.302196 b =  0.440629
Epoch: 0050 cost =  0.004992532 W =  0.301885 b =  0.440554
Epoch: 0050 cost =  0.004992663 W =  0.301731 b =  0.440538
Epoch: 0050 cost =  0.004992894 W =  0.302786 b =  0.440709
Epoch: 0050 cost =  0.004992459 W =  0.301854 b =  0.440586
Epoch: 0050 cost =  0.004992228 W =  0.302015 b =  0.440661
Epoch: 0050 cost =  0.004993400 W =  0.30309 b =  0.440813
Epoch: 0050 cost =  0.004992515 W =  0.301525 b =  0.440668
Epoch: 0050 cost =  0.004996782 W =  0.300298 b =  0.440437
Epoch: 0050 cost =  0.004995449 W =  0.30059 b =  0.440474
Epoch: 0050 cost =  0.004992603 W =  0.301521 b =  0.440638
Epoch: 0050 cost =  0.004998659 W =  0.299909 b =  0.440465
Epoch: 0050 cost =  0.004999533 W =  0.299[...]
{'fraction_non_alphanumeric': 0.15737455651292448, 'fraction_numerical': 0.15509376583882412, 'mean_word_length': 3.23952738990333, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '71127', 'license': 'MIT', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1640, 'n_words': 371, 'path': 'tensorFlow-examples/notebooks/3_Models/linear_regression.ipynb', 'repo_name': 'chenghuiyu/DeepLearning-Examples', 'size': '3946'}