TensorFlow2.0正式版安装

一、熟悉conda常用的cmd指令

首先要确保安装好anaconda,然后添加到系统环境变量,尽量使用管理员身份运行命令行程序,确保不会出现意想不到的问题。常见的conda命令如下:

  1. 查看conda环境:conda env list
  2. 新建conda环境(env_name就是创建的环境名,可以自定义):conda create -n env_name
  3. 激活conda环境(ubuntu与Macos 将conda 替换为source):conda activate env_name
  4. 退出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来运行。

在这里插入图片描述

  • 13
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值