<jupyter_start><jupyter_text>No.1 Tensorflow Programming Import modules.<jupyter_code>import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import multivariate_normal, permutation
import pandas as pd
from pandas import DataFrame, Series<jupyter_output><empty_output><jupyter_text>Generate a training set.<jupyter_code>np.random.seed(20160512)

n0, mu0, variance0 = 20, [10, 11], 20
data0 = multivariate_normal(mu0, np.eye(2)*variance0 ,n0)
df0 = DataFrame(data0, columns=['x1','x2'])
df0['t'] = 0

n1, mu1, variance1 = 15, [18, 20], 22
data1 = multivariate_normal(mu1, np.eye(2)*variance1 ,n1)
df1 = DataFrame(data1, columns=['x1','x2'])
df1['t'] = 1

df = pd.concat([df0, df1], ignore_index=True)
train_set = df.reindex(permutation(df.index)).reset_index(drop=True)<jupyter_output><empty_output><jupyter_text>Show data in the training set.<jupyter_code>train_set<jupyter_output><empty_output><jupyter_text>Extract (x1, x2) and t separately into NumPy array objects.<jupyter_code>train_x = train_set[['x1','x2']].as_matrix()
train_t = train_set['t'].as_matrix().reshape([len(train_set), 1])<jupyter_output><empty_output><jupyter_text>Define a relationship to calculate probability p of t=1 for training set.<jupyter_code>x = tf.placeholder(tf.float32, [None, 2])
w = tf.Variable(tf.zeros([2, 1]))
w0 = tf.Variable(tf.zeros([1]))
f = tf.matmul(x, w) + w0
p = tf.sigmoid(f)<jupyter_output><empty_output><jupyter_text>Define a loss function and a training algorithm (train\_step).<jupyter_code>t = tf.placeholder(tf.float32, [None, 1])
loss = -tf.reduce_sum(t*tf.log(p) + (1-t)*tf.log(1-p))
train_step = tf.train.AdamOptimizer().minimize(loss)<jupyter_output><empty_output><jupyter_text>Defina an accuracy.<jupyter_code>correct_prediction = tf.equal(tf.sign(p-0.5), tf.sign(t-0.5))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))<jupyter_output><empty_output><jupyter_text>Prepare a session and initialize Variables.<jupyter_code>sess = tf.Session()
sess.run(tf.global_variables_initializer())<jupyter_output><empty_output><jupyter_text>Iterate the optimization using the gradient decent for 20,000 times.<jupyter_code>i = 0
for _ in range(20000):
    i += 1
    sess.run(train_step, feed_dict={x:train_x, t:train_t})
    if i % 2000 == 0:
        loss_val, acc_val = sess.run(
            [loss, accuracy], feed_dict={x:train_x, t:train_t})
        print ('Step: %d, Loss: %f, Accuracy: %f'
               % (i, loss_val, acc_val))<jupyter_output>Step: 2000, Loss: 15.165894, Accuracy: 0.885714
Step: 4000, Loss: 10.772635, Accuracy: 0.914286
Step: 6000, Loss: 8.197757, Accuracy: 0.971429
Step: 8000, Loss: 6.576121, Accuracy: 0.971429
Step: 10000, Loss: 5.511984, Accuracy: 0.942857
Step: 12000, Loss: 4.798029, Accuracy: 0.942857
Step: 14000, Loss: 4.314191, Accuracy: 0.942857
Step: 16000, Loss: 3.986273, Accuracy: 0.942857
Step: 18000, Loss: 3.766507, Accuracy: 0.942857
Step: 20000, Loss: 3.623062, Accuracy: 0.942857<jupyter_text>Extract the parameter values.<jupyter_code>w0_val, w_val = sess.run([w0, w])
w0_val, w1_val, w2_val = w0_val[0], w_val[0][0], w_val[1][0]
print w0_val, w1_val, w2_val<jupyter_output>-15.6304 0.560301 0.492597<jupyter_text>Draw a graph with the extracted parameter values.<jupyter_code>train_set0 = train_set[train_set['t']==0]
train_set1 = train_set[train_set['t']==1]

fig = plt.figure(figsize=(6,6))
subplot = fig.add_subplot(1,1,1)
subplot.grid(False)
subplot.set_ylim([0,30])
subplot.set_xlim([0,30])
subplot.scatter(train_set1.x1, train_set1.x2, marker='o')
subplot.scatter(train_set0.x1, train_set0.x2, marker='x')

linex = np.linspace(0,30,10)
liney = - (w1_val*linex/w2_val + w0_val/w2_val)
subplot.plot(linex, liney)

field = [[(1 / (1 + np.exp(-(w0_val + w1_val*x1 + w2_val*x2))))
          for x1 in np.linspace(0,30,100)]
         for x2 in np.linspace(0,30,100)]
subplot.imshow(field, origin='lower', extent=(0,30,0,30),
               cmap=plt.cm.gray_r, alpha=0.5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15638271296527603, 'fraction_numerical': 0.09492880339745191, 'mean_word_length': 6.137254901960785, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '263278', 'license': 'Apache-2.0', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1650, 'n_words': 309, 'path': 'No.1 Tensorflow Programming.ipynb', 'repo_name': 'enakai00/cnn_introduction', 'size': '4003'}