Tensorflow杂记

  • segmentation fault (core dumped)

TensorFlow中出现段错误,import numpy,然后 import tensorflow as tf,什么原因?!

  • tf.argmax()

argmax是一个非常有用的函数,它能给出某个tensor对象在某一维上的其数据最大值所在的索引值。由于标签向量是由0,1组成,因此最大值1所在的索引位置就是类别标签,比如tf.argmax(y,1)返回的是模型对于任一输入x预测到的标签值,而 tf.argmax(y_,1)代表正确的标签,我们可以用tf.equal来检测我们的预测是否真实标签匹配(索引位置一样表示匹配)。

  • global_step = tf.Variable(0,name=’global_step’,trainable=False)

初始化为0,增加一次迭代,它就变化一次,可以用来改变学习率或者调解其他参数

  • 数据处理心得

想尽办法变成一个流来处理

  • tf.one_hot()

import tensorflow as tf
indices = [0, 2, -1, 1]#索引
depth = 5#one_hot的维度
on_value = 5#索引位置的值,默认为1,数据类型需和off_value相同
off_value = 1#非索引位置的值,默认为0
axis = -1
x = tf.one_hot(indices,depth,on_value,off_value)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(x))
#####output
[[5 1 1 1 1]
 [1 1 5 1 1]
 [1 1 1 1 1]
 [1 5 1 1 1]]

tf.pad(tensor, paddings, mode=’CONSTANT’, name=None)

# 't' is [[1, 2, 3], [4, 5, 6]].
# 'paddings' is [[1, 1,], [2, 2]].
# rank of 't' is 2.
pad(t, paddings, "CONSTANT") ==> [[0, 0, 0, 0, 0, 0, 0],
                                  [0, 0, 1, 2, 3, 0, 0],
                                  [0, 0, 4, 5, 6, 0, 0],
                                  [0, 0, 0, 0, 0, 0, 0]]

pad(t, paddings, "REFLECT") ==> [[6, 5, 4, 5, 6, 5, 4],
                                 [3, 2, 1, 2, 3, 2, 1],
                                 [6, 5, 4, 5, 6, 5, 4],
                                 [3, 2, 1, 2, 3, 2, 1]]

pad(t, paddings, "SYMMETRIC") ==> [[2, 1, 1, 2, 3, 3, 2],
                                   [2, 1, 1, 2, 3, 3, 2],
                                   [5, 4, 4, 5, 6, 6, 5],
                                   [5, 4, 4, 5, 6, 6, 5]]
  • Tensorboard

tensorboard –logdir=/tmp –port=8008

  • tf.Tensor.get_shape()

return a tuple of input tensor shape

import tensorflow as tf
T=tf.constant([[[11,12,13,14]],[[15,16,17,18]]])
#start a interactive session
sess=tf.InteractiveSession()
print("Our tensor is",T.eval())
print("Shape of the tensor is ",T.get_shape())
print("T[0] is ",T[0].eval())   #equivalent to T[0,:,:]
print("T[1,0] is",T[1,0].eval())    #equivalent to T[1,0,:]
print("T[1,0,0] is ",T[1,0,0].eval())
print("T[1,0,:] is",T[1,0,:].eval())

output:
Our tensor is [[[11 12 13 14]]
                [[15 16 17 18]]]
Shape of the tensor is  (2, 1, 4)
T[0] is  [[11 12 13 14]]
T[1,0] is [15 16 17 18]
T[1,0,0] is  15
T[0,0,:] is [11 12 13 14]
  • tf.reshape(tensor,shape,name=None)

  • tf.slice(input,begin,size,name=None)

    原文作者:哎呀_落锦繁华
    原文地址: https://www.jianshu.com/p/8e0614488730
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞