LSTM拟合正弦曲线代码(转载)
生活随笔
收集整理的這篇文章主要介紹了
LSTM拟合正弦曲线代码(转载)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于LSTM給大家推薦一篇講解的十分好的博文:
難以置信!LSTM和GRU的解析從未如此清晰(動圖+視頻)
?https://blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/82922386
import tensorflow as tf import numpy as np import matplotlib.pyplot as pltBATCH_START = 0 #建立 batch data 時候的 index TIME_STEPS = 20 # backpropagation through time 的time_steps BATCH_SIZE = 50 INPUT_SIZE = 1 # x數據輸入size OUTPUT_SIZE = 1 # cos數據輸出 size CELL_SIZE = 10 # RNN的 hidden unit size LR = 0.006 # learning rate# 定義一個生成數據的 get_batch function: def get_batch():#global BATCH_START, TIME_STEPS# xs shape (50batch, 20steps)xs = np.arange(BATCH_START, BATCH_START+TIME_STEPS*BATCH_SIZE).reshape((BATCH_SIZE, TIME_STEPS)) / (10*np.pi)res = np.cos(xs)# returned xs and res: shape (batch, step, input)return [xs[:, :, np.newaxis], res[:, :, np.newaxis]]# 定義 LSTMRNN 的主體結構 class LSTMRNN(object):def __init__(self, n_steps, input_size, output_size, cell_size, batch_size):self.n_steps = n_stepsself.input_size = input_sizeself.output_size = output_sizeself.cell_size = cell_sizeself.batch_size = batch_sizewith tf.name_scope('inputs'):self.xs = tf.placeholder(tf.float32, [None, n_steps, input_size], name='xs')self.ys = tf.placeholder(tf.float32, [None, n_steps, output_size], name='ys')with tf.variable_scope('in_hidden'):self.add_input_layer()with tf.variable_scope('LSTM_cell'):self.add_cell()with tf.variable_scope('out_hidden'):self.add_output_layer()with tf.name_scope('cost'):self.compute_cost()with tf.name_scope('train'):self.train_op = tf.train.AdamOptimizer(LR).minimize(self.cost)# 設置 add_input_layer 功能, 添加 input_layer:def add_input_layer(self, ):l_in_x = tf.reshape(self.xs, [-1, self.input_size], name='2_2D') # (batch*n_step, in_size)# Ws (in_size, cell_size)Ws_in = self._weight_variable([self.input_size, self.cell_size])# bs (cell_size, )bs_in = self._bias_variable([self.cell_size, ])# l_in_y = (batch * n_steps, cell_size)with tf.name_scope('Wx_plus_b'):l_in_y = tf.matmul(l_in_x, Ws_in) + bs_in# reshape l_in_y ==> (batch, n_steps, cell_size)self.l_in_y = tf.reshape(l_in_y, [-1, self.n_steps, self.cell_size], name='2_3D')# 設置 add_cell 功能, 添加 cell, 注意這里的 self.cell_init_state,# 因為我們在 training 的時候, 這個地方要特別說明.def add_cell(self):lstm_cell = tf.contrib.rnn.BasicLSTMCell(self.cell_size, forget_bias=1.0, state_is_tuple=True)with tf.name_scope('initial_state'):self.cell_init_state = lstm_cell.zero_state(self.batch_size, dtype=tf.float32)self.cell_outputs, self.cell_final_state = tf.nn.dynamic_rnn(lstm_cell, self.l_in_y,initial_state=self.cell_init_state,time_major=False)# 設置 add_output_layer 功能, 添加 output_layer:def add_output_layer(self):# shape = (batch * steps, cell_size)l_out_x = tf.reshape(self.cell_outputs, [-1, self.cell_size], name='2_2D')Ws_out = self._weight_variable([self.cell_size, self.output_size])bs_out = self._bias_variable([self.output_size, ])# shape = (batch * steps, output_size)with tf.name_scope('Wx_plus_b'):self.pred = tf.matmul(l_out_x, Ws_out) + bs_out# 添加 RNN 中剩下的部分:def compute_cost(self):losses = tf.contrib.legacy_seq2seq.sequence_loss_by_example([tf.reshape(self.pred, [-1], name='reshape_pred')],[tf.reshape(self.ys, [-1], name='reshape_target')],[tf.ones([self.batch_size * self.n_steps], dtype=tf.float32)],average_across_timesteps=True,softmax_loss_function=self.ms_error,name='losses')with tf.name_scope('average_cost'):self.cost = tf.div(tf.reduce_sum(losses, name='losses_sum'),self.batch_size,name='average_cost')tf.summary.scalar('cost', self.cost)def ms_error(self,labels, logits):return tf.square(tf.subtract(labels, logits))def _weight_variable(self, shape, name='weights'):initializer = tf.random_normal_initializer(mean=0., stddev=1., )return tf.get_variable(shape=shape, initializer=initializer, name=name)def _bias_variable(self, shape, name='biases'):initializer = tf.constant_initializer(0.1)return tf.get_variable(name=name, shape=shape, initializer=initializer)# 訓練 LSTMRNN if __name__ == '__main__':# 搭建 LSTMRNN 模型model = LSTMRNN(TIME_STEPS, INPUT_SIZE, OUTPUT_SIZE, CELL_SIZE, BATCH_SIZE)sess = tf.Session()saver=tf.train.Saver(max_to_keep=3)sess.run(tf.global_variables_initializer()) t = 0 if(t == 1):model_file=tf.train.latest_checkpoint('model/')saver.restore(sess,model_file )xs, res = get_batch() # 提取 batch datafeed_dict = {model.xs: xs}pred = sess.run( model.pred,feed_dict=feed_dict)xs.shape = (-1,1)res.shape = (-1, 1)pred.shape = (-1, 1)print(xs.shape,res.shape,pred.shape)plt.figure()plt.plot(xs,res,'-r')plt.plot(xs,pred,'--g') plt.show()else: # matplotlib可視化plt.ion() # 設置連續 plotplt.show() # 訓練多次for i in range(2500):xs, res = get_batch() # 提取 batch data# 初始化 datafeed_dict = {model.xs: xs,model.ys: res,} # 訓練_, cost, state, pred = sess.run([model.train_op, model.cost, model.cell_final_state, model.pred],feed_dict=feed_dict)# plottingx = xs.reshape(-1,1)r = res.reshape(-1, 1)p = pred.reshape(-1, 1)plt.clf()plt.plot(x, r, 'r', x, p, 'b--')plt.ylim((-1.2, 1.2))plt.draw()plt.pause(0.3) # 每 0.3 s 刷新一次# 打印 cost 結果if i % 20 == 0:saver.save(sess, "model/lstem_text.ckpt",global_step=i)#print('cost: ', round(cost, 4))x值較小的點先收斂,x值大的收斂速度很慢。其原因主要是BPTT的求導過程,對于時間靠前的梯度下降快
將網絡結構改為雙向循環神經網絡可以改善這個問題。
def add_cell(self):lstm_cell = tf.contrib.rnn.BasicLSTMCell(self.cell_size, forget_bias=1.0, state_is_tuple=True)lstm_cell = tf.contrib.rnn.MultiRNNCell([lstm_cell],1)with tf.name_scope('initial_state'):self.cell_init_state = lstm_cell.zero_state(self.batch_size, dtype=tf.float32)self.cell_outputs, self.cell_final_state = tf.nn.dynamic_rnn(lstm_cell, self.l_in_y,initial_state=self.cell_init_state,time_major=False)對于分類問題,其實和回歸是一樣的,假設在上面的正弦函數的基礎上,若y大于0標記為1,y小于0標記為0,則輸出變成了一個n_class(n個類別)的向量,本例中兩個維度分別代表標記為0的概率和標記為1的概率。
需要修改的地方為:
首先是數據產生函數,添加一個打標簽的過程:
# 定義一個生成數據的 get_batch function: def get_batch():#global BATCH_START, TIME_STEPS# xs shape (50batch, 20steps)xs = np.arange(BATCH_START, BATCH_START+TIME_STEPS*BATCH_SIZE).reshape((BATCH_SIZE, TIME_STEPS)) / (200*np.pi)res = np.where(np.cos(4*xs)>=0,0,1).tolist()for i in range(BATCH_SIZE):for j in range(TIME_STEPS): res[i][j] = [0,1] if res[i][j] == 1 else [1,0]# returned xs and res: shape (batch, step, input/output)return [xs[:, :, np.newaxis], np.array(res)]然后修改損失函數,回歸問題就不能用最小二乘的損失了,可以采用交叉熵損失函數:
def compute_cost(self):self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = self.ys,logits = self.pred))?
總結
以上是生活随笔為你收集整理的LSTM拟合正弦曲线代码(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux系统管理实验报告总结_Linu
- 下一篇: 两数相加python代码实现(leetc