群里的小伙伴们都说TF2.0挺不错的,方便了很多,今天咱也先简单学习一波。
先推荐一个教程:https://zhuanlan.zhihu.com/c_1091021863043624960
TensorFlow 2.0 使用 Keras 作为开发者的核心体验。今天体验了一把,确实挺不错的,那么今天就手把手来教学如何通过TF2.0 + Keras构建一个简单神经网络。
1、安装TF2.0虚拟环境
首先,我们要在电脑里装一个tf2.0的虚拟环境(我的电脑是mac,windows和linux类似)。这里使用anaconda的命令:
conda create --name tf2 python=3.6
注意,后面的python版本最好带上,否则什么库都不给你装。这样,在anaconda的envs路径下,就多了一个tf2的虚拟环境,我们可以使用activate命令激活它:
source activate tf2
也可以使用deactivate来退出环境:
source deactivate tf2
接下来,安装tensorflow的环境,我们首先使用清华源:
sudo pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
然后,安装tf2.0:
sudo pip install tensorflow==2.0.0-alpha
2、创建Jupyter环境
安装了tf2.0的环境还不够,我们还需要把它作为一个kernel加入到jupyter环境中,执行下面两步:
conda install notebook ipykernel
sudo python -m ipykernel install --name tf2
接下来,使用jupyter notebook命令启动ipython。环境切换到tf2:
![4155986-01e8a15f7bd196a8.png](https://i-blog.csdnimg.cn/blog_migrate/fe03f45ebac11f44721d3a2ec15e3956.webp?x-image-process=image/format,png)
接下来,我们导入tf和keras,看看版本对不对:
import tensorflow as tf
from tensorflow.keras import layers
print(tf.__version__)
print(tf.keras.__version__)
输出为:
2.0.0-alpha0
2.2.4-tf
哈哈,我们的环境算是安装成功了!接下来,我们这里介绍两种建立神经网络的方式,分别是使用tf.keras.Sequential和使用 Keras 函数式 A