python 各种函数及应用技巧

1.

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #此方法,可以临时屏蔽警告信息

2.

.placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。

  • import tensorflow as tf

  • import numpy as np

  •  
  • 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:[3.], input2: [4.]})

3.

tf.variable_scope()会在模型中开辟各自的空间,而其中的变量均在这个空间内进行管理

get_variable(),它会去搜索变量名,有就直接用,没有再新建

4.

np.newaxis的作用是增加一个维度。
对于[: , np.newaxis] 和 [np.newaxis,:]
是在np.newaxis这里增加1维

5.

np.linspace(-1, 1, 300) 参数前俩个参数为随机数字范围,第三个参数为列表长度

6.

 

参数: learning_rate: A Tensor or a floating point value. 要使用的学习率 use_locking: 要是True的话,就对于更新操作(update operations.)使用锁 name: 名字,可选,默认是”GradientDescent”

minimize() 函数处理了梯度计算和参数更新两个操作

7.

tf.nn.relu()函数的目的是,将输入小于0的值幅值为0,输入大于0的值不变。

8.

tf.square( )求平方

9.误差函数的更新过程

import tensorflow as tf

x = tf.Variable(2, name='x', dtype=tf.float32)
log_x = tf.log(x)
log_x_squared = tf.square(log_x)

optimizer = tf.train.GradientDescentOptimizer(0.5)

temp=compute_gradients(log_x_squared)

train = optimizer.minimize(log_x_squared)

init = tf.initialize_all_variables()


def optimize():
    with tf.Session() as session:
        session.run(init)
        print("starting at", "x:", session.run(x), "log(x)^2:", session.run(log_x_squared),"tidu:",session.run(temp))
        for step in range(10):
            session.run(train)
            print("step", step, "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))


optimize()

10.np.argmax(a)

返回的是a中元素最大值所对应的索引值

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值