<jupyter_start><jupyter_text>tensorflow 代码结构拆解  1. 线性回归，拟合平面  1.1 导入模块<jupyter_code>import tensorflow as tf
import numpy as np<jupyter_output><empty_output><jupyter_text>1.2 数据准备<jupyter_code># 使用 NumPy 生成假数据(phony data), 总共 100 个点.
x_data = np.float32(np.random.rand(2, 100)) # 随机输入
y_data = np.dot([0.100, 0.200], x_data) + 0.300<jupyter_output><empty_output><jupyter_text>1.3 构造一个线性模型主要是描述这个模型的结构，有哪些输入，输入之间的关系<jupyter_code># 模型参数
b = tf.Variable(tf.zeros([1]))   
# 模型参数
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))  
# 模型参数 + 真实数据输入
y = tf.matmul(W, x_data) + b<jupyter_output><empty_output><jupyter_text>1.4 定义损失函数<jupyter_code># 最小化方差，模型参数+真实数据 得到的结果y和真实label y 进行比较计算损失函数
loss = tf.reduce_mean(tf.square(y - y_data))<jupyter_output><empty_output><jupyter_text>1.5 定一优化器<jupyter_code>optimizer = tf.train.GradientDescentOptimizer(0.5)<jupyter_output><empty_output><jupyter_text>1.6 给优化器选定优化目标，即目标损失函数<jupyter_code>train = optimizer.minimize(loss)<jupyter_output><empty_output><jupyter_text>1.7 初始化tf变量<jupyter_code># 初始化变量
init = tf.initialize_all_variables()<jupyter_output><empty_output><jupyter_text>1.8 启动图<jupyter_code># 启动图 (graph)
sess = tf.Session()
sess.run(init)<jupyter_output><empty_output><jupyter_text>1.9 开始拟合并输出中间结果<jupyter_code># 拟合平面
for step in range(0, 201):
    sess.run(train)  # 训练，把优化器minimisze的结果放进去
    if step % 20 == 0:
        # 输出 w和b
        print (step, sess.run(W), sess.run(b))

# 得到最佳拟合结果 W: [[0.100  0.200]], b: [0.300]<jupyter_output>0 [[-0.12605554 -0.04678726]] [1.0582082]
20 [[-0.05134993  0.02065844]] [0.4658503]
40 [[0.03929748 0.12705551]] [0.36706945]
60 [[0.07551952 0.1704446 ]] [0.32712308]
80 [[0.09010912 0.18804006]] [0.3109687]
100 [[0.0960013  0.19516231]] [0.3044358]
120 [[0.09838308 0.19804348]] [0.30179384]
140 [[0.09934615 0.19920877]] [0.30072543]
160 [[0.09973559 0.19968003]] [0.30029336]
180 [[0.09989308 0.19987063]] [0.30011863]
200 [[0.09995676 0.19994767]] [0.300048]
{'fraction_non_alphanumeric': 0.16555612506406972, 'fraction_numerical': 0.19682214249103025, 'mean_word_length': 6.1501831501831505, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '550441', 'license': 'MIT', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1093, 'n_words': 136, 'path': 'Line-Regression.ipynb', 'repo_name': 'pengjeck/deep_learn_practice', 'size': '1951'}