TensorFlow2.0正式版安装
tensorflow
一个面向所有人的开源机器学习框架
项目地址:https://gitcode.com/gh_mirrors/te/tensorflow
免费下载资源
·
文章目录
一、熟悉conda常用的cmd指令
首先要确保安装好anaconda,然后添加到系统环境变量,尽量使用管理员身份运行命令行程序,确保不会出现意想不到的问题。常见的conda命令如下:
- 查看conda环境:conda env list
- 新建conda环境(env_name就是创建的环境名,可以自定义):conda create -n env_name
- 激活conda环境(ubuntu与Macos 将conda 替换为source):conda activate env_name
- 退出conda环境:conda deactivate
二、TF2.0 CPU版本安装
1 新建TF2.0 CPU环境
2C
表示CPU,2G
表示GPU,使用conda 新建环境指令 python==3.6
表示在新建环境时同时python3.6
conda create -n TF_2C python=3.6
当弹出 :Proceed ([y]/n)?
输入y回车,完成后就可以进入此环境。
2 进入TF_2C环境
conda activate TF_2C
进入后我们就可以发现:
(TF_2C) C:\WINDOWS\system32>
TF_2C
在之前路径前面,表示进入了这个环境。使用conda deactivate
可以退出这个环境。
3 在环境中安装TF2.0 CPU版本
继续在这个环境下安装TensorFlow,使用pip install
来安装,后面的 -i 表示从国内清华源下载。
pip install tensorflow==2.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
4 测试TensorFlow是否安装成功
新建一个test_tf.py
的脚本
import tensorflow as tf
version = tf.__version__
gpu_ok = tf.test.is_gpu_available()
print("tf version:",version,"\nif use GPU",gpu_ok)
然后在这个环境下,用python去执行
(TF_2C) C:\Users\xiaokai\Documents\Python Scripts\tensorflow>python test_tf.py
结果显示如下:
2019-10-04 07:44:57.612985: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
tf version: 2.0.0
if use GPU False
看到第二行和第三行的信息,就说明安装成功了。
三、测试一个简单的TensorFlow程序
新建一个线性拟合的python文件,内容如下:
import tensorflow as tf
X = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
y = tf.constant([[10.0], [20.0]])
class Linear(tf.keras.Model):
def __init__(self):
super().__init__()
self.dense = tf.keras.layers.Dense(
units=1,
activation=None,
kernel_initializer=tf.zeros_initializer(),
bias_initializer=tf.zeros_initializer()
)
def call(self, input):
output = self.dense(input)
return output
# 以下代码结构与前节类似
model = Linear()
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
for i in range(100):
with tf.GradientTape() as tape:
y_pred = model(X) # 调用模型 y_pred = model(X) 而不是显式写出 y_pred = a * X + b
loss = tf.reduce_mean(tf.square(y_pred - y))
grads = tape.gradient(loss, model.variables) # 使用 model.variables 这一属性直接获得模型中的所有变量
optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
if i % 10 == 0:
print(i, loss.numpy())
print(model.variables)
然后运行,结果如下,如果报错可以把中文注释删掉,或者在第一行标注编码格式。
(TF_2C) C:\Users\xiaokai\Documents\Python Scripts\tensorflow>python test_tf_linear_regression.py
2019-10-04 07:53:09.029551: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
0 250.0
10 0.73648137
20 0.6172349
30 0.5172956
40 0.4335389
50 0.36334264
60 0.3045124
70 0.25520816
80 0.2138865
90 0.17925593
[<tf.Variable 'linear/dense/kernel:0' shape=(3, 1) dtype=float32, numpy=
array([[0.40784496],
[1.191065 ],
[1.9742855 ]], dtype=float32)>, <tf.Variable 'linear/dense/bias:0' shape=(1,) dtype=float32, numpy=array([0.78322077], dtype=float32)>]
上面的代码是在命令行中运行的,也可以转到anaconda中,切换一下TF_2C
环境,然后安装notebook来运行。
GitHub 加速计划 / te / tensorflow
184.54 K
74.12 K
下载
一个面向所有人的开源机器学习框架
最近提交(Master分支:1 个月前 )
a49e66f2
PiperOrigin-RevId: 663726708
1 个月前
91dac11a
This test overrides disabled_backends, dropping the default
value in the process.
PiperOrigin-RevId: 663711155
1 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)