求平均值的函数

reduce_mean

axis为1表示求行
axis为0表示求列

>>> xxx=tf.constant([[1., 10.],[3.,30.]])
>>> sess.run(xxx)
array([[  1.,  10.],
       [  3.,  30.]], dtype=float32)
>>> mymean=tf.reduce_mean(xxx,0)
>>> sess.run(mymean)
array([  2.,  20.], dtype=float32)
>>> mymean=tf.reduce_mean(xxx,1)
>>> sess.run(mymean)
array([  5.5,  16.5], dtype=float32)
>>> 

) ==> [1.5, 1.5]
tf.reduce_mean(x, 1) ==> [1.,  2.]

Args:

  • input_tensor: The tensor to reduce. Should have numeric type.
  • axis: The dimensions to reduce. If None (the default), reduces all dimensions.
  • keep_dims: If true, retains reduced dimensions with length 1.
  • name: A name for the operation (optional).
  • reduction_indices: The old (deprecated) name for axis.

tf.pow

pow(
    x,
    y,
    name=None
)

Defined in tensorflow/python/ops/math_ops.py.

See the guide: Math > Basic Math Functions

Computes the power of one value to another.

Given a tensor x and a tensor y, this operation computes \\(x^y\\) for corresponding elements in x and y. For example:

# tensor 'x' is [[2, 2], [3, 3]]
# tensor 'y' is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]

class tf.train.AdamOptimizer

__init__(learning_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False, name='Adam')

线性分类源码:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 10 09:35:04 2017
@author: myhaspl@myhaspl.com,http://blog.csdn.net/myhaspl"""
import tensorflow as tf
import numpy as np

batch_size=10
w1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
w2=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))

x=tf.placeholder(tf.float32,shape=(None,2),name="x")
y=tf.placeholder(tf.float32,shape=(None,1),name="y")

h=tf.matmul(x,w1)
yo=tf.matmul(h,w2)

#损失函数计算差异平均值
cross_entropy=tf.reduce_mean(tf.abs(y-yo))
#反向传播
train_step=tf.train.AdamOptimizer().minimize(cross_entropy)

#生成200个随机样本
DATASIZE=200
x_=np.random.rand(DATASIZE,2)
y_=[[int((x1+x2)>2.5)] for (x1,x2) in x_]

with tf.Session() as sess:
    #初始化变量
    init_op=tf.global_variables_initializer()
    sess.run(init_op)
    print sess.run(w1)
    print sess.run(w2)

    #设定训练轮数
    TRAINCOUNT=10000
    for i in range(TRAINCOUNT):
        #每次递进选择一组
        start=(i*batch_size) % DATASIZE
        end=min(start+batch_size,DATASIZE)
        #开始训练
        sess.run(train_step,feed_dict={x:x_[start:end],y:y_[start:end]})
        if i%1000==0:
            total_cross_entropy=sess.run(cross_entropy,feed_dict={x:x_[start:end],y:y_[start:end]})
            print("%d 次训练之后,损失:%g"%(i+1,total_cross_entropy))
    print(sess.run(w1))
    print(sess.run(w2))

[[-0.81131822  1.48459876  0.06532937 -2.4427042   0.0992484   0.59122431]
 [ 0.59282297 -2.12292957 -0.72289723 -0.05627038  0.64354479 -0.26432407]]
[[-0.81131822]
 [ 1.48459876]
 [ 0.06532937]
 [-2.4427042 ]
 [ 0.0992484 ]
 [ 0.59122431]]
1 次训练之后,损失:2.37311
1001 次训练之后,损失:0.587702
2001 次训练之后,损失:0.00187977
3001 次训练之后,损失:0.000224713
4001 次训练之后,损失:0.000245593
5001 次训练之后,损失:0.000837345
6001 次训练之后,损失:0.000561878
7001 次训练之后,损失:0.000521504
8001 次训练之后,损失:0.000369141
9001 次训练之后,损失:2.88023e-05
[[-0.40749896  0.74481744 -1.35231423 -1.57555723  1.5161525   0.38725093]
 [ 0.84865922 -2.07912779 -0.41053897 -0.21082011 -0.0567192  -0.69210052]]
[[ 0.36143586]
 [ 0.34388798]
 [ 0.79891819]
 [-1.57640576]
 [-0.86542428]
 [-0.51558757]]

tf.nn.relu

relu(
    features,
    name=None
)

Defined in tensorflow/python/ops/gen_nn_ops.py.

See the guides: Layers (contrib) > Higher level ops for building neural network layers, Neural Network > Activation Functions

Computes rectified linear: max(features, 0)



TF随笔-7的更多相关文章

  1. TF随笔-13

    import tensorflow as tf a=tf.constant(5) b=tf.constant(3) res1=tf.divide(a,b) res2=tf.div(a,b) with ...

  2. TF随笔-11

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- import tensorflow as tf my_var=tf.Variable(0.) step=t ...

  3. TF随笔-10

    #!/usr/bin/env python# -*- coding: utf-8 -*-import tensorflow as tf x = tf.constant(2)y = tf.constan ...

  4. TF随笔-9

    计算累加 #!/usr/bin/env python2 # -*- coding: utf-8 -*-"""Created on Mon Jul 24 08:25:41 ...

  5. TF随笔-8

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Mon Jul 10 09:35:04 201 ...

  6. tf随笔-6

    import tensorflow as tfx=tf.constant([-0.2,0.5,43.98,-23.1,26.58])y=tf.clip_by_value(x,1e-10,1.0)ses ...

  7. tf随笔-5

    # -*- coding: utf-8 -*-import tensorflow as tfw1=tf.Variable(tf.random_normal([2,6],stddev=1))w2=tf. ...

  8. TF随笔-4

    >>> import tensorflow as tf>>> a=tf.constant([[1,2],[3,4]])>>> b=tf.const ...

  9. TF随笔-3

    >>> import tensorflow as tf>>> node1 = tf.constant(3.0, dtype=tf.float32)>>& ...

随机推荐

  1. 收藏 19 个 ES6常用的简写技巧

    代码精炼是每个有追求的程序所向往的,本文总结了19个JavaScript的简写技术,其中包括三元操作符.短路求值简写方式.声明变量简写方法等等,还有些自己的理解加上去:希望对你有帮助. 三元操作符 当 ...

  2. 快用Visual Studio(一)- 打开文件

    在命令行中使用Visual Studio code打开文件: 打开Visual Studio code: CMD + SHIFT + P打开控制面板: 键入"shell command&qu ...

  3. 【读书笔记】《深入浅出nodejs》第四章 异步编程

    1. 异步编程的基础 -- 函数式编程 (1)高阶函数 -- 是可以把函数作为参数,或是将函数作为返回值的函数. (2)偏函数用法 -- 创建一个调用另外一个部分 -- 参数或变量已经预置的函数 -- ...

  4. 解读:hadoop压缩格式

    Hadoop中用得比较多的4种压缩格式:lzo,gzip,snappy,bzip2.它们的优缺点和应用场景如下: 1). gzip压缩 优点:压缩率比较高,而且压缩/解压速度也比较快:hadoop本身 ...

  5. 分布式缓存DistributedCache

    本文是对MR案例:Map-Join的解读. 在hadoop中,共享全局变量或全局文件的几种方法 使用Configuration的set()方法,只适合数据内容比较小的场景 将缓存文件放在HDFS上,每 ...

  6. JDBC连接池&DBUtils

    JDBC连接池 DBCP:Apache推出的Database Connection Pool 使用步骤: > 添加jar包  commons-dbcp-1.4.jar  commons-pool ...

  7. linux下挂载ISO像镜文件

    挂载命令(mount) 命令格式:mount [-t vfstype] [-o options] device dir其中:1.-t vfstype 指定文件系统的类型,通常不必指定.mount 会自 ...

  8. scrapy之手机app抓包爬虫

    手机App抓包爬虫 1. items.py class DouyuspiderItem(scrapy.Item): name = scrapy.Field()# 存储照片的名字 imagesUrls ...

  9. 从0开始 数据结构 AC自动机 模板(from kkke)

    AC自动机模板 2.4.1 头文件&宏&全局变量 #include <queue> #define MAXN 666666 #define MAXK 26//字符数量 st ...

  10. JavaScript高级程序设计-读书笔记(5)

    第13章 事件 1.事件流 事件流描述的是从页面中接收事件的顺序.IE的事件流是事件冒泡流,而Netscape Communicator的事件流是事件捕获流. (1)事件冒泡,即事件开始时由最具体的元 ...