基本开发步骤
- 准备数据
- 模型搭建
- 正向模型搭建
- 反向模型搭建
- 迭代训练模型
- 训练模型
- 训练模型可视化
- 使用模型
代码
"""
TensorFlow基本开发步骤--以逻辑回归拟合二维数组为例
示例1:从一组看似混乱的数据中找出y≈2x的规律
"""
# tensorflow 框架
import tensorflow as tf
# 数据类型
import numpy as np
# 图形化
import matplotlib.pyplot as plt
"""
定义变量函数
"""
# 绘图数据 批量尺寸 损失
plotdata = {"batchsize": [], "loss": []}
def moving_average(a, w=10):
"""
移动平均数
:param a: :param w: :return:
""" # 判断a的大小是否小于w
if len(a) < w:
return a[:]
# else 看不懂
return [val if idx < w else sum(a[(idx - w):idx]) / w for idx, val in enumerate(a)]
"""
准备数据
"""
# 生成100个随机数
train_X = np.linspace(-1, 1, 100)
# y=2x 但是加入了噪声
train_Y = 2 * train_X + np.random.randn(*train_X.shape) * 0.3
# 显示模拟数据点 Original=原始
plt.plot(train_X, train_Y, "ro", label="Original data")
# 显示图例
plt.legend()
plt.show()
"""
上方只是展示了未经过训练的原始数据图例
"""
"""
正向模型搭建
"""
# 创建模型
# 占位符 X 输入值 Y 真实值
X = tf.placeholder("float")
Y = tf.placeholder("float")
# 模型参数 W 权重 b 偏执
W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.zeros([1]), name="bias")
# 前向结构 方程公式
z = tf.multiply(X, W) + b
"""
反向搭建模型
"""
# 反向优化
# cost 等于生成之与真实值的平方差
cost = tf.reduce_mean(tf.square(Y - z))
# 学习率
learning_rate = 0.01
# 梯度下降
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# 迭代训练模型 分两步
# 第一步 训练模型
# 初始化所有变量
init = tf.global_variables_initializer()
# 定义参数 循环次数 打印频率
training_epochs = 20
display_setp = 4
# 启动session
with tf.Session() as sess:
# 初始化
sess.run(init)
plotdata = {"batchsize": [], "loss": []}
# 向模型输入数
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={X: x, Y: y})
# 显示训练中的详细信息
if epoch % display_setp == 0:
loss = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print("Epoch:", epoch + 1, "cost=", loss, "W=", sess.run(W), "b=", sess.run(b))
if not (loss == "NA"):
plotdata["batchsize"].append(epoch)
plotdata["loss"].append(loss)
print("Finished!!!")
print("cost=", sess.run(cost, feed_dict={X: train_X, Y: train_Y}), "W=", sess.run(W), "b=", sess.run(b))
# 第二步 训练模型可视化
plt.plot(train_X, train_Y, "ro", label="Original data")
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label="Fittedline")
plt.legend()
plt.show()
plotdata["avgloss"] = moving_average(plotdata["loss"])
plt.figure(1)
plt.subplot(211)
plt.plot(plotdata["batchsize"], plotdata["avgloss"], "b--")
plt.xlabel("Minibatch number")
plt.ylabel("loss")
plt.title("Minibatch run vs. Tranining loss")
plt.show()
# 使用模型
print("x=0.2,z=", sess.run(z, feed_dict={X: 0.2}))
运行结果
Epoch: 1 cost= 0.4302579 W= [1.0741222] b= [-0.01339927]
Epoch: 1 cost= 0.4175731 W= [1.096076] b= [-0.03580576]
Epoch: 1 cost= 0.4107386 W= [1.1090443] b= [-0.0493201]
Epoch: 1 cost= 0.4070973 W= [1.1163998] b= [-0.05715009]
Epoch: 1 cost= 0.39562747 W= [1.1423792] b= [-0.0854134]
Epoch: 1 cost= 0.39191964 W= [1.1520866] b= [-0.09621159]
Epoch: 1 cost= 0.38957307 W= [1.1587859] b= [-0.10383497]
Epoch: 1 cost= 0.38473746 W= [1.1745638] b= [-0.12221149]
Epoch: 1 cost= 0.3833689 W= [1.1797644] b= [-0.1284146]
Epoch: 1 cost= 0.38140237 W= [1.1883081] b= [-0.13885696]
Epoch: 1 cost= 0.3808404 W= [1.1911186] b= [-0.14237893]
Epoch: 1 cost= 0.378618 W= [1.2053323] b= [-0.16065371]
Epoch: 1 cost= 0.37783945 W= [1.2131053] b= [-0.17091419]
Epoch: 1 cost= 0.37737668 W= [1.2221395] b= [-0.18316594]
Epoch: 1 cost= 0.377352 W= [1.2241675] b= [-0.18599378]
Epoch: 1 cost= 0.37757292 W= [1.2321974] b= [-0.1975149]
Epoch: 1 cost= 0.37738755 W= [1.228898] b= [-0.1926397]
Epoch: 1 cost= 0.37729377 W= [1.2273921] b= [-0.19034606]
Epoch: 1 cost= 0.37859008 W= [1.2391189] b= [-0.208774]
Epoch: 1 cost= 0.37858278 W= [1.2390745] b= [-0.20870191]
Epoch: 1 cost= 0.37918252 W= [1.2420846] b= [-0.2137528]
Epoch: 1 cost= 0.3788398 W= [1.2405946] b= [-0.211165]
Epoch: 1 cost= 0.38123405 W= [1.2490487] b= [-0.22638245]
Epoch: 1 cost= 0.3817624 W= [1.2505527] b= [-0.22919165]
Epoch: 1 cost= 0.381961 W= [1.2510567] b= [-0.23016995]
Epoch: 1 cost= 0.3815175 W= [1.2500244] b= [-0.22808433]
Epoch: 1 cost= 0.3815129 W= [1.2500145] b= [-0.22806354]
Epoch: 1 cost= 0.38499114 W= [1.2564143] b= [-0.24214312]
Epoch: 1 cost= 0.38524345 W= [1.2568146] b= [-0.24306478]
Epoch: 1 cost= 0.38390106 W= [1.2548324] b= [-0.23827834]
Epoch: 1 cost= 0.38592055 W= [1.257544] b= [-0.24516165]
Epoch: 1 cost= 0.38870543 W= [1.2607919] b= [-0.25385195]
Epoch: 1 cost= 0.38616097 W= [1.2580843] b= [-0.24619347]
Epoch: 1 cost= 0.38713038 W= [1.2590405] b= [-0.24906206]
Epoch: 1 cost= 0.387484 W= [1.2593545] b= [-0.25006482]
Epoch: 1 cost= 0.38677657 W= [1.2587825] b= [-0.24811243]
Epoch: 1 cost= 0.3847087 W= [1.2572385] b= [-0.24245101]
Epoch: 1 cost= 0.38824815 W= [1.2595925] b= [-0.25177288]
Epoch: 1 cost= 0.38740855 W= [1.2591021] b= [-0.24966207]
Epoch: 1 cost= 0.38883168 W= [1.2598387] b= [-0.25313458]
Epoch: 1 cost= 0.39167175 W= [1.2611042] b= [-0.25972864]
Epoch: 1 cost= 0.39057717 W= [1.2606823] b= [-0.25727198]
Epoch: 1 cost= 0.38174722 W= [1.2575941] b= [-0.23688953]
Epoch: 1 cost= 0.37995708 W= [1.2570332] b= [-0.23261812]
Epoch: 1 cost= 0.37833732 W= [1.2566065] b= [-0.22877687]
Epoch: 1 cost= 0.38059166 W= [1.2570789] b= [-0.23397419]
Epoch: 1 cost= 0.38117614 W= [1.2571706] b= [-0.23527026]
Epoch: 1 cost= 0.38426444 W= [1.2575027] b= [-0.24184701]
Epoch: 1 cost= 0.38850582 W= [1.2577621] b= [-0.25040796]
Epoch: 1 cost= 0.38385457 W= [1.2576691] b= [-0.24120675]
Epoch: 1 cost= 0.38336313 W= [1.2576789] b= [-0.24023546]
Epoch: 1 cost= 0.38642433 W= [1.257501] b= [-0.24610378]
Epoch: 1 cost= 0.3817218 W= [1.2579497] b= [-0.23722017]
Epoch: 1 cost= 0.379011 W= [1.2583141] b= [-0.23206683]
Epoch: 1 cost= 0.37598747 W= [1.2588369] b= [-0.22631617]
Epoch: 1 cost= 0.37381843 W= [1.2592945] b= [-0.222197]
Epoch: 1 cost= 0.36766517 W= [1.2608446] b= [-0.21039242]
Epoch: 1 cost= 0.36739722 W= [1.2609227] b= [-0.20987669]
Epoch: 1 cost= 0.36083862 W= [1.2631016] b= [-0.19718745]
Epoch: 1 cost= 0.3536625 W= [1.2658552] b= [-0.18283968]
Epoch: 1 cost= 0.35028893 W= [1.2673179] b= [-0.17594409]
Epoch: 1 cost= 0.34524614 W= [1.2697477] b= [-0.16548544]
Epoch: 1 cost= 0.34590843 W= [1.2694018] b= [-0.16685544]
Epoch: 1 cost= 0.3374946 W= [1.274209] b= [-0.1492288]
Epoch: 1 cost= 0.33898956 W= [1.2732832] b= [-0.1523894]
Epoch: 1 cost= 0.33071753 W= [1.2788119] b= [-0.13473332]
Epoch: 1 cost= 0.32788956 W= [1.2808872] b= [-0.12850724]
Epoch: 1 cost= 0.3282033 W= [1.2806455] b= [-0.12919106]
Epoch: 1 cost= 0.3197539 W= [1.2876579] b= [-0.11042812]
Epoch: 1 cost= 0.3154568 W= [1.2915903] b= [-0.10044577]
Epoch: 1 cost= 0.3067015 W= [1.3004892] b= [-0.07895833]
Epoch: 1 cost= 0.3003567 W= [1.3078004] b= [-0.06212547]
Epoch: 1 cost= 0.29908293 W= [1.3093853] b= [-0.05863873]
Epoch: 1 cost= 0.29690158 W= [1.3122214] b= [-0.05266481]
Epoch: 1 cost= 0.29289734 W= [1.3177805] b= [-0.04143319]
Epoch: 1 cost= 0.2893889 W= [1.3230485] b= [-0.03120705]
Epoch: 1 cost= 0.28758204 W= [1.3259287] b= [-0.0258271]
Epoch: 1 cost= 0.28378922 W= [1.3323746] b= [-0.01422458]
Epoch: 1 cost= 0.27923954 W= [1.3408991] b= [0.00058127]
Epoch: 1 cost= 0.27637774 W= [1.3467946] b= [0.0104737]
Epoch: 1 cost= 0.27552703 W= [1.3486382] b= [0.01346574]
Epoch: 1 cost= 0.2724599 W= [1.3556631] b= [0.02450483]
Epoch: 1 cost= 0.27178177 W= [1.3573012] b= [0.02699987]
Epoch: 1 cost= 0.2712393 W= [1.3586314] b= [0.02896526]
Epoch: 1 cost= 0.26884517 W= [1.3647473] b= [0.03774019]
Epoch: 1 cost= 0.26877764 W= [1.3649257] b= [0.03798907]
Epoch: 1 cost= 0.26534674 W= [1.3744662] b= [0.05092739]
Epoch: 1 cost= 0.26102665 W= [1.3882649] b= [0.06914166]
Epoch: 1 cost= 0.25916466 W= [1.3950377] b= [0.07784954]
Epoch: 1 cost= 0.25670004 W= [1.4050244] b= [0.09036455]
Epoch: 1 cost= 0.25427285 W= [1.4164909] b= [0.10437912]
Epoch: 1 cost= 0.25192487 W= [1.4301268] b= [0.12064362]
Epoch: 1 cost= 0.25065768 W= [1.4392493] b= [0.13126868]
Epoch: 1 cost= 0.25004146 W= [1.444314] b= [0.13703194]
Epoch: 1 cost= 0.24945322 W= [1.4495856] b= [0.14289582]
Epoch: 1 cost= 0.24894637 W= [1.454544] b= [0.14829017]
Epoch: 1 cost= 0.25018308 W= [1.4439526] b= [0.13701542]
Epoch: 1 cost= 0.24901825 W= [1.4532883] b= [0.14674424]
Epoch: 1 cost= 0.24767247 W= [1.4669563] b= [0.16069394]
Epoch: 1 cost= 0.2467124 W= [1.4814045] b= [0.17514221]
Epoch: 5 cost= 0.116768606 W= [1.9688624] b= [0.04067802]
Epoch: 5 cost= 0.11617405 W= [1.9746969] b= [0.03472328]
Epoch: 5 cost= 0.11635546 W= [1.9728377] b= [0.03666078]
Epoch: 5 cost= 0.117049925 W= [1.9665632] b= [0.04334001]
Epoch: 5 cost= 0.11569047 W= [1.9800237] b= [0.02869618]
Epoch: 5 cost= 0.11584041 W= [1.9782435] b= [0.03067647]
Epoch: 5 cost= 0.11620195 W= [1.9744127] b= [0.03503567]
Epoch: 5 cost= 0.11563986 W= [1.98055] b= [0.02788744]
Epoch: 5 cost= 0.11595985 W= [1.976937] b= [0.03219687]
Epoch: 5 cost= 0.11591226 W= [1.9774361] b= [0.03158692]
Epoch: 5 cost= 0.11638422 W= [1.9729168] b= [0.03725025]
Epoch: 5 cost= 0.11563069 W= [1.980466] b= [0.02754418]
Epoch: 5 cost= 0.11548836 W= [1.9821932] b= [0.02526419]
Epoch: 5 cost= 0.11523117 W= [1.9857571] b= [0.02043099]
Epoch: 5 cost= 0.11544007 W= [1.9828503] b= [0.02448411]
Epoch: 5 cost= 0.11518464 W= [1.9864433] b= [0.01932899]
Epoch: 5 cost= 0.11577856 W= [1.9791698] b= [0.03007631]
Epoch: 5 cost= 0.11636081 W= [1.9741199] b= [0.03776768]
Epoch: 5 cost= 0.11543959 W= [1.9827021] b= [0.02428128]
Epoch: 5 cost= 0.115702055 W= [1.9798836] b= [0.02885561]
Epoch: 5 cost= 0.11564306 W= [1.9804629] b= [0.02788341]
Epoch: 5 cost= 0.11605456 W= [1.97686] b= [0.03414103]
Epoch: 5 cost= 0.11535361 W= [1.983495] b= [0.02219818]
Epoch: 5 cost= 0.115357324 W= [1.9834505] b= [0.02228113]
Epoch: 5 cost= 0.115428135 W= [1.9826555] b= [0.02382444]
Epoch: 5 cost= 0.115646526 W= [1.980553] b= [0.02807226]
Epoch: 5 cost= 0.115751445 W= [1.9796823] b= [0.02990625]
Epoch: 5 cost= 0.115185454 W= [1.9854121] b= [0.01730062]
Epoch: 5 cost= 0.11519232 W= [1.9853156] b= [0.0175228]
Epoch: 5 cost= 0.115396425 W= [1.9829928] b= [0.02313153]
Epoch: 5 cost= 0.11517416 W= [1.9855039] b= [0.01675705]
Epoch: 5 cost= 0.11501669 W= [1.988676] b= [0.00826968]
Epoch: 5 cost= 0.11514434 W= [1.9860023] b= [0.01583239]
Epoch: 5 cost= 0.115077 W= [1.9870877] b= [0.01257607]
Epoch: 5 cost= 0.11505208 W= [1.9876125] b= [0.01090004]
Epoch: 5 cost= 0.11506555 W= [1.9873197] b= [0.01189971]
Epoch: 5 cost= 0.11514707 W= [1.9861102] b= [0.01633471]
Epoch: 5 cost= 0.11502624 W= [1.9888417] b= [0.0055183]
Epoch: 5 cost= 0.115026176 W= [1.9887595] b= [0.00587172]
Epoch: 5 cost= 0.11505948 W= [1.9899236] b= [0.00038406]
Epoch: 5 cost= 0.11524792 W= [1.9916244] b= [-0.00847802]
Epoch: 5 cost= 0.115249746 W= [1.9916346] b= [-0.00853753]
Epoch: 5 cost= 0.11500562 W= [1.9889644] b= [0.00908524]
Epoch: 5 cost= 0.11501138 W= [1.9887973] b= [0.01035792]
Epoch: 5 cost= 0.11501486 W= [1.9887297] b= [0.0109658]
Epoch: 5 cost= 0.11504084 W= [1.9895171] b= [0.00230476]
Epoch: 5 cost= 0.115127265 W= [1.9898696] b= [-0.00268112]
Epoch: 5 cost= 0.115478404 W= [1.9903994] b= [-0.01316955]
Epoch: 5 cost= 0.11620573 W= [1.9907839] b= [-0.02585987]
Epoch: 5 cost= 0.11588611 W= [1.9907348] b= [-0.02100153]
Epoch: 5 cost= 0.11611943 W= [1.9906987] b= [-0.02458245]
Epoch: 5 cost= 0.11696954 W= [1.9903767] b= [-0.03520808]
Epoch: 5 cost= 0.11662664 W= [1.990575] b= [-0.03128266]
Epoch: 5 cost= 0.11662676 W= [1.9905748] b= [-0.0312841]
Epoch: 5 cost= 0.11659274 W= [1.9906116] b= [-0.03088048]
Epoch: 5 cost= 0.11671444 W= [1.9904542] b= [-0.03229618]
Epoch: 5 cost= 0.116215914 W= [1.9912534] b= [-0.02620984]
Epoch: 5 cost= 0.11665634 W= [1.990438] b= [-0.03159111]
Epoch: 5 cost= 0.116118774 W= [1.9915744] b= [-0.02497301]
Epoch: 5 cost= 0.115575895 W= [1.9931303] b= [-0.01686568]
Epoch: 5 cost= 0.11554701 W= [1.9932344] b= [-0.01637499]
Epoch: 5 cost= 0.1153328 W= [1.9941392] b= [-0.01248067]
Epoch: 5 cost= 0.11581728 W= [1.9920967] b= [-0.0205693]
Epoch: 5 cost= 0.11519592 W= [1.9950309] b= [-0.00981036]
Epoch: 5 cost= 0.11578358 W= [1.9920512] b= [-0.01998232]
Epoch: 5 cost= 0.115174145 W= [1.9953412] b= [-0.00947575]
Epoch: 5 cost= 0.11522569 W= [1.9949892] b= [-0.01053169]
Epoch: 5 cost= 0.11570263 W= [1.9921281] b= [-0.01862422]
Epoch: 5 cost= 0.11507101 W= [1.9963264] b= [-0.00739078]
Epoch: 5 cost= 0.114971496 W= [1.9972476] b= [-0.00505262]
Epoch: 5 cost= 0.114612795 W= [2.0029356] b= [0.00868212]
Epoch: 5 cost= 0.11459038 W= [2.0068352] b= [0.0176601]
Epoch: 5 cost= 0.11458206 W= [2.0048063] b= [0.01319628]
Epoch: 5 cost= 0.11459362 W= [2.0038257] b= [0.01113051]
Epoch: 5 cost= 0.11457836 W= [2.0053647] b= [0.01424004]
Epoch: 5 cost= 0.11457885 W= [2.0064096] b= [0.01626852]
Epoch: 5 cost= 0.11458216 W= [2.0048645] b= [0.01338216]
Epoch: 5 cost= 0.11457841 W= [2.0066836] b= [0.01665676]
Epoch: 5 cost= 0.11463751 W= [2.010382] b= [0.02308022]
Epoch: 5 cost= 0.11466329 W= [2.0112536] b= [0.02454297]
Epoch: 5 cost= 0.11459364 W= [2.0078785] b= [0.01906534]
Epoch: 5 cost= 0.114616506 W= [2.0094929] b= [0.02160211]
Epoch: 5 cost= 0.114588775 W= [2.005532] b= [0.01556956]
Epoch: 5 cost= 0.11465696 W= [2.0010788] b= [0.00898952]
Epoch: 5 cost= 0.11465297 W= [2.0012317] b= [0.00920863]
Epoch: 5 cost= 0.11488743 W= [1.995272] b= [0.00089877]
Epoch: 5 cost= 0.11474116 W= [1.9985048] b= [0.00528289]
Epoch: 5 cost= 0.114575386 W= [2.005832] b= [0.01495492]
Epoch: 5 cost= 0.114574306 W= [2.005976] b= [0.01513993]
Epoch: 5 cost= 0.11456986 W= [2.009183] b= [0.01915881]
Epoch: 5 cost= 0.11462654 W= [2.0137258] b= [0.02471125]
Epoch: 5 cost= 0.11483376 W= [2.0203018] b= [0.03255487]
Epoch: 5 cost= 0.11492096 W= [2.0222359] b= [0.03480739]
Epoch: 5 cost= 0.114823654 W= [2.0199914] b= [0.03225345]
Epoch: 5 cost= 0.11474811 W= [2.0178418] b= [0.02986232]
Epoch: 5 cost= 0.1146792 W= [2.0152757] b= [0.02707062]
Epoch: 5 cost= 0.114764825 W= [1.9970653] b= [0.00768529]
Epoch: 5 cost= 0.11471731 W= [1.9986968] b= [0.00938542]
Epoch: 5 cost= 0.11460754 W= [2.0045846] b= [0.01539451]
Epoch: 5 cost= 0.114594996 W= [2.0111864] b= [0.0219962]
Epoch: 9 cost= 0.11423885 W= [2.039431] b= [0.01494343]
Epoch: 9 cost= 0.11422315 W= [2.0434062] b= [0.01088624]
Epoch: 9 cost= 0.114236124 W= [2.0398242] b= [0.01461914]
Epoch: 9 cost= 0.11439786 W= [2.0319533] b= [0.02299775]
Epoch: 9 cost= 0.114223965 W= [2.0439348] b= [0.00996288]
Epoch: 9 cost= 0.11422787 W= [2.0407846] b= [0.01346695]
Epoch: 9 cost= 0.11430307 W= [2.0356853] b= [0.01926954]
Epoch: 9 cost= 0.114227735 W= [2.0406485] b= [0.0134888]
Epoch: 9 cost= 0.11429943 W= [2.0359492] b= [0.0190939]
Epoch: 9 cost= 0.114312075 W= [2.0354438] b= [0.01971167]
Epoch: 9 cost= 0.11451357 W= [2.0299964] b= [0.02653828]
Epoch: 9 cost= 0.11427831 W= [2.0366883] b= [0.01793436]
Epoch: 9 cost= 0.114259176 W= [2.0376246] b= [0.01669842]
Epoch: 9 cost= 0.11422281 W= [2.0404594] b= [0.01285399]
Epoch: 9 cost= 0.114276715 W= [2.0368812] b= [0.01784328]
Epoch: 9 cost= 0.11422684 W= [2.0398567] b= [0.01357414]
Epoch: 9 cost= 0.11446012 W= [2.032016] b= [0.02515952]
Epoch: 9 cost= 0.11482643 W= [2.0264459] b= [0.03364316]
Epoch: 9 cost= 0.114339024 W= [2.0345519] b= [0.02090522]
Epoch: 9 cost= 0.11449584 W= [2.031298] b= [0.02618603]
Epoch: 9 cost= 0.114485055 W= [2.0314803] b= [0.02588004]
Epoch: 9 cost= 0.114777476 W= [2.0275161] b= [0.0327652]
Epoch: 9 cost= 0.1143523 W= [2.033823] b= [0.02141271]
Epoch: 9 cost= 0.11436859 W= [2.0334816] b= [0.02205024]
Epoch: 9 cost= 0.114427306 W= [2.0324187] b= [0.02411364]
Epoch: 9 cost= 0.11459665 W= [2.0300753] b= [0.02884828]
Epoch: 9 cost= 0.11469566 W= [2.0289888] b= [0.03113696]
Epoch: 9 cost= 0.11429888 W= [2.034526] b= [0.01895496]
Epoch: 9 cost= 0.11431128 W= [2.0342586] b= [0.0195707]
Epoch: 9 cost= 0.11447306 W= [2.0317848] b= [0.02554386]
Epoch: 9 cost= 0.1143102 W= [2.0341635] b= [0.01950555]
Epoch: 9 cost= 0.114212014 W= [2.0372202] b= [0.01132692]
Epoch: 9 cost= 0.11430311 W= [2.0344467] b= [0.01917173]
Epoch: 9 cost= 0.11425321 W= [2.035447] b= [0.01617159]
Epoch: 9 cost= 0.114235856 W= [2.0358994] b= [0.01472651]
Epoch: 9 cost= 0.11425001 W= [2.035546] b= [0.01593254]
Epoch: 9 cost= 0.114331655 W= [2.0342867] b= [0.02054994]
Epoch: 9 cost= 0.114209235 W= [2.036978] b= [0.00989253]
Epoch: 9 cost= 0.11420976 W= [2.0368643] b= [0.01038213]
Epoch: 9 cost= 0.114230566 W= [2.0380042] b= [0.00500835]
Epoch: 9 cost= 0.114390165 W= [2.0396874] b= [-0.00376167]
Epoch: 9 cost= 0.11438985 W= [2.0396855] b= [-0.00375044]
Epoch: 9 cost= 0.11422762 W= [2.0370078] b= [0.0139222]
Epoch: 9 cost= 0.11424055 W= [2.0368369] b= [0.01522431]
Epoch: 9 cost= 0.114247866 W= [2.0367682] b= [0.01584162]
Epoch: 9 cost= 0.11421514 W= [2.0375564] b= [0.00717041]
Epoch: 9 cost= 0.114265025 W= [2.037911] b= [0.00215515]
Epoch: 9 cost= 0.11453375 W= [2.038443] b= [-0.00838149]
Epoch: 9 cost= 0.11515623 W= [2.0388296] b= [-0.02113844]
Epoch: 9 cost= 0.11488527 W= [2.0387814] b= [-0.01636484]
Epoch: 9 cost= 0.11509024 W= [2.0387442] b= [-0.02004819]
Epoch: 9 cost= 0.11584316 W= [2.0384185] b= [-0.03079362]
Epoch: 9 cost= 0.1155514 W= [2.03861] b= [-0.02700502]
Epoch: 9 cost= 0.11556277 W= [2.038599] b= [-0.02715994]
Epoch: 9 cost= 0.11554564 W= [2.0386202] b= [-0.02692612]
Epoch: 9 cost= 0.11566513 W= [2.0384424] b= [-0.0285276]
Epoch: 9 cost= 0.11525148 W= [2.039215] b= [-0.02264266]
Epoch: 9 cost= 0.115643255 W= [2.0383668] b= [-0.02824062]
Epoch: 9 cost= 0.11520155 W= [2.0394635] b= [-0.02185412]
Epoch: 9 cost= 0.114771366 W= [2.0409722] b= [-0.01399299]
Epoch: 9 cost= 0.11476066 W= [2.041021] b= [-0.01376272]
Epoch: 9 cost= 0.11460678 W= [2.041862] b= [-0.01014268]
Epoch: 9 cost= 0.115003146 W= [2.0397468] b= [-0.0185191]
Epoch: 9 cost= 0.114531256 W= [2.042599] b= [-0.00806107]
Epoch: 9 cost= 0.115004346 W= [2.0395274] b= [-0.01854671]
Epoch: 9 cost= 0.11454256 W= [2.0427153] b= [-0.00836618]
Epoch: 9 cost= 0.11459305 W= [2.0422506] b= [-0.00976013]
Epoch: 9 cost= 0.114984706 W= [2.039266] b= [-0.01820227]
Epoch: 9 cost= 0.11450872 W= [2.0433297] b= [-0.00732961]
Epoch: 9 cost= 0.114449255 W= [2.0441043] b= [-0.005363]
Epoch: 9 cost= 0.11426308 W= [2.0496342] b= [0.00798984]
Epoch: 9 cost= 0.1143446 W= [2.0533636] b= [0.01657601]
Epoch: 9 cost= 0.11427822 W= [2.0511522] b= [0.01171088]
Epoch: 9 cost= 0.11426347 W= [2.0499766] b= [0.00923477]
Epoch: 9 cost= 0.114280626 W= [2.0513084] b= [0.01192536]
Epoch: 9 cost= 0.11429863 W= [2.0521333] b= [0.01352678]
Epoch: 9 cost= 0.11426694 W= [2.0503554] b= [0.01020569]
Epoch: 9 cost= 0.114293024 W= [2.0519292] b= [0.01303837]
Epoch: 9 cost= 0.114407636 W= [2.0553694] b= [0.01901318]
Epoch: 9 cost= 0.114435054 W= [2.05597] b= [0.02002105]
Epoch: 9 cost= 0.11430508 W= [2.052311] b= [0.01408281]
Epoch: 9 cost= 0.114341825 W= [2.0536287] b= [0.01615373]
Epoch: 9 cost= 0.11425814 W= [2.0493588] b= [0.00965059]
Epoch: 9 cost= 0.1142763 W= [2.0445843] b= [0.00259572]
Epoch: 9 cost= 0.114279136 W= [2.0444036] b= [0.00233627]
Epoch: 9 cost= 0.11446787 W= [2.0380983] b= [-0.00645537]
Epoch: 9 cost= 0.11436194 W= [2.040974] b= [-0.00255576]
Epoch: 9 cost= 0.11425614 W= [2.0479324] b= [0.00662958]
Epoch: 9 cost= 0.11425638 W= [2.0476964] b= [0.0063262]
Epoch: 9 cost= 0.11426806 W= [2.0505126] b= [0.00985551]
Epoch: 9 cost= 0.114338934 W= [2.0546544] b= [0.01491772]
Epoch: 9 cost= 0.114556305 W= [2.0608191] b= [0.02227093]
Epoch: 9 cost= 0.114628926 W= [2.0623324] b= [0.02403338]
Epoch: 9 cost= 0.114507474 W= [2.059658] b= [0.02099019]
Epoch: 9 cost= 0.114413336 W= [2.0570698] b= [0.01811114]
Epoch: 9 cost= 0.11433112 W= [2.054057] b= [0.01483329]
Epoch: 9 cost= 0.11442535 W= [2.0353918] b= [-0.00503591]
Epoch: 9 cost= 0.11439001 W= [2.0365615] b= [-0.00381691]
Epoch: 9 cost= 0.114278965 W= [2.041981] b= [0.00171423]
Epoch: 9 cost= 0.114251636 W= [2.0481083] b= [0.0078416]
Epoch: 13 cost= 0.11423771 W= [2.0441515] b= [0.01321484]
Epoch: 13 cost= 0.11424778 W= [2.0480022] b= [0.00928473]
Epoch: 13 cost= 0.11423789 W= [2.0443048] b= [0.01313787]
Epoch: 13 cost= 0.1143536 W= [2.0363271] b= [0.02163028]
Epoch: 13 cost= 0.11425005 W= [2.0482097] b= [0.00870316]
Epoch: 13 cost= 0.11423603 W= [2.044968] b= [0.01230929]
Epoch: 13 cost= 0.1142848 W= [2.0397837] b= [0.01820856]
Epoch: 13 cost= 0.1142357 W= [2.0446684] b= [0.01251941]
Epoch: 13 cost= 0.11428505 W= [2.0398965] b= [0.01821132]
Epoch: 13 cost= 0.11429667 W= [2.0393238] b= [0.01891132]
Epoch: 13 cost= 0.11447552 W= [2.0338142] b= [0.02581586]
Epoch: 13 cost= 0.11427107 W= [2.0404487] b= [0.01728578]
Epoch: 13 cost= 0.11425669 W= [2.041332] b= [0.01611978]
Epoch: 13 cost= 0.114232056 W= [2.044118] b= [0.01234161]
Epoch: 13 cost= 0.114272825 W= [2.0404947] b= [0.01739361]
Epoch: 13 cost= 0.114234336 W= [2.0434287] b= [0.01318383]
Epoch: 13 cost= 0.11444126 W= [2.03555] b= [0.02482537]
Epoch: 13 cost= 0.11479122 W= [2.0299451] b= [0.03336211]
Epoch: 13 cost= 0.11433134 W= [2.0380192] b= [0.02067431]
Epoch: 13 cost= 0.11447967 W= [2.0347362] b= [0.02600247]
Epoch: 13 cost= 0.11447089 W= [2.0348918] b= [0.02574113]
Epoch: 13 cost= 0.11475426 W= [2.0309033] b= [0.03266836]
Epoch: 13 cost= 0.11434671 W= [2.0371883] b= [0.02135544]
Epoch: 13 cost= 0.11436308 W= [2.036827] b= [0.02203015]
Epoch: 13 cost= 0.11442034 W= [2.035746] b= [0.02412841]
Epoch: 13 cost= 0.11458582 W= [2.0333865] b= [0.0288957]
Epoch: 13 cost= 0.114683956 W= [2.0322855] b= [0.03121487]
Epoch: 13 cost= 0.11429821 W= [2.0378098] b= [0.01906127]
Epoch: 13 cost= 0.1143107 W= [2.037531] b= [0.01970341]
Epoch: 13 cost= 0.11446936 W= [2.035047] b= [0.02570103]
Epoch: 13 cost= 0.11431031 W= [2.037417] b= [0.01968527]
Epoch: 13 cost= 0.11421603 W= [2.0404658] b= [0.01152738]
Epoch: 13 cost= 0.114304535 W= [2.0376856] b= [0.01939112]
Epoch: 13 cost= 0.11425571 W= [2.0386798] b= [0.01640819]
Epoch: 13 cost= 0.11423884 W= [2.0391276] b= [0.01497862]
Epoch: 13 cost= 0.11425301 W= [2.0387702] b= [0.01619852]
Epoch: 13 cost= 0.11433462 W= [2.0375075] b= [0.02082818]
Epoch: 13 cost= 0.11421217 W= [2.0401962] b= [0.01018148]
Epoch: 13 cost= 0.11421275 W= [2.0400803] b= [0.01068025]
Epoch: 13 cost= 0.11423278 W= [2.0412185] b= [0.00531415]
Epoch: 13 cost= 0.11439052 W= [2.0429006] b= [-0.00344965]
Epoch: 13 cost= 0.11439009 W= [2.0428977] b= [-0.00343362]
Epoch: 13 cost= 0.114233226 W= [2.0402195] b= [0.01424242]
Epoch: 13 cost= 0.114246644 W= [2.0400484] b= [0.01554656]
Epoch: 13 cost= 0.11425423 W= [2.0399797] b= [0.01616456]
Epoch: 13 cost= 0.11421761 W= [2.0407681] b= [0.00749273]
Epoch: 13 cost= 0.114265054 W= [2.041123] b= [0.00247557]
Epoch: 13 cost= 0.11452831 W= [2.0416553] b= [-0.00806423]
Epoch: 13 cost= 0.11514381 W= [2.042042] b= [-0.02082559]
Epoch: 13 cost= 0.114876024 W= [2.0419939] b= [-0.01605759]
Epoch: 13 cost= 0.11507906 W= [2.0419567] b= [-0.01974774]
Epoch: 13 cost= 0.11582544 W= [2.0416307] b= [-0.03050113]
Epoch: 13 cost= 0.11553697 W= [2.0418217] b= [-0.02672162]
Epoch: 13 cost= 0.11554895 W= [2.04181] b= [-0.02688675]
Epoch: 13 cost= 0.115532815 W= [2.0418303] b= [-0.02666423]
Epoch: 13 cost= 0.115652025 W= [2.041651] b= [-0.02827808]
Epoch: 13 cost= 0.11524385 W= [2.042422] b= [-0.02240656]
Epoch: 13 cost= 0.11563222 W= [2.0415716] b= [-0.02801896]
Epoch: 13 cost= 0.11519668 W= [2.0426657] b= [-0.0216479]
Epoch: 13 cost= 0.114773795 W= [2.0441713] b= [-0.01380319]
Epoch: 13 cost= 0.11476408 W= [2.0442164] b= [-0.01359028]
Epoch: 13 cost= 0.11461397 W= [2.0450532] b= [-0.00998854]
Epoch: 13 cost= 0.11500421 W= [2.0429332] b= [-0.01838416]
Epoch: 13 cost= 0.11454201 W= [2.04578] b= [-0.00794621]
Epoch: 13 cost= 0.1150072 W= [2.0427022] b= [-0.01845278]
Epoch: 13 cost= 0.11455495 W= [2.0458832] b= [-0.00829401]
Epoch: 13 cost= 0.114605054 W= [2.045411] b= [-0.00971053]
Epoch: 13 cost= 0.11499073 W= [2.0424182] b= [-0.018176]
Epoch: 13 cost= 0.114524774 W= [2.0464728] b= [-0.00732743]
Epoch: 13 cost= 0.11446766 W= [2.0472376] b= [-0.00538563]
Epoch: 13 cost= 0.11429262 W= [2.052757] b= [0.00794172]
Epoch: 13 cost= 0.11438076 W= [2.056475] b= [0.01650172]
Epoch: 13 cost= 0.114310175 W= [2.0542514] b= [0.01160979]
Epoch: 13 cost= 0.11429333 W= [2.053063] b= [0.00910627]
Epoch: 13 cost= 0.11431231 W= [2.0543807] b= [0.01176888]
Epoch: 13 cost= 0.11433115 W= [2.055191] b= [0.01334177]
Epoch: 13 cost= 0.1142968 W= [2.0533974] b= [0.00999164]
Epoch: 13 cost= 0.11432453 W= [2.0549548] b= [0.0127948]
Epoch: 13 cost= 0.11444252 W= [2.0583775] b= [0.01873964]
Epoch: 13 cost= 0.11446973 W= [2.05896] b= [0.01971714]
Epoch: 13 cost= 0.11433543 W= [2.055282] b= [0.01374813]
Epoch: 13 cost= 0.11437282 W= [2.05658] b= [0.01578792]
Epoch: 13 cost= 0.11428513 W= [2.0522897] b= [0.00925334]
Epoch: 13 cost= 0.11429971 W= [2.0474937] b= [0.00216674]
Epoch: 13 cost= 0.11430275 W= [2.0472906] b= [0.00187532]
Epoch: 13 cost= 0.114488214 W= [2.0409625] b= [-0.00694851]
Epoch: 13 cost= 0.11438475 W= [2.043814] b= [-0.00308128]
Epoch: 13 cost= 0.11428276 W= [2.0507479] b= [0.00607154]
Epoch: 13 cost= 0.11428289 W= [2.0504866] b= [0.00573553]
Epoch: 13 cost= 0.114295505 W= [2.0532768] b= [0.00923212]
Epoch: 13 cost= 0.11436717 W= [2.057392] b= [0.01426157]
Epoch: 13 cost= 0.1145851 W= [2.0635293] b= [0.02158199]
Epoch: 13 cost= 0.11465665 W= [2.0650144] b= [0.02331168]
Epoch: 13 cost= 0.11453356 W= [2.0623114] b= [0.02023579]
Epoch: 13 cost= 0.11443813 W= [2.0596938] b= [0.01732412]
Epoch: 13 cost= 0.11435503 W= [2.0566509] b= [0.01401377]
Epoch: 13 cost= 0.11444988 W= [2.0379555] b= [-0.00588777]
Epoch: 13 cost= 0.11441538 W= [2.0390944] b= [-0.00470094]
Epoch: 13 cost= 0.1143043 W= [2.0444825] b= [0.00079824]
Epoch: 13 cost= 0.11427606 W= [2.050578] b= [0.00689391]
Epoch: 17 cost= 0.1142384 W= [2.044469] b= [0.0130987]
Epoch: 17 cost= 0.11425015 W= [2.0483115] b= [0.00917713]
Epoch: 17 cost= 0.114238665 W= [2.0446062] b= [0.01303836]
Epoch: 17 cost= 0.11435121 W= [2.0366213] b= [0.02153842]
Epoch: 17 cost= 0.114252366 W= [2.0484972] b= [0.00861855]
Epoch: 17 cost= 0.1142371 W= [2.0452492] b= [0.01223154]
Epoch: 17 cost= 0.11428406 W= [2.0400593] b= [0.01813731]
Epoch: 17 cost= 0.11423669 W= [2.0449386] b= [0.01245432]
Epoch: 17 cost= 0.11428451 W= [2.0401616] b= [0.01815206]
Epoch: 17 cost= 0.11429605 W= [2.0395844] b= [0.01885759]
Epoch: 17 cost= 0.11447335 W= [2.0340705] b= [0.02576736]
Epoch: 17 cost= 0.11427095 W= [2.0407012] b= [0.01724223]
Epoch: 17 cost= 0.11425687 W= [2.041581] b= [0.01608094]
Epoch: 17 cost= 0.114233024 W= [2.0443635] b= [0.01230721]
Epoch: 17 cost= 0.1142729 W= [2.0407374] b= [0.01736342]
Epoch: 17 cost= 0.114235155 W= [2.0436687] b= [0.01315763]
Epoch: 17 cost= 0.11444031 W= [2.0357876] b= [0.02480294]
Epoch: 17 cost= 0.11478916 W= [2.0301802] b= [0.03334324]
Epoch: 17 cost= 0.11433113 W= [2.038252] b= [0.02065882]
Epoch: 17 cost= 0.11447886 W= [2.0349672] b= [0.02599016]
Epoch: 17 cost= 0.114470236 W= [2.0351212] b= [0.02573182]
Epoch: 17 cost= 0.11475298 W= [2.0311313] b= [0.03266187]
Epoch: 17 cost= 0.114346616 W= [2.0374148] b= [0.02135161]
Epoch: 17 cost= 0.114363 W= [2.0370522] b= [0.02202882]
Epoch: 17 cost= 0.114420146 W= [2.03597] b= [0.02412943]
Epoch: 17 cost= 0.11458536 W= [2.0336094] b= [0.02889892]
Epoch: 17 cost= 0.114683434 W= [2.0325074] b= [0.03122014]
Epoch: 17 cost= 0.11429844 W= [2.0380309] b= [0.01906846]
Epoch: 17 cost= 0.11431092 W= [2.0377512] b= [0.01971238]
Epoch: 17 cost= 0.11446938 W= [2.0352666] b= [0.02571163]
Epoch: 17 cost= 0.11431059 W= [2.0376358] b= [0.0196974]
Epoch: 17 cost= 0.11421656 W= [2.0406842] b= [0.01154089]
Epoch: 17 cost= 0.114304885 W= [2.0379035] b= [0.01940591]
Epoch: 17 cost= 0.11425614 W= [2.0388975] b= [0.01642413]
Epoch: 17 cost= 0.11423929 W= [2.0393448] b= [0.01499561]
Epoch: 17 cost= 0.114253476 W= [2.0389872] b= [0.01621644]
Epoch: 17 cost= 0.1143351 W= [2.0377243] b= [0.02084693]
Epoch: 17 cost= 0.114212625 W= [2.0404127] b= [0.01020095]
Epoch: 17 cost= 0.114213206 W= [2.0402966] b= [0.01070034]
Epoch: 17 cost= 0.11423319 W= [2.0414348] b= [0.00533475]
Epoch: 17 cost= 0.114390805 W= [2.0431166] b= [-0.00342863]
Epoch: 17 cost= 0.11439035 W= [2.0431137] b= [-0.00341228]
Epoch: 17 cost= 0.11423387 W= [2.0404356] b= [0.01426398]
Epoch: 17 cost= 0.114247315 W= [2.0402644] b= [0.01556827]
Epoch: 17 cost= 0.11425489 W= [2.0401957] b= [0.01618631]
Epoch: 17 cost= 0.11421804 W= [2.0409842] b= [0.00751443]
Epoch: 17 cost= 0.11426531 W= [2.041339] b= [0.00249714]
Epoch: 17 cost= 0.1145282 W= [2.0418713] b= [-0.00804287]
Epoch: 17 cost= 0.11514323 W= [2.042258] b= [-0.02080452]
Epoch: 17 cost= 0.11487565 W= [2.0422099] b= [-0.0160369]
Epoch: 17 cost= 0.11507856 W= [2.0421727] b= [-0.01972751]
Epoch: 17 cost= 0.11582449 W= [2.0418468] b= [-0.03048143]
Epoch: 17 cost= 0.11553625 W= [2.0420375] b= [-0.02670253]
Epoch: 17 cost= 0.11554828 W= [2.0420258] b= [-0.02686835]
Epoch: 17 cost= 0.11553223 W= [2.042046] b= [-0.02664659]
Epoch: 17 cost= 0.1156514 W= [2.0418668] b= [-0.02826128]
Epoch: 17 cost= 0.115243584 W= [2.0426376] b= [-0.02239065]
Epoch: 17 cost= 0.11563173 W= [2.0417871] b= [-0.02800402]
Epoch: 17 cost= 0.11519663 W= [2.042881] b= [-0.021634]
Epoch: 17 cost= 0.114774205 W= [2.0443864] b= [-0.01379039]
Epoch: 17 cost= 0.11476455 W= [2.0444312] b= [-0.01357865]
Epoch: 17 cost= 0.114614695 W= [2.0452676] b= [-0.00997815]
Epoch: 17 cost= 0.11500452 W= [2.043147] b= [-0.01837505]
Epoch: 17 cost= 0.11454296 W= [2.0459933] b= [-0.00793845]
Epoch: 17 cost= 0.11500763 W= [2.0429153] b= [-0.01844643]
Epoch: 17 cost= 0.114556015 W= [2.0460958] b= [-0.00828912]
Epoch: 17 cost= 0.1146061 W= [2.045623] b= [-0.00970715]
Epoch: 17 cost= 0.11499136 W= [2.0426297] b= [-0.01817419]
Epoch: 17 cost= 0.11452608 W= [2.0466835] b= [-0.00732724]
Epoch: 17 cost= 0.114469126 W= [2.047448] b= [-0.0053871]
Epoch: 17 cost= 0.11429486 W= [2.0529666] b= [0.00793853]
Epoch: 17 cost= 0.114383414 W= [2.0566838] b= [0.01649677]
Epoch: 17 cost= 0.11431254 W= [2.0544593] b= [0.01160304]
Epoch: 17 cost= 0.11429556 W= [2.0532699] b= [0.00909769]
Epoch: 17 cost= 0.11431467 W= [2.054587] b= [0.01175843]
Epoch: 17 cost= 0.114333555 W= [2.055396] b= [0.0133294]
Epoch: 17 cost= 0.11429903 W= [2.0536015] b= [0.00997732]
Epoch: 17 cost= 0.114326864 W= [2.0551577] b= [0.0127785]
Epoch: 17 cost= 0.11444508 W= [2.0585792] b= [0.01872133]
Epoch: 17 cost= 0.11447229 W= [2.0591605] b= [0.01969679]
Epoch: 17 cost= 0.11433769 W= [2.0554814] b= [0.01372571]
Epoch: 17 cost= 0.114375114 W= [2.0567782] b= [0.01576342]
Epoch: 17 cost= 0.114287145 W= [2.0524864] b= [0.00922672]
Epoch: 17 cost= 0.1143015 W= [2.047689] b= [0.002138]
Epoch: 17 cost= 0.11430455 W= [2.0474844] b= [0.00184442]
Epoch: 17 cost= 0.114489794 W= [2.0411546] b= [-0.00698157]
Epoch: 17 cost= 0.1143865 W= [2.0440047] b= [-0.0031165]
Epoch: 17 cost= 0.11428476 W= [2.050937] b= [0.00603412]
Epoch: 17 cost= 0.114284895 W= [2.050674] b= [0.00569592]
Epoch: 17 cost= 0.11429756 W= [2.0534625] b= [0.00919032]
Epoch: 17 cost= 0.11436928 W= [2.0575757] b= [0.01421756]
Epoch: 17 cost= 0.11458725 W= [2.0637112] b= [0.02153578]
Epoch: 17 cost= 0.11465873 W= [2.0651944] b= [0.02326327]
Epoch: 17 cost= 0.114535496 W= [2.0624893] b= [0.02018519]
Epoch: 17 cost= 0.114439994 W= [2.0598698] b= [0.01727133]
Epoch: 17 cost= 0.11435683 W= [2.056825] b= [0.01395881]
Epoch: 17 cost= 0.114451736 W= [2.0381274] b= [-0.00594491]
Epoch: 17 cost= 0.114417285 W= [2.0392642] b= [-0.00476023]
Epoch: 17 cost= 0.11430621 W= [2.04465] b= [0.00073682]
Epoch: 17 cost= 0.1142779 W= [2.0507436] b= [0.00683036]
Finished!!!
cost= 0.11427801 W= [2.0507534] b= [0.00682681]
x=0.2,z= [0.4169775]
结语
最后 cost(生成值和真实值的平方差)一直在减少,W(权重)接近于 2,b(偏执)接近于 0。(训练次数越多越明显)
测试的代码 print("x=0.2,z=", sess.run(z, feed_dict={X: 0.5})) 结果 0.2*2=0.4169775 符合我们的要求
个人暂时的理解:
W:两个值之间的关系,例如 y≈2x,所以这里的 W≈2。
b:应该是和 loss(损失值)差不多的概念吧
cost:生成值和真实值的平方差 含义应该和 b 差不多
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于