欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Win10+VS2017+Python3.6.5+Tensorflow1.9安装

程序员文章站 2022-07-13 21:53:49
...

1、安装VS2017,选择安装Anaconda3(64bit)支持

2、下载tensorflow‑1.9.0‑cp36‑cp36m‑win_amd64.whl到桌面(网址https://www.lfd.uci.edu/~gohlke/pythonlibs/

3、打开Anaconda Prompt,cd到桌面,输入pip install tensorflow‑1.9.0‑cp36‑cp36m‑win_amd64.whl(联网自动安装依赖项)

Win10+VS2017+Python3.6.5+Tensorflow1.9安装

4、完成后测试如下:

import numpy as np
import tensorflow as tf

# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares

# optimizer,定义一个优化器,使用梯度下降优化方法
optimizer = tf.train.GradientDescentOptimizer(0.01)
#优化器最小化loss
train = optimizer.minimize(loss)

# training data
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]

# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong

#迭代一千次后,参数已经训练好
for i in range(1000):
  sess.run(train, {x:x_train, y:y_train})

#送入训练好的参数,求出loss
# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

5、结果:W:[-0.9999969] b:[0.9999908] loss:5.6999738e-11

注:如果anaconda为独立安装,应按照上面步骤安装tensorflow,VS2017新建Python应用后,如果numpy等库无法检测到,请检查工具->Python->Python环境,检查是否为安装tensorflow的anaconda环境

Win10+VS2017+Python3.6.5+Tensorflow1.9安装

 

如果不是,则在Python环境中按下图1~3步骤添加Anaconda3,然后在解决方案中Python环境中选择Anaconda3

Win10+VS2017+Python3.6.5+Tensorflow1.9安装

Win10+VS2017+Python3.6.5+Tensorflow1.9安装

相关标签: python tensorflow