tensorflow学习笔记之(一)—— 安装和基本使用

tensorflow学习笔记之(一)—— 安装和基本使用

首发时间:2018年01月12日12:06:17 更新时间:2022年4月28日07:01:31

【参考文献】:

英文版链接:
中文版链接:

1.【安装】

为了简化并能够在各种计算机上都能使用,目前我们先不安装基于GPU支持的版本,仅仅能用就行,之前按照【参考】当中的指示,使用 Virtualenv 完成安装,一切使用没有问题,主线分以下几个部分:

  • Install pip and Virtualenv.
  • Create a Virtualenv environment.
  • Activate the Virtualenv environment and install TensorFlow in it.
  • After the install you will activate the Virtualenv environment each time you want to use TensorFlow

1.1 安装pip和Virtualenv

1.1.1 环境包安装

sudo apt-get install python-pip python-dev python-virtualenv

1.1.2 在指定目录安装Virtualenv

 virtualenv --system-site-packages {指定的目录}

例如我使用的代码:

virtualenv --system-site-packages /media/lucky/D500G/work/tensorflow/VE

安装显示:

lucky@lucky-PC:/media/lucky/D500G/work/tensorflow/day1$ virtualenv --system-site-packages /media/lucky/D500G/work/tensorflow/VE
Running virtualenv with interpreter /usr/bin/python2
New python executable in /media/lucky/D500G/work/tensorflow/VE/bin/python2
Also creating executable in /media/lucky/D500G/work/tensorflow/VE/bin/python
Installing setuptools, pkg_resources, pip, wheel...

1.1.3 激活环境:

source /media/lucky/D500G/work/tensorflow/VE/bin/activate

进入了virtualenv环境,那怎么退出这个环境呢?
1.1.4 退出环境

在环境里面运行:

deactivate

1.2 安装Tensorflow

还是先进入virtualenv环境,不过我是懒得敲这么多东西,直接在终端里去相应的路径下运行activate就是了:

./activate

安装方法1:

pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc1-cp27-none-linux_x86_64.whl

很显然,这个安装的是指定的版本,我这人比较懒,直接用这个方法安装:
安装方法2:

pip install tensorflow

啊,看着好美丽啊!反正一切运行很ok!只是这里有一个需要注意,不知道这种安装方式是支持cpu的还是gpu的?这个问题先放在这里,等使用GPU加速的时候再来这里给补充吧!
话又多了,看看上面我是把这个环境安装在了一块移动硬盘上,这样在哪里我都带着硬盘去就行了,这样环境就跟着走,可是后来我发现这样很不稳定,也不知道是不是个案,所以后来我就还是老老实实的把环境安装在本地计算机上了,带着程序和数据走就是了。

再后来,我发现大家用jupyter 进行文档撰写以及程序调试非常方便,于是也尝试了一下,最终我就又推翻了上述的方法,直接新建Anaconda环境,然后在里面安装了python2.7和python3.6两个环境,分别支持一种版本的tensorflow,用起来也挺方便的。更多详细内容大家看**Python学习笔记之(一)——环境建设**吧!

1.3测试安装

安装完了,测试一下是否安装成功了。
进入环境,执行python,进入Python环境,输入“import tensorflow as tf”,回车,如果不出错,说明环境没问题。
我将测试代码写成了hellotensorflow.py文件,在环境当中执行如果结果运行正常那就ok了!py文件代码如下:

#!/usr/bin/env python2
# -*- coding: utf-8 -*- 
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
a = tf.constant(10)
b = tf.constant(32)
print(sess.run(a + b))

进入环境(source {path/activate})之后运行:

./helloTensorflow.py 

得到结果:
Hello, TensorFlow!
42
好啦,环境建好了!“deactivate”退出。

2. tensorflow基本使用

参照【参考文献】:英文版链接中文版链接进行学习就是了。以下是一些在学习中教材中的代码实例,使用jupyter完成,对照英文解释,再看看代码上手很快的。需要注意的是,python和tensorflow还是有一些差异的,在使用的时候别搞混了。

#!/usr/bin/env python2
# -*- coding: utf-8 -*- 
import tensorflow as tf
# Create a Constant op that produces a 1x2 matrix. 
matrix1 = tf.constant([[3., 3.]])
# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])
# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
product = tf.matmul(matrix1, matrix2)
# Launch the default graph.
sess = tf.Session()

sess.run(matrix1)
sess.run(matrix2)
array([[ 3.,  3.]], dtype=float32)
array([[ 2.],
       [ 2.]], dtype=float32)
result = sess.run(product)
print(result)
# Close the Session when we're done.
sess.close()
[[ 12.]]

#The Session closes automatically at the end of the with block.
with tf.Session() as tfsess:
    result = tfsess.run([product])
    print (result)
    tfsess.run([product])
    tfsess.run(product)
    #sess.run(product)#sess had been closed
    #tfsess.run(result)#tfsess had been closed with 'with'
[array([[ 12.]], dtype=float32)]
[array([[ 12.]], dtype=float32)]
array([[ 12.]], dtype=float32)
result
[array([[ 12.]], dtype=float32)]
sess = tf.InteractiveSession()
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])
x.initializer.run()#变量初始化
sub = tf.subtract(x, a)
print(sub)
print(sub.eval())
Tensor("Sub:0", shape=(2,), dtype=float32)
[-2. -1.]
# Close the Session when we're done.
sess.close()

变量

#变量
state = tf.Variable(0, name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)#赋值
# Variables must be initialized by running an `init` Op after having
# launched the graph.  We first have to add the `init` Op to the graph.
init_op = tf.global_variables_initializer()#初始化所有的变量
# Launch the graph and run the ops.
with tf.Session() as sess:
  # Run the 'init' op
  sess.run(init_op)
  # Print the initial value of 'state'
  print('state=',sess.run(state))
  # Run the op that updates 'state' and print 'state'.
  for _ in range(3):
    op_add = sess.run(update)
    print(sess.run(state))
state= 0
1
2
3

fetches 取

获取数据可以通过run来实现,而且可以一次获取多个数据

input1 = tf.constant([3.0])
input2 = tf.constant([2.0])
input3 = tf.constant([5.0])
intermed = tf.add(input2, input3)
mul = tf.multiply(input1, intermed)
with tf.Session() as sess:
  result = sess.run([input1, intermed,mul])#多个数据fetch
  print(result)
[array([ 3.], dtype=float32), array([ 7.], dtype=float32), array([ 21.], dtype=float32)]

feeds

TensorFlow also provides a feed mechanism for patching a tensor directly into any operation in the graph.

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)

with tf.Session() as sess:
  print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))#feed data
[array([ 14.], dtype=float32)]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值