Skip to content

Commit e49ce63

Browse files
committed
test
1 parent f0257bb commit e49ce63

File tree

9 files changed

+103
-3
lines changed

9 files changed

+103
-3
lines changed

ml_notes/TensorFlow/Image 001.png

-7.74 KB
Binary file not shown.

ml_notes/TensorFlow/Image 002.png

-20.1 KB
Binary file not shown.

ml_notes/TensorFlow/Image 004.png

-8.23 KB
Binary file not shown.

ml_notes/TensorFlow/TensorFlow入门.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
**TensorFlow**是Google维护的一个基于数据图的可扩展的机器学习框架,其项目地址位于 https://github.com/tensorflow/tensorflow ,我们可以从项目中找到官方的文档说明。
1+
**TensorFlow**是Google维护的一个基于数据流图的可扩展的机器学习框架,其项目地址位于 https://github.com/tensorflow/tensorflow ,我们可以从项目中找到官方的文档说明。
22

33
![image](TensorFlow.png)
44

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,103 @@
1-
TensorFlow支持Ubuntu、Mac OS X、Windows,也支持从源码安装。本文以Ubuntu操作系统为例,来讲解TensorFlow的环境搭建过程。
1+
TensorFlow支持Ubuntu、Mac OS X、Windows,也支持从源码安装。本文以**Ubuntu**操作系统为例,来讲解TensorFlow的环境搭建过程。
22

3-
TensorFlow有
3+
### TensorFlow分为两个版本:
4+
5+
* CPU支持版本
6+
7+
如果系统没有安装GPU,那么只能使用TensorFlow的CPU支持版本。
8+
9+
* GPU支持版本
10+
11+
如果系统安装了GPU,并且添加了其他相关支持(安装CUDA Toolkit、cuDNN、libcupti-dev库等),则可以使用TensorFlow的GPU支持版本。
12+
13+
### TensorFlow可以从以下方式安装:
14+
15+
* virtualenv安装
16+
17+
* 原生的pip安装
18+
19+
* Docker安装
20+
21+
* Anaconda安装
22+
23+
* 从源码安装
24+
25+
### 在Ubuntu上安装TensorFlow
26+
27+
首先,查看系统版本:
28+
29+
$ cat /etc/lsb-release
30+
DISTRIB_ID=Ubuntu
31+
DISTRIB_RELEASE=14.04
32+
DISTRIB_CODENAME=trusty
33+
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
34+
35+
由于没有安装GPU支持,因此只能在Ubuntu系统上安装TensorFlow的CPU支持版本。
36+
37+
其次,查看python和pip:
38+
39+
![image](python&pip.png)
40+
41+
这里,采用Anaconda中的pip来安装,对应python2版本:
42+
43+
pip install tensorflow
44+
45+
安装完毕,我们可以使用python交互式终端运行下面代码:
46+
47+
>>> import tensorflow as tf
48+
>>> sess = tf.Session()
49+
>>> hello = tf.constant('hello world!')
50+
>>> print(sess.run(hello))
51+
hello world!
52+
53+
### Python2与Python3的版本切换
54+
55+
我们来测试TensorFlow官方的一段代码:
56+
57+
import numpy as np
58+
import tensorflow as tf
59+
60+
'''
61+
tf.estimator
62+
'''
63+
64+
x_train = np.array([1., 2., 3., 4.])
65+
y_train = np.array([0., -1., -2., -3.])
66+
x_eval = np.array([2., 5., 8., 1.])
67+
y_eval = np.array([-1.01, -4.1, -7, 0.])
68+
input_fn = tf.estimator.inputs.numpy_input_fn(
69+
{"x": x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True)
70+
train_input_fn = tf.estimator.inputs.numpy_input_fn(
71+
{"x": x_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False)
72+
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
73+
{"x": x_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False)
74+
75+
feature_columns = [tf.feature_column.numeric_column("x", shape=[1])]
76+
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)
77+
78+
estimator.train(input_fn=input_fn, steps=1000)
79+
80+
train_metrics = estimator.evaluate(input_fn=train_input_fn)
81+
eval_metrics = estimator.evaluate(input_fn=eval_input_fn)
82+
print("train metrics: %r"% train_metrics)
83+
print("eval metrics: %r"% eval_metrics)
84+
85+
将测试代码写进Test.py脚本,在python2环境下,运行结果如下:
86+
87+
![image](python2test.png)
88+
89+
##### 程序会报错,指出来tf.estimator模块下不存在LinearRegressor属性。
90+
91+
为什么会出现这个问题呢?我想可能是Python2下的tensorflow库中一些上层接口功能没有跟进,而且考虑到Python2版本到2020年会停止支持,因此Google官方可能对Python3下的tensorflow库支持会更全面一些。既然这样,从Python2环境切换到Python3环境,是一个避免这些问题的办法。
92+
93+
与Virualenv类似,Anaconda支持搭建Python的虚拟环境,也就是说,无论Anaconda是基于Python2版本还是Python3版本,我们都可以搭建Python的双版本共存环境,根据项目需要,随时可以切换版本。
94+
95+
##### 搭建Python3环境:
96+
97+
![image](python3.png)
98+
99+
##### 激活python3环境,并运行Test.py:
100+
101+
![image](python3test.png)
102+
103+
运行成功!

ml_notes/TensorFlow/python&pip.png

7.27 KB
Loading
7.28 KB
Loading
File renamed without changes.
19.3 KB
Loading

0 commit comments

Comments
 (0)