win10 python3.6 pip配置tensorflow
在网上看了很多资料感觉都比较混乱,实际自己下载安装的时候发现也不需要anaconda
官网的安装教程:
https://www.tensorflow.org/install/install_windows
如果要安装GPU版本的tensorflow 需要安装CUDA和相关的库CuDNN
之前我已经安装好了python3.6 以及pip
直接打开cmd
输入:
pip install tensorflow即可
输入pip install tensorflow-gpu表示安装的是gpu版本
检测方法:
python
import tensorflow
如果没有报错则表示tensorflow已经安装好了
IDE:pycharm
但是在pycharm中我直接import tensorflow会找不到
可以用如下的方法解决:
在setting中打开:点+ 安装tensorflow
然后就可以在pycharm中运行tensorflow了
例程:
import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL’] = ‘2’
import tensorflow as tf
import numpy as np
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
得到结果:
注释:
一开始pycharm会报:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 一开始不太明白是什么意思,查了一下之后发现是CPU的extensions,因为电脑上有GPU 所以可以不管,因此在代码前面加上
import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL’] = ‘2’
https://stackoverflow.com/questions/47068709/your-cpu-supports-instructions-that-this-tensorflow-binary-was-not-compiled-to-u