10
10
from __future__ import print_function
11
11
12
12
import tensorflow as tf
13
- from tensorflow .python . ops import rnn , rnn_cell
13
+ from tensorflow .contrib import rnn
14
14
import numpy as np
15
15
16
16
# Import MNIST data
@@ -60,20 +60,20 @@ def BiRNN(x, weights, biases):
60
60
# Reshape to (n_steps*batch_size, n_input)
61
61
x = tf .reshape (x , [- 1 , n_input ])
62
62
# Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)
63
- x = tf .split (0 , n_steps , x )
63
+ x = tf .split (x , n_steps , 0 )
64
64
65
65
# Define lstm cells with tensorflow
66
66
# Forward direction cell
67
- lstm_fw_cell = rnn_cell .BasicLSTMCell (n_hidden , forget_bias = 1.0 )
67
+ lstm_fw_cell = rnn .BasicLSTMCell (n_hidden , forget_bias = 1.0 )
68
68
# Backward direction cell
69
- lstm_bw_cell = rnn_cell .BasicLSTMCell (n_hidden , forget_bias = 1.0 )
69
+ lstm_bw_cell = rnn .BasicLSTMCell (n_hidden , forget_bias = 1.0 )
70
70
71
71
# Get lstm cell output
72
72
try :
73
- outputs , _ , _ = rnn .bidirectional_rnn (lstm_fw_cell , lstm_bw_cell , x ,
73
+ outputs , _ , _ = rnn .static_bidirectional_rnn (lstm_fw_cell , lstm_bw_cell , x ,
74
74
dtype = tf .float32 )
75
75
except Exception : # Old TensorFlow version only returns outputs not states
76
- outputs = rnn .bidirectional_rnn (lstm_fw_cell , lstm_bw_cell , x ,
76
+ outputs = rnn .static_bidirectional_rnn (lstm_fw_cell , lstm_bw_cell , x ,
77
77
dtype = tf .float32 )
78
78
79
79
# Linear activation, using rnn inner loop last output
@@ -82,15 +82,15 @@ def BiRNN(x, weights, biases):
82
82
pred = BiRNN (x , weights , biases )
83
83
84
84
# Define loss and optimizer
85
- cost = tf .reduce_mean (tf .nn .softmax_cross_entropy_with_logits (pred , y ))
85
+ cost = tf .reduce_mean (tf .nn .softmax_cross_entropy_with_logits (logits = pred , labels = y ))
86
86
optimizer = tf .train .AdamOptimizer (learning_rate = learning_rate ).minimize (cost )
87
87
88
88
# Evaluate model
89
89
correct_pred = tf .equal (tf .argmax (pred ,1 ), tf .argmax (y ,1 ))
90
90
accuracy = tf .reduce_mean (tf .cast (correct_pred , tf .float32 ))
91
91
92
92
# Initializing the variables
93
- init = tf .initialize_all_variables ()
93
+ init = tf .global_variables_initializer ()
94
94
95
95
# Launch the graph
96
96
with tf .Session () as sess :
0 commit comments