tensorflow库和numpy库中的相关函数

来源:互联网 发布:xp网络连接图标不见了 编辑:程序博客网 时间:2024/06/15 08:50

tensorflow:

  • tf.placeholder(shape=[2,10],dtype=tf.float32);//占位符操作,类型为[[2,10]的二维张量
  • tf.gather(params,indices);表示从params中根据indices索引获取相应的滑块,举例如下:
  • tf.expand_dims(tensor,axis);表明在制定位置对数据进行扩展一个维度,且扩展的这个维度一定为1维,如t2的shape为[2,3,4],expand_dims(t2,0),则其维度变为[1,2,3,4];如果为expand_dims(t2,3),则其维度为[2,3,4,1];

    temp=tf.range(0,10)//表明生成0..910个整数张量temp2=tf.gather(temp,[0,1,9])sess=tf.Session()sess.run(temp)sess.close()

    temp2的值为array([0,1,5],dtype=int32),表明tf.gather指示根据indices索引从params中找寻滑块;
    tf.argmax(input,axis);//获取制定列的最大值索引

  • tf.reduce_sum(input_tensor,axis,keep_dims=True);//求和操作,如果添上axis列的话,则是求制定列的和

  • tf.squeeze(input,axis);//从input张量中移除掉所有维度为1的维度,如果axis也被声明的话,则从制定的列中移除掉值为1的维度,如tensor的shape是[1,2,3,5],直接调用squeeze函数的话,则会移除掉第一维度的维度,返回的shape为[2,3,5],如果制定axis的话则首先得保证axis维度的维度一定为1;

numpy

  • np.ceil:解释起来有些麻烦,可以参照如下代码理解:

    a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])np.ceil(a)array([-1., -1., -0.,  1.,  2.,  2.,  2.])
  • np.argmax(array,axis,out);表示返回某一列最大值的索引,并将起天降到out数组中,如果out数组提供了的话,否则指示返回索引即可,见如下代码:

    a=[[1,2,3],[2,3,4],[9,7,8]]b=np.argmax(a,0)

    返回值为array([2,2,0]),np.argmax中的axis表示行和列,0代表列,1代表行

创建数组的方法:

from numpy import *a=array([1,2,3],[4,6,7],dtype=float32)b=zeros([3,4],dtype=int16)c=arange(10,30,5)==>[10,15,20,25]c_val=c.reshape(3,2)