函数引入
# 条件语句为真时返回A,条件为假时返回B
tf.where(条件语句,A,B)
# 实例
import tensorflow as tf
a = tf.constant([1,2,3,1,2])
b = tf.constant([2,4,5,7,1])
c = tf.where(tf.greater(a,b),1,-1)
print(c)
# 返回一个[0,1)之间的随机数。不传维度参数,返回一个标量
np.random.RandomState.rand(维度)
# 实例
import numpy as np
RS = np.random.RandomState(seed=1)
a = RS.rand()
b = RS.rand(2,3)
print(a)
print(b)
# 两个数组纵向叠加
np.vstack(数组1,数组2)
# 实例
a1 = np.array([1,2,3])
a2 = np.array([4,5,6])
merge = np.vstack((a1,a2))
print(merge)
# 生成多维数组,每一维采用“起始值:终止值:步长”
np.mgrid[第1维,第2维,...,第n维]
# 将变量的维度进行展开,变为一维数组
变量.ravel()
# 使其中的数组进行配对
np.c[数组1,数组2,...,数组n]
# 实例
x,y = np.mgrid[2:5:1,2:10:2]
res = np.c_[x.ravel(),y.ravel()]
print(res)
激活函数
损失函数
均方误差
公式:
其中:
n:数据个数
y:真实值
y _:预测值
loss_mes = tf.reduce_mean(tf.square(y,y_))
# tf.reduce_mean:累加求平均
# tf.square:两数之差的平方
交叉熵损失函数
公式:
tf.losses.categorical_crossentropy(预测值,准确值)
softmax与交叉熵结合
tf.nn.softmax_cross_entropy_with_logits(预测值,准确值)