本篇内容基于 Python3 TensorFlow 1.4 版本。本节内容 本节通过最简单的示例 —— 平面拟合来说明 TensorFlow 的基本用法。
构造数据 TensorFlow 的引入方式是:
import tensorflow as tf
接下来我们构造一些随机的三维数据,然后用 TensorFlow 找到平面去拟合它,首先我们用 Numpy 生成随机三维点,其中变量 x 代表三维点的 (x, y) 坐标,是一个 2×100 的矩阵,即 100 个 (x, y),然后变量 y 代表三位点的 z 坐标,我们用 Numpy 来生成这些随机的点:
import numpy as np
x_data = np.float32(np.random.rand(2, 100))
y_data = np.dot([0.300, 0.200], x_data) + 0.400
print(x_data)
print(y_data)
这里利用 Numpy 的 random 模块的 rand() 方法生成了 2×100 的随机矩阵,这样就生成了 100 个 (x, y) 坐标,然后用了一个 dot() 方法算了矩阵乘法,用了一个长度为 2 的向量跟此矩阵相乘,得到一个长度为 100 的向量,然后再加上一个常量,得到 z 坐标,输出结果样例如下:
[[ 0.97232962 0.08897641 0.54844421 0.5877986 0.5121088 0.64716059
0.22353953 0.18406206 0.16782761 0.97569454 0.65686035 0.75569868
0.35698661 0.43332314 0.41185728 0.24801297 0.50098598 0.12025958
0.40650111 0.51486945 0.19292323 0.03679928 0.56501174 0.5321334
0.71044683 0.00318134 0.76611853 0.42602748 0.33002195 0.04414672
0.73208278 0.62182301 0.49471655 0.8116194 0.86148429 0.48835048
0.69902027 0.14901569 0.18737803 0.66826463 0.43462989 0.35768151
0.79315376 0.0400687 0.76952982 0.12236254 0.61519378 0.92795062
0.84952474 0.16663995 0.13729768 0.50603199 0.38752931 0.39529857
0.29228279 0.09773371 0.43220878 0.2603009 0.14576958 0.21881725
0.64888018 0.41048348 0.27641159 0.61700606 0.49728736 0.75936913
0.04028837 0.88986284 0.84112513 0.34227493 0.69162005 0.89058989
0.39744586 0.85080278 0.37685293 0.80529863 0.31220895 0.50500977
0.95800418 0.43696108 0.04143282 0.05169986 0.33503434 0.1671818
0.10234453 0.31241918 0.23630807 0.37890589 0.63020509 0.78184551
0.87924582 0.99288088 0.30762389 0.43499199 0.53140771 0.43461791
0.23833922 0.08681628 0.74615192 0.25835371]
[ 0.8174957 0.26717573 0.23811154 0.02851068 0.9627012 0.36802396
0.50543582 0.29964805 0.44869211 0.23191817 0.77344608 0.36636299
0.56170034 0.37465382 0.00471885 0.19509546 0.49715847 0.15201907
0.5642485 0.70218688 0.6031307 0.4705168 0.98698962 0.865367
0.36558965 0.72073907 0.83386165 0.29963031 0.72276717 0.98171854
0.30932376 0.52615297 0.35522953 0.13186514 0.73437029 0.03887378
0.1208882 0.67004597 0.83422536 0.17487818 0.71460873 0.51926661
0.55297899 0.78169805 0.77547258 0.92139858 0.25020468 0.70916855
0.68722379 0.75378138 0.30182058 0.91982585 0.93160367 0.81539184
0.87977934 0.07394848 0.1004181 0.48765802 0.73601437 0.59894943
0.34601998 0.69065076 0.6768015 0.98533565 0.83803362 0.47194552
0.84103006 0.84892255 0.04474261 0.02038293 0.50802571 0.15178065
0.86116213 0.51097614 0.44155359 0.67713588 0.66439205 0.67885226
0.4243969 0.35731083 0.07878648 0.53950399 0.84162414 0.24412845
0.61285144 0.00316137 0.67407191 0.83218956 0.94473189 0.09813353
0.16728765 0.95433819 0.1416636 0.4220584 0.35413414 0.55999744
0.94829601 0.62568033 0.89808714 0.07021013]]
[ 0.85519803 0.48012807 0.61215557 0.58204171 0.74617288 0.66775297
0.56814902 0.51514823 0.5400867 0.739092 0.75174732 0.6999822
0.61943605 0.60492771 0.52450095 0.51342299 0.64972749 0.46648169
0.63480003 0.69489821 0.57850311 0.50514314 0.76690145 0.73271342
0.68625198 0.54510222 0.79660789 0.58773431 0.64356002 0.60958773
0.68148959 0.6917775 0.61946087 0.66985885 0.80531934 0.5542799
0.63388372 0.5787139 0.62305848 0.63545502 0.67331071 0.61115777
0.74854193 0.56836022 0.78595346 0.62098848 0.63459907 0.8202189
0.79230218 0.60074826 0.50155342 0.73577477 0.70257953 0.68166794
0.6636407 0.44410981 0.54974625 0.57562188 0.59093375 0.58543506
0.66386805 0.6612752 0.61828378 0.78216895 0.71679293 0.72219985
0.58029252 0.83674336 0.66128606 0.50675907 0.70909116 0.6975331
0.69146618 0.75743606 0.6013666 0.77701676 0.6265411 0.68727338
0.77228063 0.60255049 0.42818714 0.52341076 0.66883513 0.49898023
0.55327365 0.49435803 0.6057068 0.68010968 0.77800791 0.65418036
0.69723127 0.8887319 0.52061989 0.61490928 0.63024914 0.64238486
0.66116097 0.55118095 0.80346301 0.49154814]
这样我们就得到了一些三维的点。