Skip to content

Commit 1a44c34

Browse files
RWaleckiaymericdamien
authored andcommitted
added Python 3 compatibility (aymericdamien#58)
1 parent 60dc7c1 commit 1a44c34

15 files changed

+99
-71
lines changed

examples/1_Introduction/basic_operations.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Project: https://github.com/aymericdamien/TensorFlow-Examples/
66
'''
77

8+
from __future__ import print_function
9+
810
import tensorflow as tf
911

1012
# Basic constant operations
@@ -15,9 +17,9 @@
1517

1618
# Launch the default graph.
1719
with tf.Session() as sess:
18-
print "a=2, b=3"
19-
print "Addition with constants: %i" % sess.run(a+b)
20-
print "Multiplication with constants: %i" % sess.run(a*b)
20+
print("a=2, b=3")
21+
print("Addition with constants: %i" % sess.run(a+b))
22+
print("Multiplication with constants: %i" % sess.run(a*b))
2123

2224
# Basic Operations with variable as graph input
2325
# The value returned by the constructor represents the output
@@ -33,8 +35,8 @@
3335
# Launch the default graph.
3436
with tf.Session() as sess:
3537
# Run every operation with variable input
36-
print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})
37-
print "Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3})
38+
print("Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3}))
39+
print("Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3}))
3840

3941

4042
# ----------------
@@ -69,5 +71,5 @@
6971
# The output of the op is returned in 'result' as a numpy `ndarray` object.
7072
with tf.Session() as sess:
7173
result = sess.run(product)
72-
print result
74+
print(result)
7375
# ==> [[ 12.]]

examples/1_Introduction/helloworld.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Project: https://github.com/aymericdamien/TensorFlow-Examples/
66
'''
77

8+
from __future__ import print_function
9+
810
import tensorflow as tf
911

1012
#Simple hello world using TensorFlow
@@ -20,4 +22,4 @@
2022
sess = tf.Session()
2123

2224
# Run the op
23-
print sess.run(hello)
25+
print(sess.run(hello))

examples/2_BasicModels/linear_regression.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Project: https://github.com/aymericdamien/TensorFlow-Examples/
66
'''
77

8+
from __future__ import print_function
9+
810
import tensorflow as tf
911
import numpy
1012
import matplotlib.pyplot as plt
@@ -53,12 +55,12 @@
5355
#Display logs per epoch step
5456
if (epoch+1) % display_step == 0:
5557
c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
56-
print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
57-
"W=", sess.run(W), "b=", sess.run(b)
58+
print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
59+
"W=", sess.run(W), "b=", sess.run(b))
5860

59-
print "Optimization Finished!"
60-
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
61-
print "Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n'
61+
print("Optimization Finished!"
62+
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y}))
63+
print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
6264

6365
#Graphic display
6466
plt.plot(train_X, train_Y, 'ro', label='Original data')
@@ -70,13 +72,13 @@
7072
test_X = numpy.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1])
7173
test_Y = numpy.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03])
7274

73-
print "Testing... (Mean square loss Comparison)"
75+
print("Testing... (Mean square loss Comparison)")
7476
testing_cost = sess.run(
7577
tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * test_X.shape[0]),
7678
feed_dict={X: test_X, Y: test_Y}) # same function as cost above
77-
print "Testing cost=", testing_cost
78-
print "Absolute mean square loss difference:", abs(
79-
training_cost - testing_cost)
79+
print("Testing cost=", testing_cost)
80+
print("Absolute mean square loss difference:", abs(
81+
training_cost - testing_cost))
8082

8183
plt.plot(test_X, test_Y, 'bo', label='Testing data')
8284
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')

examples/2_BasicModels/logistic_regression.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Project: https://github.com/aymericdamien/TensorFlow-Examples/
88
'''
99

10+
from __future__ import print_function
11+
1012
import tensorflow as tf
1113

1214
# Import MINST data

examples/2_BasicModels/nearest_neighbor.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Project: https://github.com/aymericdamien/TensorFlow-Examples/
88
'''
99

10+
from __future__ import print_function
11+
1012
import numpy as np
1113
import tensorflow as tf
1214

@@ -42,10 +44,10 @@
4244
# Get nearest neighbor
4345
nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})
4446
# Get nearest neighbor class label and compare it to its true label
45-
print "Test", i, "Prediction:", np.argmax(Ytr[nn_index]), \
46-
"True Class:", np.argmax(Yte[i])
47+
print("Test", i, "Prediction:", np.argmax(Ytr[nn_index]), \
48+
"True Class:", np.argmax(Yte[i]))
4749
# Calculate accuracy
4850
if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
4951
accuracy += 1./len(Xte)
50-
print "Done!"
51-
print "Accuracy:", accuracy
52+
print("Done!")
53+
print("Accuracy:", accuracy)

examples/3_NeuralNetworks/bidirectional_rnn.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Project: https://github.com/aymericdamien/TensorFlow-Examples/
88
'''
99

10+
from __future__ import print_function
11+
1012
import tensorflow as tf
1113
from tensorflow.python.ops import rnn, rnn_cell
1214
import numpy as np
@@ -106,15 +108,15 @@ def BiRNN(x, weights, biases):
106108
acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
107109
# Calculate batch loss
108110
loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
109-
print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
111+
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
110112
"{:.6f}".format(loss) + ", Training Accuracy= " + \
111-
"{:.5f}".format(acc)
113+
"{:.5f}".format(acc))
112114
step += 1
113-
print "Optimization Finished!"
115+
print("Optimization Finished!")
114116

115117
# Calculate accuracy for 128 mnist test images
116118
test_len = 128
117119
test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
118120
test_label = mnist.test.labels[:test_len]
119-
print "Testing Accuracy:", \
120-
sess.run(accuracy, feed_dict={x: test_data, y: test_label})
121+
print("Testing Accuracy:", \
122+
sess.run(accuracy, feed_dict={x: test_data, y: test_label}))

examples/3_NeuralNetworks/convolutional_network.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Project: https://github.com/aymericdamien/TensorFlow-Examples/
88
'''
99

10+
from __future__ import print_function
11+
1012
import tensorflow as tf
1113

1214
# Import MINST data
@@ -119,14 +121,14 @@ def conv_net(x, weights, biases, dropout):
119121
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
120122
y: batch_y,
121123
keep_prob: 1.})
122-
print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
124+
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
123125
"{:.6f}".format(loss) + ", Training Accuracy= " + \
124-
"{:.5f}".format(acc)
126+
"{:.5f}".format(acc))
125127
step += 1
126-
print "Optimization Finished!"
128+
print("Optimization Finished!")
127129

128130
# Calculate accuracy for 256 mnist test images
129-
print "Testing Accuracy:", \
131+
print("Testing Accuracy:", \
130132
sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
131133
y: mnist.test.labels[:256],
132-
keep_prob: 1.})
134+
keep_prob: 1.}))

examples/3_NeuralNetworks/dynamic_rnn.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
Project: https://github.com/aymericdamien/TensorFlow-Examples/
1010
'''
1111

12+
from __future__ import print_function
13+
1214
import tensorflow as tf
1315
import random
1416

@@ -179,16 +181,16 @@ def dynamicRNN(x, seqlen, weights, biases):
179181
# Calculate batch loss
180182
loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y,
181183
seqlen: batch_seqlen})
182-
print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
184+
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
183185
"{:.6f}".format(loss) + ", Training Accuracy= " + \
184-
"{:.5f}".format(acc)
186+
"{:.5f}".format(acc))
185187
step += 1
186-
print "Optimization Finished!"
188+
print("Optimization Finished!")
187189

188190
# Calculate accuracy
189191
test_data = testset.data
190192
test_label = testset.labels
191193
test_seqlen = testset.seqlen
192-
print "Testing Accuracy:", \
194+
print("Testing Accuracy:", \
193195
sess.run(accuracy, feed_dict={x: test_data, y: test_label,
194-
seqlen: test_seqlen})
196+
seqlen: test_seqlen}))

examples/3_NeuralNetworks/multilayer_perceptron.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Project: https://github.com/aymericdamien/TensorFlow-Examples/
88
'''
99

10+
from __future__ import print_function
11+
1012
# Import MINST data
1113
from tensorflow.examples.tutorials.mnist import input_data
1214
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
@@ -82,12 +84,12 @@ def multilayer_perceptron(x, weights, biases):
8284
avg_cost += c / total_batch
8385
# Display logs per epoch step
8486
if epoch % display_step == 0:
85-
print "Epoch:", '%04d' % (epoch+1), "cost=", \
86-
"{:.9f}".format(avg_cost)
87-
print "Optimization Finished!"
87+
print("Epoch:", '%04d' % (epoch+1), "cost=", \
88+
"{:.9f}".format(avg_cost))
89+
print("Optimization Finished!")
8890

8991
# Test model
9092
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
9193
# Calculate accuracy
9294
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
93-
print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})
95+
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

examples/3_NeuralNetworks/recurrent_network.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
Project: https://github.com/aymericdamien/TensorFlow-Examples/
88
'''
99

10+
from __future__ import print_function
11+
1012
import tensorflow as tf
1113
from tensorflow.python.ops import rnn, rnn_cell
1214
import numpy as np
@@ -97,15 +99,15 @@ def RNN(x, weights, biases):
9799
acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
98100
# Calculate batch loss
99101
loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
100-
print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
102+
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
101103
"{:.6f}".format(loss) + ", Training Accuracy= " + \
102-
"{:.5f}".format(acc)
104+
"{:.5f}".format(acc))
103105
step += 1
104-
print "Optimization Finished!"
106+
print("Optimization Finished!")
105107

106108
# Calculate accuracy for 128 mnist test images
107109
test_len = 128
108110
test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
109111
test_label = mnist.test.labels[:test_len]
110-
print "Testing Accuracy:", \
111-
sess.run(accuracy, feed_dict={x: test_data, y: test_label})
112+
print("Testing Accuracy:", \
113+
sess.run(accuracy, feed_dict={x: test_data, y: test_label}))

0 commit comments

Comments
 (0)