TensorFlow 从零到helloWorld

目录

1.git安装与使用

     1.1 git安装

     1.2 修改git bash默认路径

     1.3 git常用操作

2.环境搭建

   2.1 tensorflow安装

   2.2 CUDA安装

   2.3 CuDNN安装

3.测试

     3.1 helloword测试

     3.2 简单线性回归测试


1.git安装与使用

1.1 git安装

     1、从Git官网下载一个Git安装包,官网地址为:http://git-scm.com/downloads;
     2、一键安装,环境变量会自己配置好

1.2 修改git bash默认路径

  1. 开始菜单下找到Git Bash 快捷方式
  2. 选中Git Bash图标,右键,选中“属性” 

  3. 去掉--cd-to-home,修改“起始位置”为自定义的git 本地仓库的路径,如:F:git_code

1.3 git常用操作

      1. 创建新仓库:创建文件夹,进入文件夹,执行git init 命令
      2. 检出仓库 :git clone username@host:/path/to/repository
      3. 从远程下载 1) git remote add origin git@github.com:demonxian3/hellowrold.git #关联本地和远程仓库
                             2) git pull origin master         #从远程把新变化拉下来
      4. 本地上传    1) git add your_resource          #从本地仓库增加,结果会保存到本机缓存里
                             2) git commit –m    “注释”                          #提交本机缓存的内容到本机HEAD里面
                             3)git push origin master          #把本地仓库提交到远程仓库 origin代表关联的远程仓库

2.环境搭建

2.1 tensorflow安装

1.pip install tensorflow 

2.2 安装CUDA(是显卡厂商NVIDIA推出的运算平台)

 1.打开链接https://developer.nvidia.com/cuda-toolkit-archive 找对应的版本下载 可以下local版(1.4G) 或者network    版 比较小
  2.安装后 检查环境变量 C:Program FilesNVIDIA GPU Computing ToolkitCUDAv9.0in

2.3. 安装cuDNN(是用于深度神经网络的GPU加速库)

  1.下载https://developer.nvidia.com/rdp/cudnn-download
  2.解压配置环境变量C:Program FilesNVIDIA GPU Computing Toolkitcudnn-9.0-windows10-x64-v7cudain

3.测试

 3.1 helloword测试

  1.跑helloworld 发现警告 Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
  解释:1)为了提升CPU计算速度的。若你有支持cuda的GPU,则可以忽略这个问题,因为安装SSE4.1, SSE4.2, AVX, AVX2, FMA, 仅仅提升CPU的运算速度(大概有3倍)
  解决办法:
      1)忽视警告,并屏蔽警告
        开头输入如下:
        import os
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
      2)进 tensorflow 官网,从源码安装。

  2.代码

'''
HelloWorld example using TensorFlow library.

Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
'''

from __future__ import print_function

import tensorflow as tf

# Simple hello world using TensorFlow

# Create a Constant op
# The op is added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
hello = tf.constant('Hello, TensorFlow!')

# Start tf session
sess = tf.Session()

# Run the op
print(sess.run(hello))

  3.2 简单线性回归测试

'''
@author :Eric-chen
@contact:809512722@qq.com
@time   :2018/4/14 18:09
@desc   :简单线性回归
'''
import tensorflow as tf
import numpy as np
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#create data
x_data=np.random.rand(100).astype(np.float32)
y_data=0.1*x_data+0.3

#create tensorflow structure start
Weights=tf.Variable(tf.random_uniform([1],-2.0,2.0))
biases=tf.Variable(tf.zeros([1]))

y=Weights*x_data+biases
loss=tf.reduce_mean(tf.square(y-y_data))

optimizer=tf.train.GradientDescentOptimizer(0.4)
train=optimizer.minimize(loss)

init=tf.global_variables_initializer()
#create tensorflow structure end

sess=tf.Session()
#Very important
sess.run(init)
for step in range(2000):
    sess.run(train)
    if step%20 ==0:
        print(step,sess.run(Weights),sess.run(biases))

  




参考资料:

1.Windows下修改Git Bash 默认路径
2.Git服务搭建及github使用教程
3.CPU、GPU、CUDA,CuDNN 简介

原文地址:https://www.cnblogs.com/jycjy/p/8836152.html