tensorflow 笔记

   
   
  • 1

在params中查找与ids对应的表示。 
如下代码表示在W中查找self.input_x对应的表示。

W = tf.Variable( tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0),name="W")
self.embedded_chars = tf.nn.embedding_lookup(W, self.input_x)
   
   
  • 1
  • 2
  • 1
  • 2

与在numpy中检索arrays类似, E.g.

matrix = np.random.random([1024, 64])  # 64-dimensional embeddings
ids = np.array([0, 5, 17, 33])
print matrix[ids]  # prints a matrix of shape [4, 64] 
   
   
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

partition_strategy 决定了ids分布的方式,如果partition_strategy 是 “mod”,每个 id 按照 p = id % len(params) 分割. 例如,13 ids 按照 5 的间隔分割成5份: [[0, 5, 10], [1, 6, 11], [2, 7, 12], [3, 8], [4, 9]]

如果 partition_strategy 是 “div”, 每个 id连续地进行分割, 上一个例子分割为: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10], [11, 12]]

tf.expand_dims

tf.expand_dims(Tensor, dim) 
为张量+1维。官网的例子:’t’ is a tensor of shape [2] 
shape(expand_dims(t, 0)) ==> [1, 2] 
shape(expand_dims(t, 1)) ==> [2, 1] 
shape(expand_dims(t, -1)) ==> [2, 1]

sess = tf.InteractiveSession()
labels = [1,2,3]
x = tf.expand_dims(labels, 0)
print(sess.run(x))
x = tf.expand_dims(labels, 1)
print(sess.run(x))
#>>>[[1 2 3]]
#>>>[[1]
#    [2]
#    [3]]
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)

这个网站不错。 ⬆️

Arguments

input shape:  [batch, in_height, in_width, in_channels]

filter shape:  [filter_height, filter_width, in_channels, out_channels]

stride: Must have strides[0] = strides[3] = 1. For the most common case of the same horizontal and vertices strides, strides = [1, stride, stride, 1].

padding: A string from: “SAME“, “VALID“. The type of padding algorithm to use.

注意:

Input shape 和 filter shape 不同, input shape是普通图片数据集的四个维度,分别是 batch height width 和channel。

Filter shape 是 height, width, input channel, output channel。 filter 改变channel, 而不会改变batch size。 filter 和 stride 一起改变了height 和 width。

stride 的 batch (stride[0]) 和 channel (stride[3]) 都是1, height 和 width相等。

padding的方法有两种:

1、如果padding = ‘VALID’

new_height = new_width = (W – F + 1) / S (结果向上取整) 
VALID方式不会在原有输入上加padding

2、如果padding = ‘SAME’

new_height = new_width = W / S (结果向上取整) 
最常见的如果stride = [1 , 1, 1, 1] 图片会加padding使得输入与输出的height width 不变


enumerate() 方法的实例:

>>> seasons = [ ' Spring ' , ' Summer ' , ' Fall ' , ' Winter ' ] >>> list ( enumerate ( seasons ) ) [ ( 0 , ' Spring ' ) , ( 1 , ' Summer ' ) , ( 2 , ' Fall ' ) , ( 3 , ' Winter ' ) ] >>> list ( enumerate ( seasons , start = 1 ) ) # 小标从 1 开始 [ ( 1 , ' Spring ' ) , ( 2 , ' Summer ' ) , ( 3 , ' Fall ' ) , ( 4 , ' Winter ' ) ]

普通的 for 循环

>>> i = 0 >>> seq = [ ' one ' , ' two ' , ' three ' ] >>> for element in seq :... print i , seq [ i ] ... i += 1 ... 0 one 1 two 2 three

for 循环使用 enumerate

>>> seq = [ ' one ' , ' two ' , ' three ' ] >>> for i , element in enumerate ( seq ) :... print i , seq [ i ] ... 0 one 1 two 2 three >>>

训练过程

在TensorFlow中,会话是一个图执行的环境(也就是说,图必须在会话中被启动),它包含有关的变量和队列状态。每个会话执行一个单一的图。如果你在创建变量和操作时,没有明确地使用一个会话,那么TensorFlow会创建一个当前默认会话。你可以通过在session.as_default() 中来修改默认会话(如下)。

图(Graph)中包含各种操作和张量。你可以在程序中使用多个图,但是大多数程序都只需要一个图。你可以把一张图在多个会话中使用,但是不能在一个会话中使用多个图。TensorFlow总是会创建一个默认图,但是你也可以自己手动创建一个图,并且把它设置为默认图,就像我们下面所写的一样。显示的创建会话和图可以确保在不需要它们的时候,正确的释放资源。这是一个很好的习惯。

with tf.Graph().as_default():
    session_conf = tf.ConfigProto(
      allow_soft_placement=FLAGS.allow_soft_placement,
      log_device_placement=FLAGS.log_device_placement)
    sess = tf.Session(config=session_conf)
    with sess.as_default():
        # Code that operates on the default graph and session comes here...

allow_soft_placement 参数的设置,允许 TensorFlow 回退到特定操作的设备,如果在优先设备不存在时。比如,如果我们的代码是运行在一个GPU上面的,但是我们的代码在一个没有GPU的机器上运行了。那么,如果不使用allow_soft_placement 参数,程序就会报错。如果设置了 log_device_placement 参数,TensorFlow 会记录它运行操作的设备(CPU或者GPU)。这对调试程序非常有用,FLAGS 是我们程序的命令行参数。


tf.ConfigProto一般用在创建session的时候。用来对session进行参数配置

with tf.Session(config = tf.ConfigProto(...),...)
    
    
  • 1
  • 1
#tf.ConfigProto()的参数
log_device_placement=True : 是否打印设备分配日志
allow_soft_placement=True : 如果你指定的设备不存在,允许TF自动分配设备
tf.ConfigProto(log_device_placement=True,allow_soft_placement=True)




整体的介绍

http://www.mamicode.com/info-detail-1866579.html





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值