Skip to content

Commit b13b9ac

Browse files
committed
add bilstm examples
1 parent 3ef6826 commit b13b9ac

File tree

3 files changed

+326
-4
lines changed

3 files changed

+326
-4
lines changed

14.RNN&LSTM/RNN.py

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,123 @@
1-
sum=0
2-
for i in range(1,101):
3-
sum+=i*(i+1)*(2*i+3)
1+
""" Bi-directional Recurrent Neural Network.
2+
A Bi-directional Recurrent Neural Network (LSTM) implementation example using
3+
TensorFlow library. This example is using the MNIST database of handwritten
4+
digits (http://yann.lecun.com/exdb/mnist/)
5+
Links:
6+
[Long Short Term Memory](http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf)
7+
[MNIST Dataset](http://yann.lecun.com/exdb/mnist/).
8+
Author: Aymeric Damien
9+
Project: https://github.com/aymericdamien/TensorFlow-Examples/
10+
"""
411

5-
print(sum)
12+
from __future__ import print_function
613

14+
import tensorflow as tf
15+
from tensorflow.contrib import rnn
16+
import numpy as np
17+
18+
# Import MNIST data
19+
from tensorflow.examples.tutorials.mnist import input_data
20+
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
21+
22+
'''
23+
To classify images using a bidirectional recurrent neural network, we consider
24+
every image row as a sequence of pixels. Because MNIST image shape is 28*28px,
25+
we will then handle 28 sequences of 28 steps for every sample.
26+
'''
27+
28+
# Training Parameters
29+
learning_rate = 0.001
30+
training_steps = 10000
31+
batch_size = 128
32+
display_step = 200
33+
34+
# Network Parameters
35+
num_input = 28 # MNIST data input (img shape: 28*28)
36+
timesteps = 28 # timesteps
37+
num_hidden = 128 # hidden layer num of features
38+
num_classes = 10 # MNIST total classes (0-9 digits)
39+
40+
# tf Graph input
41+
X = tf.placeholder("float", [None, timesteps, num_input])
42+
Y = tf.placeholder("float", [None, num_classes])
43+
44+
# Define weights
45+
weights = {
46+
# Hidden layer weights => 2*n_hidden because of forward + backward cells
47+
'out': tf.Variable(tf.random_normal([2*num_hidden, num_classes]))
48+
}
49+
biases = {
50+
'out': tf.Variable(tf.random_normal([num_classes]))
51+
}
52+
53+
54+
def BiRNN(x, weights, biases):
55+
56+
# Prepare data shape to match `rnn` function requirements
57+
# Current data input shape: (batch_size, timesteps, n_input)
58+
# Required shape: 'timesteps' tensors list of shape (batch_size, num_input)
59+
60+
# Unstack to get a list of 'timesteps' tensors of shape (batch_size, num_input)
61+
x = tf.unstack(x, timesteps, 1)
62+
63+
# Define lstm cells with tensorflow
64+
# Forward direction cell
65+
lstm_fw_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)
66+
# Backward direction cell
67+
lstm_bw_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)
68+
69+
# Get lstm cell output
70+
try:
71+
outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
72+
dtype=tf.float32)
73+
except Exception: # Old TensorFlow version only returns outputs not states
74+
outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
75+
dtype=tf.float32)
76+
77+
# Linear activation, using rnn inner loop last output
78+
return tf.matmul(outputs[-1], weights['out']) + biases['out']
79+
80+
logits = BiRNN(X, weights, biases)
81+
prediction = tf.nn.softmax(logits)
82+
83+
# Define loss and optimizer
84+
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
85+
logits=logits, labels=Y))
86+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
87+
train_op = optimizer.minimize(loss_op)
88+
89+
# Evaluate model (with test logits, for dropout to be disabled)
90+
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
91+
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
92+
93+
# Initialize the variables (i.e. assign their default value)
94+
init = tf.global_variables_initializer()
95+
96+
# Start training
97+
with tf.Session() as sess:
98+
99+
# Run the initializer
100+
sess.run(init)
101+
102+
for step in range(1, training_steps+1):
103+
batch_x, batch_y = mnist.train.next_batch(batch_size)
104+
# Reshape data to get 28 seq of 28 elements
105+
batch_x = batch_x.reshape((batch_size, timesteps, num_input))
106+
# Run optimization op (backprop)
107+
sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
108+
if step % display_step == 0 or step == 1:
109+
# Calculate batch loss and accuracy
110+
loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,
111+
Y: batch_y})
112+
print("Step " + str(step) + ", Minibatch Loss= " + \
113+
"{:.4f}".format(loss) + ", Training Accuracy= " + \
114+
"{:.3f}".format(acc))
115+
116+
print("Optimization Finished!")
117+
118+
# Calculate accuracy for 128 mnist test images
119+
test_len = 128
120+
test_data = mnist.test.images[:test_len].reshape((-1, timesteps, num_input))
121+
test_label = mnist.test.labels[:test_len]
122+
print("Testing Accuracy:", \
123+
sess.run(accuracy, feed_dict={X: test_data, Y: test_label}))

14.RNN&LSTM/bilstm_mnist.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import numpy as np
2+
import pandas as pd
3+
import tensorflow as tf
4+
import tensorflow.contrib.rnn as rnn
5+
import matplotlib.pyplot as plt
6+
7+
8+
TIME_STEPS=28
9+
BATCH_SIZE=128
10+
HIDDEN_UNITS1=30
11+
HIDDEN_UNITS=10
12+
LEARNING_RATE=0.001
13+
EPOCH=50
14+
15+
TRAIN_EXAMPLES=42000
16+
TEST_EXAMPLES=28000
17+
18+
#------------------------------------Generate Data-----------------------------------------------#
19+
#generate data
20+
train_frame = pd.read_csv("../Mnist/train.csv")
21+
test_frame = pd.read_csv("../Mnist/test.csv")
22+
23+
# pop the labels and one-hot coding
24+
train_labels_frame = train_frame.pop("label")
25+
26+
# get values
27+
# one-hot on labels
28+
X_train = train_frame.astype(np.float32).values
29+
y_train=pd.get_dummies(data=train_labels_frame).values
30+
X_test = test_frame.astype(np.float32).values
31+
32+
#trans the shape to (batch,time_steps,input_size)
33+
X_train=np.reshape(X_train,newshape=(-1,28,28))
34+
X_test=np.reshape(X_test,newshape=(-1,28,28))
35+
#print(X_train.shape)
36+
#print(y_dummy.shape)
37+
#print(X_test.shape)
38+
39+
#-----------------------------------------------------------------------------------------------------#
40+
41+
42+
#--------------------------------------Define Graph---------------------------------------------------#
43+
graph=tf.Graph()
44+
with graph.as_default():
45+
46+
#------------------------------------construct LSTM------------------------------------------#
47+
#place hoder
48+
X_p=tf.placeholder(dtype=tf.float32,shape=(None,TIME_STEPS,28),name="input_placeholder")
49+
y_p=tf.placeholder(dtype=tf.float32,shape=(None,10),name="pred_placeholder")
50+
51+
#lstm instance
52+
lstm_forward=rnn.BasicLSTMCell(num_units=HIDDEN_UNITS)
53+
lstm_backward=rnn.BasicLSTMCell(num_units=HIDDEN_UNITS)
54+
55+
outputs,states=tf.nn.bidirectional_dynamic_rnn(
56+
cell_fw=lstm_forward,
57+
cell_bw=lstm_backward,
58+
inputs=X_p,
59+
dtype=tf.float32
60+
)
61+
62+
outputs_fw=outputs[0]
63+
outputs_bw = outputs[1]
64+
h=outputs_fw[:,-1,:]+outputs_bw[:,-1,:]
65+
# print(h.shape)
66+
#---------------------------------------;-----------------------------------------------------#
67+
68+
#---------------------------------define loss and optimizer----------------------------------#
69+
cross_loss=tf.losses.softmax_cross_entropy(onehot_labels=y_p,logits=h)
70+
#print(loss.shape)
71+
72+
correct_prediction = tf.equal(tf.argmax(h, 1), tf.argmax(y_p, 1))
73+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
74+
75+
optimizer=tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss=cross_loss)
76+
77+
init=tf.global_variables_initializer()
78+
79+
80+
#-------------------------------------------Define Session---------------------------------------#
81+
with tf.Session(graph=graph) as sess:
82+
sess.run(init)
83+
for epoch in range(1,EPOCH+1):
84+
#results = np.zeros(shape=(TEST_EXAMPLES, 10))
85+
train_losses=[]
86+
accus=[]
87+
#test_losses=[]
88+
print("epoch:",epoch)
89+
for j in range(TRAIN_EXAMPLES//BATCH_SIZE):
90+
_,train_loss,accu=sess.run(
91+
fetches=(optimizer,cross_loss,accuracy),
92+
feed_dict={
93+
X_p:X_train[j*BATCH_SIZE:(j+1)*BATCH_SIZE],
94+
y_p:y_train[j*BATCH_SIZE:(j+1)*BATCH_SIZE]
95+
}
96+
)
97+
train_losses.append(train_loss)
98+
accus.append(accu)
99+
print("average training loss:", sum(train_losses) / len(train_losses))
100+
print("accuracy:",sum(accus)/len(accus))

14.RNN&LSTM/bilstm_mnist2.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import numpy as np
2+
import pandas as pd
3+
import tensorflow as tf
4+
import tensorflow.contrib.rnn as rnn
5+
import matplotlib.pyplot as plt
6+
7+
8+
TIME_STEPS=28
9+
BATCH_SIZE=128
10+
HIDDEN_UNITS1=30
11+
HIDDEN_UNITS=10
12+
LEARNING_RATE=0.001
13+
EPOCH=50
14+
15+
TRAIN_EXAMPLES=42000
16+
TEST_EXAMPLES=28000
17+
18+
#------------------------------------Generate Data-----------------------------------------------#
19+
#generate data
20+
train_frame = pd.read_csv("../Mnist/train.csv")
21+
test_frame = pd.read_csv("../Mnist/test.csv")
22+
23+
# pop the labels and one-hot coding
24+
train_labels_frame = train_frame.pop("label")
25+
26+
# get values
27+
# one-hot on labels
28+
X_train = train_frame.astype(np.float32).values
29+
y_train=pd.get_dummies(data=train_labels_frame).values
30+
X_test = test_frame.astype(np.float32).values
31+
32+
#trans the shape to (batch,time_steps,input_size)
33+
X_train=np.reshape(X_train,newshape=(-1,28,28))
34+
X_test=np.reshape(X_test,newshape=(-1,28,28))
35+
#print(X_train.shape)
36+
#print(y_dummy.shape)
37+
#print(X_test.shape)
38+
39+
#-----------------------------------------------------------------------------------------------------#
40+
41+
42+
#--------------------------------------Define Graph---------------------------------------------------#
43+
graph=tf.Graph()
44+
with graph.as_default():
45+
46+
#------------------------------------construct LSTM------------------------------------------#
47+
#place hoder
48+
X_p=tf.placeholder(dtype=tf.float32,shape=(None,TIME_STEPS,28),name="input_placeholder")
49+
y_p=tf.placeholder(dtype=tf.float32,shape=(None,10),name="pred_placeholder")
50+
51+
#lstm instance
52+
lstm_forward_1=rnn.BasicLSTMCell(num_units=HIDDEN_UNITS1)
53+
lstm_forward_2=rnn.BasicLSTMCell(num_units=HIDDEN_UNITS)
54+
lstm_forward=rnn.MultiRNNCell(cells=[lstm_forward_1,lstm_forward_2])
55+
56+
lstm_backward_1 = rnn.BasicLSTMCell(num_units=HIDDEN_UNITS1)
57+
lstm_backward_2 = rnn.BasicLSTMCell(num_units=HIDDEN_UNITS)
58+
lstm_backward=rnn.MultiRNNCell(cells=[lstm_backward_1,lstm_backward_2])
59+
60+
outputs,states=tf.nn.bidirectional_dynamic_rnn(
61+
cell_fw=lstm_forward,
62+
cell_bw=lstm_backward,
63+
inputs=X_p,
64+
dtype=tf.float32
65+
)
66+
67+
outputs_fw=outputs[0]
68+
outputs_bw = outputs[1]
69+
h=outputs_fw[:,-1,:]+outputs_bw[:,-1,:]
70+
# print(h.shape)
71+
#---------------------------------------;-----------------------------------------------------#
72+
73+
#---------------------------------define loss and optimizer----------------------------------#
74+
cross_loss=tf.losses.softmax_cross_entropy(onehot_labels=y_p,logits=h)
75+
#print(loss.shape)
76+
77+
correct_prediction = tf.equal(tf.argmax(h, 1), tf.argmax(y_p, 1))
78+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
79+
80+
optimizer=tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss=cross_loss)
81+
82+
init=tf.global_variables_initializer()
83+
84+
85+
#-------------------------------------------Define Session---------------------------------------#
86+
with tf.Session(graph=graph) as sess:
87+
sess.run(init)
88+
for epoch in range(1,EPOCH+1):
89+
#results = np.zeros(shape=(TEST_EXAMPLES, 10))
90+
train_losses=[]
91+
accus=[]
92+
#test_losses=[]
93+
print("epoch:",epoch)
94+
for j in range(TRAIN_EXAMPLES//BATCH_SIZE):
95+
_,train_loss,accu=sess.run(
96+
fetches=(optimizer,cross_loss,accuracy),
97+
feed_dict={
98+
X_p:X_train[j*BATCH_SIZE:(j+1)*BATCH_SIZE],
99+
y_p:y_train[j*BATCH_SIZE:(j+1)*BATCH_SIZE]
100+
}
101+
)
102+
train_losses.append(train_loss)
103+
accus.append(accu)
104+
print("average training loss:", sum(train_losses) / len(train_losses))
105+
print("accuracy:",sum(accus)/len(accus))

0 commit comments

Comments
 (0)