配置Python
在PyCharm中,我们知道ython在“File → Settings → Python Interpreter”,点击齿轮进行添加我们安装好的Python的".exe"文件。
配置Anaconda中的Tensorflow
此时我们如果不用TemsorFlow,就像很多博客中讲到的,直接就把Anaconda中的python.exe选择即可,如图所示。
但是如果安装好了TensorFlow并且想要使用,我们需要进入到Anaconda
目录下的envs
,再进入envs
目录中的tensorflow
找到里面的python.exe
文件,如图所示。
这样就可以使用了。
直接使用import tensorflow as tf
检验一下就可以了。
这是我的测试代码:
import tensorflow as tf
# tensorflow的2.0版本
# 原因是2.0与1.0版本不兼容,在程序开始部分添加以下代码:
tf.compat.v1.disable_eager_execution();
hello = tf.constant('Hello, TensorFlow!');
test = tf.constant('Hello, World!');
# tensorflow2.0版本中的确没有Session这个属性,
# 如果安装的是tensorflow2.0版本又想利用Session属性,可以将tf.Session()更改为:
sess = tf.compat.v1.Session(); #这个方法可以解决此类问题,不仅仅适用于Session属性。
print(sess.run(hello));
#输出会带一个 b,表示字符串,下面的方法则可以去掉从而直接显示字符串的内容。
print(sess.run(test).decode());