监督学习的两大类为分类问题与回归问题
定义常量:tf.constant([1.0,2.0],name="a")
定义变量:tf.Variable进行变量声明
w2=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
tf.random_normal生成一些随机数:
tf.random_normal([2,3],stddev=1,seed=1)
例
import tensorflow as tfdata = tf.random_normal([2,3],stddev=1,seed=1) #输出一个2*3的矩阵with tf.Session() as sess: a = sess.run(data)
print(a)
结果:
[[-0.8113182 1.4845988 0.06532937]
[-2.4427042 0.0992484 0.5912243 ]]
即生成2*3的矩阵
定义会话:
with tf.Session() as sess: #此处掉了(),运行时报错AttributeError: __enter__ init_op = tf.global_variables_initializer()#初始化变量 sess.run(init_op) sess.run(w1) sess.run(w2)
import tensorflow as tfa=tf.constant(3,name='x')b=tf.constant(4,name='y')# a=3# b=4c=tf.add(a,b)with tf.Session() as session: # init = tf.global_variables_initializer() # session.run(init) print(session.run(c))
结果
7
初始化所有变量:
init_op = tf.global_variables_initializer()#初始化变量
一些数学表达公式:
矩阵乘法:tf.matmul(x,y)
如a=tf.matmul(x,w1)
创建占位符tf.placeholder
tf.placeholder(dtype, shape=None, name=None)
placeholder,中文意思是占位符
此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值
(在tensorflow中类似于函数参数,运行时必须传入值)
dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
name:名称。
在session运行阶段,需要给placeholder提供数据,利用feed_dict的字典结构给placeholdr变量“喂数据”
import tensorflow as tf
a=tf.placeholder(tf.float32)
b=tf.placeholder(tf.float32)
c=tf.add(a,b)
with tf.Session() as sess:
print(sess.run(c,feed_dict={a:10,b:30})) #把10赋给a,30赋给b
结果
40.0
import tensorflow as tf
# 定义placeholder
input1 = tf.placeholder(tf.float32,[2,3])
input2 = tf.placeholder(tf.float32,[3,2])
# 定义乘法运算
output = tf.matmul(input1, input2)
# 通过session执行乘法运行
with tf.Session() as sess:
# 执行时要传入placeholder的值
print(sess.run(output, feed_dict = {input1:[[7.,5,6],[5,9,6]], input2: [[2.,5],[3,5],[8,8]]}))
结果
[[ 77. 108.]
[ 85. 118.]]
placeholder与variable的区别:
它在使用的时候和前面的variable不同的是在session运行阶段,需要给placeholder提供数据,利用feed_dict的字典结构给placeholdr变量“喂数据”。
tf.Session.run会话
run( fetches, feed_dict=None, options=None, run_metadata=None)
tf.Session.run() 执行 fetches 中的操作,计算 fetches 中的张量值。
这个函数执行一步 TensorFlow 运算,通过运行必要的图块来执行每一个操作,并且计算每一个 fetches 中的张量的值,用相关的输入变量替换 feed_dict 中的值。
fetches 参数可能是一个单一图元素,或者任意嵌套列表,元组,namedtuple,字典,或者有序字典在叶子中包含图元素。
tf.Session.run()函数返回值为fetches的执行结果。如果fetches是一个元素就返回一个值;若fetches是一个list,则返回list的值,若fetches是一个字典类型,则返回和fetches同keys的字典。
代价函数在有些地方也称损失函数,目前常用的几种代价函数有二次代价函数、交叉熵代价函数、对数释然函数等。
二次代价函数
代价函数在有些地方也称损失函数,目前常用的几种代价函数有二次代价函数、交叉熵代价函数、对数释然函数等。
为基本的用来计算结果与预测值之间的差异
tf.reduce_mean
tensorflow中有一类在tensor的某一维度上求值的函数。如:
求最大值tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)
求平均值tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)
参数1--input_tensor:待求值的tensor。
参数2--reduction_indices:在哪一维上求解。
参数(3)(4)可忽略
举例说明:
# 'x' is [[1., 2.]
# [3., 4.]]
x是一个2维数组,分别调用reduce_*函数如下:
首先求平均值:
tf.reduce_mean(x) ==> 2.5 #如果不指定第二个参数,那么就在所有的元素中取平均值
tf.reduce_mean(x, 0) ==> [2., 3.] #指定第二个参数为0,则第一维的元素取平均值,即每一列求平均值
tf.reduce_mean(x, 1) ==> [1.5, 3.5] #
指定第二个参数为1,则第二维的元素取平均值,即每一行求平均值
同理,还可用tf.reduce_max()求最大值等。
reduction_indices参数,表示函数的处理维度
需要注意的一点,在很多的时候,我们看到别人的代码中并没有reduction_indices这个参数,此时该参数取默认值None,将把input_tensor降到0维,也就是一个数。
tf.equal(a,b)
比较a,b的大小,存储结果为布尔型变量True/False这些
tf.argmax(y,1)
argmax()官方文档如下:
tf.argmax(input, dimension, name=None)
Returns the index with the largest value across dimensions of a tensor.
Args:
input: A Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, int16, int8, complex64, qint8, quint8, qint32.
dimension: A Tensor of type int32. int32, 0 <= dimension < rank(input). Describes which dimension of the input Tensor to reduce across. For vectors, use dimension = 0.
name: A name for the operation (optional).
Returns:
A Tensor of type int64.
dimension=0 按列找
dimension=1 按行找
tf.argmax()返回最大数值的下标
通常和tf.equal()一起使用,计算模型准确度
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
b = tf.constant([[1, 2, 3], [3, 2, 1], [4, 5, 6], [6, 5, 4]])
with tf.Session() as sess:
mm=sess.run(tf.argmax(b, 0)) #按列找
print(mm)
结果
[3 2 2]
b = tf.constant([[1, 2, 3], [3, 2, 1], [4, 5, 6], [6, 5, 4]])
with tf.Session() as sess:
mm=sess.run(tf.argmax(b, 1)) #按列找
print(mm)
结果
[2 0 2 0]
tf.log()函数来对括号里面张量求对数
例
import tensorflow as tfx = [1.,2.]
y = tf.log (x)
with tf.Session() as sess:
a=sess.run(y)
print(a)
结果
[0. 0.6931472]