TensorFlow基础知识
张量(Tensor)
张量可以看做是n维的数组,一阶张量表示形式是向量,二阶是一个二维数组。但在程序实际运行的过程中张量的返回值具有三个部分:节点的操作,数据维度shape,数据类型
import tensorflow as tf
a = tf.constant([1.0, 2.0], name="a")
b = tf.constant([1.0, 2.0], name="b")
result = a + b
print(result)
>>> Tensor("add:0", shape=(2,), dtype=float32)
会话(Session)
会话管理系统运行时所需要的资源,用run()指令来运行计算图,close()关闭并且释放资源,为了防止遗忘close()或者异常发生,使用Python中的with/as结构
with tf.Session() as sess:
sess.run()
# example
loss=tf.reduce_mean(tf.square(tf.reshape(pred,[-1])-tf.reshape(Y, [-1])))
train_op=tf.train.AdamOptimizer(learning_rate=lr).minimize(loss)
sess.run([train_op,loss],feed_dict={X:train_X[start:end],Y:train_Y[start:end]})
placeholder 机制
在定义网络模型的时候一般需要输入大量的数据,这样就会在计算图中产生大量的输入节点,但是利用率很低。placeholder就是为了解决在有限的输入节点上输入大量的数据
import tensorflow as tf
a = tf.placeholder(tf.float32,shape=(2),name='input')
b = tf.placeholder(tf.float32,shape=(2),name='input')
result = a + b
with tf.Session() as sess:
#feed_dict类型是字典,必须要把所有输入都包括,否则会报错
print(sess.run(result,feed_dict={a:[1.0, 2.0],b:[3.0, 4.0]}))
print(result)
>>> [4. 6.]
Tensor("add:0", shape=(2,), dtype=float32)
变量(variable)
变量的创建使用Variable()函数,常用的随机数和常熟生成函数如下:
随机生成函数 | 描述 | 常数函数 | 描述 |
---|---|---|---|
random_normal() | 正态分布 | zeros() | 全0 |
random_uniform() | 平均分布 | ones() | 全1 |
random_gamma() | 伽马(Gamma)分布 | fill() | 全一个数 |
constant() | 全给定 |
# random_normal(shape= ,mean=, stddev= ,dtype= , seed= ,name= )
weights = tf.Variable(tf.random_normal([3, 4],mean=1, stddev=1))
bias = tf.Variable(tf.zeros([3],int32)
# 变量使用之前要被初始
sess.run(weights.initializer)
# 或者初始化所有变量
init_op = tf.initialize_all_variables()
sess.run(init_op)
深度学习中的TensorFlow
激活函数
函数 | 数学表达 |
---|---|
tf.nn.sigmoid(x, name = None) | y = 1 / (1 + exp(-x)) |
tf.nn.tanh(x, name = None) | y = (exp(x) - exp(-x)) / (exp(x) + exp(-x)) |
tf.nn.relu(features, name=None) | y = max(features, 0) |
tf.nn.softplus(features, name=None) | y = log(exp(features) + 1) |
# matmul()表示矩阵乘法,* 表示对应位置元素相乘
y = tf.nn.relu(tf.matmul(x, w1)+b1)
损失函数
均方误差损失函数
MSE = \frac{\sum_{i=1}^{n}(y_{i}-y)^2}{n}
loss = tf.reduce_mean(tf.square(tf.reshape(pred,[-1])-tf.reshape(Y, [-1])))
交叉熵损失函数
假设P是真实值,Q是预测结果,那么Q对概率分布P估计的准确程度可以用如下公式描述
H(P,Q)=-\sum_{X}{P(X)}{logQ(x)}
可以用如下函数拼装交叉熵函数(y_真实值,y预测值)
# 用于求平均值
reduce_mean()
# 对数运算
log(x,name)
# 将张量中数值限制在一个范围内,低于设定的换为clip_value_min,高于clip_value_max
clip_by_value(t,clip_value_min,clip_value_max,name)
#交叉熵函数为
cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y,le-10,1.0)))
softmax交叉熵函数
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(y_, y)
tensorflow优化器
描述 | 函数 |
---|---|
梯度下降优化器 | tf.train.GradientDescentOptimizer(learning_rate) |
自适应的梯度下降算法 | tf.train.AdagradOptimizer(self,learning_rate,initial_accumulator_value=0.1) |
自适应学习率优化器 | tf.train.AdamOptimizer(learning_rate) |
学习率使用人过来控制梯度下降中参数更新的速度或者(幅度),太小会导致更新缓慢,太大会导致参数在最小值两侧摇摆。一般凭经验设置
RNN实现函数
def dynamic_rnn(cell, inputs, sequence_length=None, initial_state=None,
dtype=None, parallel_iterations=None, swap_memory=False,
time_major=False, scope=None):
"""Creates a recurrent neural network specified by RNNCell `cell`."""
Performs fully dynamic unrolling of `inputs`.
Example:
# create a BasicRNNCell
rnn_cell = tf.nn.rnn_cell.BasicRNNCell(hidden_size)
# 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size]
# defining initial state
initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32)
# 'state' is a tensor of shape [batch_size, cell_state_size]
outputs, state = tf.nn.dynamic_rnn(rnn_cell, input_data,
initial_state=initial_state,
dtype=tf.float32)
长短期记忆实现函数
TensorFlow提供了BasicLSTMCell()函数来完成基础的LSTM单元操作
class BasicLSTMCell(RNNCell):
def __init__(self, num_units, forget_bias=1.0,
state_is_tuple=True, activation=None, reuse=None):
"""Initialize the basic LSTM cell.
Args:
num_units: int, The number of units in the LSTM cell.
forget_bias: float, The bias added to forget gates (see above).
state_is_tuple: If True, accepted and returned states are 2-tuples of
the `c_state` and `m_state`. If False, they are concatenated
along the column axis. The latter behavior will soon be deprecated.
activation: Activation function of the inner states. Default: `tanh`.
reuse: (optional) Python boolean describing whether to reuse variables
in an existing scope. If not `True`, and the existing scope already has
the given variables, an error is raised.
When restoring from CudnnLSTM-trained checkpoints, must use
CudnnCompatibleLSTMCell instead.
"""
# 函数的返回值是隐藏状态h 和new_state = LSTMStateTuple(new_c, new_h)
数据读取
简单读取
fname =open('1m.csv',encoding='UTF-8')
df = pd.read_csv(fname)
depth = np.array(df['Depth(m)'])
new_depth=depth.tolist()
队列加载
模型训练与保存
train.Saver()提供了保存和还原模型的API
保存模型
# 定义Saver类来保存模型
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run()
saver.save(sess,"/home/.../model.ckpt")
模型保存之后会生成四个文件:checkpoint、model.data-00000-of-00001、model.index、model.meta
加载训练好的模型
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,"/home/.../model.ckpt")
sess.run(result)
tensorboard可视化
生成一个写日志的writer,讲图写入指定文件,之后在终端输入命令打开日志文件
import tensorflow as tf
a = tf.placeholder(tf.float32,shape=(2),name='x-input')
b = tf.placeholder(tf.float32,shape=(2),name='y-input')
result = a + b
with tf.Session() as sess:
writer = tf.summary.FileWriter("E:\Code\Python_shuizhiyuce\log", tf.get_default_graph())
print(sess.run(result,feed_dict={a:[1.0, 2.0],b:[3.0, 4.0]}))
print(result)
writer.close()
tensorboard --logdir=E:\Code\Python_shuizhiyuce\log