Tensorflow的安装部署及Tensorflow使用初体验

Tensorflow的安装部署及Tensorflow使用初体验。

对于 y = 0.1 *x + 0.3 ,使用tensorflow 进行拟合,训练201次epoch,预测线性方程中x的系数(0.1)及截距(bias =0.3)的值。

1,Anaconda中安装tensorflow

 (g:\ProgramData\Anaconda3) C:\Users\lenovo>pip install --upgrade --ignore-installed tensorflow
Collecting tensorflow
  Downloading tensorflow-1.7.0-cp36-cp36m-win_amd64.whl (33.1MB)
    100% |████████████████████████████████| 33.1MB 96kB/s
Collecting six>=1.10.0 (from tensorflow)
  Using cached six-1.11.0-py2.py3-none-any.whl
Collecting termcolor>=1.1.0 (from tensorflow)
  Downloading termcolor-1.1.0.tar.gz
Collecting tensorboard<1.8.0,>=1.7.0 (from tensorflow)
  Downloading tensorboard-1.7.0-py3-none-any.whl (3.1MB)
    100% |████████████████████████████████| 3.1MB 145kB/s
Collecting gast>=0.2.0 (from tensorflow)
  Downloading gast-0.2.0.tar.gz
Collecting grpcio>=1.8.6 (from tensorflow)
  Downloading grpcio-1.11.0-cp36-cp36m-win_amd64.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 112kB/s
Collecting astor>=0.6.0 (from tensorflow)
  Downloading astor-0.6.2-py2.py3-none-any.whl
Collecting absl-py>=0.1.6 (from tensorflow)
  Downloading absl-py-0.1.13.tar.gz (80kB)
    100% |████████████████████████████████| 81kB 178kB/s
Collecting numpy>=1.13.3 (from tensorflow)
  Using cached numpy-1.14.2-cp36-none-win_amd64.whl
Collecting wheel>=0.26 (from tensorflow)
  Downloading wheel-0.31.0-py2.py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 146kB/s
Collecting protobuf>=3.4.0 (from tensorflow)
  Downloading protobuf-3.5.2.post1-cp36-cp36m-win_amd64.whl (958kB)
    100% |████████████████████████████████| 962kB 311kB/s
Collecting werkzeug>=0.11.10 (from tensorboard<1.8.0,>=1.7.0->tensorflow)
  Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
    100% |████████████████████████████████| 327kB 331kB/s
Collecting bleach==1.5.0 (from tensorboard<1.8.0,>=1.7.0->tensorflow)
  Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting markdown>=2.6.8 (from tensorboard<1.8.0,>=1.7.0->tensorflow)
  Downloading Markdown-2.6.11-py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 379kB/s
Collecting html5lib==0.9999999 (from tensorboard<1.8.0,>=1.7.0->tensorflow)
  Downloading html5lib-0.9999999.tar.gz (889kB)
    100% |████████████████████████████████| 890kB 420kB/s
Collecting setuptools (from protobuf>=3.4.0->tensorflow)
  Using cached setuptools-39.0.1-py2.py3-none-any.whl
Building wheels for collected packages: termcolor, gast, absl-py, html5lib
  Running setup.py bdist_wheel for termcolor ... done
  Stored in directory: C:\Users\lenovo\AppData\Local\pip\Cache\wheels\de\f7\bf\1bcac7bf30549e6a4957382e2ecab04c88e513117207067b03
  Running setup.py bdist_wheel for gast ... done
  Stored in directory: C:\Users\lenovo\AppData\Local\pip\Cache\wheels\8e\fa\d6\77dd17d18ea23fd7b860e02623d27c1be451521af40dd4a13e
  Running setup.py bdist_wheel for absl-py ... done
  Stored in directory: C:\Users\lenovo\AppData\Local\pip\Cache\wheels\76\f7\0c\88796d7212af59bb2f496b12267e0605f205170781e9b86479
  Running setup.py bdist_wheel for html5lib ... done
  Stored in directory: C:\Users\lenovo\AppData\Local\pip\Cache\wheels\6f\85\6c\56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962
Successfully built termcolor gast absl-py html5lib
notebook 5.4.0 requires ipykernel, which is not installed.
jupyter 1.0.0 requires ipykernel, which is not installed.
jupyter-console 5.2.0 requires ipykernel, which is not installed.
ipywidgets 7.1.1 requires ipykernel>=4.5.1, which is not installed.
Installing collected packages: six, termcolor, werkzeug, html5lib, bleach, wheel, markdown, numpy, setuptools, protobuf, tensorboard, gast, grpcio, astor, absl-py, tensorflow


(g:\ProgramData\Anaconda3) C:\Users\lenovo>

2, tensorflow.py测试代码:

# -*- coding: utf-8 -*-

import tensorflow as tf
import numpy as np

# create data
x_data =np.random.rand(100).astype(np.float32)
y_data =x_data*0.1+0.3

# create tensorflow structure start #

Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights*x_data+biases

loss = tf.reduce_mean(tf.square(y - y_data))
optimizer =tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)


init =tf.initialize_all_variables()

# create tensorflow structure end #

sess =tf.Session()
sess.run(init)

for step in range(201):
    sess.run(train)
    if step % 20 ==0:
        print(step,sess.run(Weights),sess.run(biases))   
        




3,运行结果如下:每20次epoch打印一次结果,最终预测线性方程中x的系数为0.09999968,非常接近于0.1,bias预测值为0.3000002,非常接近于0.3。

0 [0.07204677] [0.43982464]
20 [0.07680491] [0.31260863]
40 [0.09329562] [0.30364445]
60 [0.09806214] [0.3010534]
80 [0.09943987] [0.3003045]
100 [0.0998381] [0.30008802]
120 [0.09995321] [0.30002543]
140 [0.09998649] [0.30000734]
160 [0.0999961] [0.30000213]
180 [0.09999887] [0.30000064]
200 [0.09999968] [0.3000002]





猜你喜欢

转载自blog.csdn.net/duan_zhihua/article/details/81022069