RNN入門:利用TF的API(二)
上一篇中手工構建了RNN網路,這裡介紹如何利用TensorFlow(r1.1)的API簡化這一程式碼。
構建模型
將建模部分的程式碼替換為:
cell = tf.contrib.rnn.BasicRNNCell(state_size)
current_state = init_state
states_series = []
for current_input in inputs_series:
with tf.variable_scope('rnn') as vs:
try:
output, current_state = cell(current_input, current_state)
except:
vs.reuse_variables()
output, current_state = cell(current_input, current_state)
states_series.append(current_state)
需要注意的是,tf.contrib.rnn.BasicRNNCell函式每次都會宣告一次變數,這會導致第二次呼叫失敗。所以需要加入try語句,在異常時宣告vs.reuse_variables()。
全部程式碼
from __future__ import print_function, division
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
num_epochs = 100
total_series_length = 50000
truncated_backprop_length = 15
state_size = 4
num_classes = 2
echo_step = 3
batch_size = 5
num_batches = total_series_length//batch_size//truncated_backprop_length
def generateData():
x = np.array(np.random.choice(2, total_series_length, p=[0.5, 0.5]))
y = np.roll(x, echo_step)
y[0:echo_step] = 0
x = x.reshape((batch_size, -1)) # The first index changing slowest, subseries as rows
y = y.reshape((batch_size, -1))
return (x, y)
batchX_placeholder = tf.placeholder(tf.float32, [batch_size, truncated_backprop_length])
batchY_placeholder = tf.placeholder(tf.int32, [batch_size, truncated_backprop_length])
init_state = tf.placeholder(tf.float32, [batch_size, state_size])
W2 = tf.Variable(np.random.rand(state_size, num_classes),dtype=tf.float32)
b2 = tf.Variable(np.zeros((1,num_classes)), dtype=tf.float32)
# Unpack columns
inputs_series = tf.split(batchX_placeholder, truncated_backprop_length, axis=1)
labels_series = tf.unstack(batchY_placeholder, axis=1)
# Forward passes
cell = tf.contrib.rnn.BasicRNNCell(state_size)
current_state = init_state
states_series = []
for current_input in inputs_series:
with tf.variable_scope('rnn') as vs:
try:
output, current_state = cell(current_input, current_state)
except:
vs.reuse_variables()
output, current_state = cell(current_input, current_state)
states_series.append(current_state)
logits_series = [tf.matmul(state, W2) + b2 for state in states_series] #Broadcasted addition
predictions_series = [tf.nn.softmax(logits) for logits in logits_series]
losses = [tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels) for logits, labels in zip(logits_series,labels_series)]
total_loss = tf.reduce_mean(losses)
train_step = tf.train.AdagradOptimizer(0.3).minimize(total_loss)
def plot(loss_list, predictions_series, batchX, batchY):
plt.subplot(2, 3, 1)
plt.cla()
plt.plot(loss_list)
for batch_series_idx in range(5):
one_hot_output_series = np.array(predictions_series)[:, batch_series_idx, :]
single_output_series = np.array([(1 if out[0] < 0.5 else 0) for out in one_hot_output_series])
plt.subplot(2, 3, batch_series_idx + 2)
plt.cla()
plt.axis([0, truncated_backprop_length, 0, 2])
left_offset = range(truncated_backprop_length)
plt.bar(left_offset, batchX[batch_series_idx, :], width=1, color="blue")
plt.bar(left_offset, batchY[batch_series_idx, :] * 0.5, width=1, color="red")
plt.bar(left_offset, single_output_series * 0.3, width=1, color="green")
plt.draw()
plt.pause(0.0001)
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
plt.ion()
plt.figure()
plt.show()
loss_list = []
for epoch_idx in range(num_epochs):
x,y = generateData()
_current_state = np.zeros((batch_size, state_size))
print("New data, epoch", epoch_idx)
for batch_idx in range(num_batches):
start_idx = batch_idx * truncated_backprop_length
end_idx = start_idx + truncated_backprop_length
batchX = x[:,start_idx:end_idx]
batchY = y[:,start_idx:end_idx]
_total_loss, _train_step, _current_state, _predictions_series = sess.run(
[total_loss, train_step, current_state, predictions_series],
feed_dict={
batchX_placeholder:batchX,
batchY_placeholder:batchY,
init_state:_current_state
})
loss_list.append(_total_loss)
if batch_idx%100 == 0:
print("Step",batch_idx, "Loss", _total_loss)
plot(loss_list, _predictions_series, batchX, batchY)
plt.ioff()
plt.show()
參考文獻:
相關文章
- Elasticsearch 入門實戰(8)--REST API 使用二(Search API)ElasticsearchRESTAPI
- tensorflow教程:tf.contrib.rnn.DropoutWrapperRNNAPP
- Elasticsearch 入門實戰(9)--Java API Client 使用二ElasticsearchJavaAPIclient
- GDAL API入門API
- 利用Google API生成二維碼GoAPI
- Tungsten Fabric入門寶典丨TF元件的七種“武器”元件
- 網路流量預測入門(一)之RNN 介紹RNN
- gRPC(二)入門:Protobuf入門RPC
- Web API--入門--(一)ASP.NET Web API 2(C#)入門WebAPIASP.NET
- 電商API介面入門指南API
- REST API 最佳入門指南RESTAPI
- mySql入門-(二)MySql
- 利用MAT分析JVM記憶體問題,從入門到精通(二)JVM記憶體
- ElasticSearch的Java Api基本操作入門指南ElasticsearchJavaAPI
- LittleVGL (LVGL)乾貨入門教程二之LVGL的輸入裝置(indev)API對接。devAPI
- Storm入門指南第二章 入門ORM
- java8 Stream APi 入門JavaAPI
- ArcGis api配合vue開發入門系列(二)距離以及面積的測量APIVue
- 第二章 Redis API的使用 單執行緒介紹【Redis入門教程】RedisAPI執行緒
- Flutter入門篇(二)Flutter
- Go快速入門(二)Go
- spring入門(二)Spring
- Vue快速入門(二)Vue
- Electron 入門指北(二)
- RNN二進位制加法例項RNN
- Android開發者的Flutter入門(二)AndroidFlutter
- npm入門(二)—package(包)的管理NPMPackage
- Docker for windows 入門二(Kitematic的使用)DockerWindows
- 五分鐘入門 Dingo APIGoAPI
- Java8 - Stream API快速入門JavaAPI
- OpenAI Chat completion API 入門指南OpenAIAPI
- Web Animation API從入門到上座WebAPI
- 利用SQLLDR載入包含LOB物件的資料(二)SQL物件
- 五線譜入門(二)
- [譯】Redux入門教程(二)Redux
- Flask二之快速入門Flask
- Flutter入門——山寨掘金(二)Flutter
- HTML入門基礎(二)HTML