In [1]:
%matplotlib inline
import os
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
In [2]:
local_dir = os.path.join("/DATA",os.environ.get("USER"),"MNIST_data")
os.makedirs(local_dir,mode=0o755, exist_ok=True)
Dostęp do danych MNIST jest ułatwniony w tensorflow poprzez moduł tensorflow.examples.tutorials.mnist
.
Można korzystać w sposób bezpośredni, albo przez next_batch
, przykłady:
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(local_dir, one_hot=True)
In [4]:
mnist.train.images.shape,mnist.test.images.shape,mnist.test.labels.shape
Out[4]:
In [5]:
plt.matshow(mnist.train.images[1,:].reshape(28,28),cmap='gray')
Out[5]:
In [6]:
mnist.train.labels[1]
Out[6]:
In [7]:
mnist.train.next_batch(100)[1].shape
Out[7]:
In [8]:
mnist.train.images[123:124,:].shape
Out[8]:
In [9]:
mnist.train._num_examples
Out[9]:
In [10]:
mnist.train.next_batch
Out[10]:
In [11]:
x = tf.placeholder(tf.float32, [None, 784])
#W = tf.Variable(tf.zeros([784, 10]))
W = tf.Variable( tf.truncated_normal([784, 10], stddev=0.1,dtype=tf.float32) )
b = tf.Variable(tf.zeros([10]))
Sieć jest definiowana jedną linią w tensorflow:
In [12]:
y = tf.nn.softmax(tf.matmul(x, W) + b)
#y = tf.nn.sigmoid(tf.matmul(x, W) + b)
#y = tf.nn.relu(tf.matmul(x, W) + b)
#y = tf.matmul(x, W) + b
Niech y_
będzie zawieram poprawą odpowiedź (label), jako funkcję kosztu możemy zastosować cross entropię lub zwykła normę.
In [13]:
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
norm = tf.norm(y_ - y)
In [14]:
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)
In [15]:
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
In [16]:
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
In [ ]:
In [17]:
config = tf.ConfigProto(log_device_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.05
sess = tf.InteractiveSession(config=config)
tf.global_variables_initializer().run()
Przetestujmy przepuszczenie jednego obrazka przez sieć i obliczenie np. normy:
Uwaga - na wejsciu musi być tensor z dwoma indeskami, więc jeśli chcemy przetworzyc dokładnie jeden obrazek to zamiast shate (784,) trzeba dać (1,784). Sprawdź różnicę między:
mnist.train.images[123:124,:].shape
mnist.train.images[123,:].shape
In [18]:
sess.run(y,feed_dict={x: mnist.train.images[123:124,:], y_: mnist.train.labels[123:124,:]})
Out[18]:
In [19]:
sess.run(norm,feed_dict={x: mnist.train.images[123:124,:], y_: mnist.train.labels[123:124,:]})
Out[19]:
In [20]:
%%time
hst = []
hst2 = []
tf.global_variables_initializer().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
hst2.append( sess.run(cross_entropy, feed_dict={x: batch_xs, y_: batch_ys}))
hst.append( sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys}))
In [21]:
plt.plot(hst)
plt.plot(hst2,'r')
plt.axhline(1.0,color='black', linestyle='--')
Out[21]:
In [22]:
print(sess.run(accuracy, feed_dict={x: mnist.train.images, y_: mnist.train.labels}))
In [23]:
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
In [ ]:
In [25]:
W_local,b_local = W.eval(),b.eval()
In [26]:
ith = 21
x_in = mnist.test.images[ith]
print(np.argmax(np.dot(x_in,W_local)+b_local))
print(np.argmax(mnist.test.labels[ith]))
Zobaczmy ten obrazek:
In [27]:
plt.matshow(x_in.reshape(28,28),cmap='gray')
Out[27]:
In [ ]:
In [28]:
errors = []
for ith in range(mnist.test.num_examples):
x_in = mnist.test.images[ith]
prediction = np.argmax(np.dot(x_in,W_local)+b_local)
label = np.argmax(mnist.test.labels[ith])
if prediction!=label:
#print(ith,prediction,label)
errors.append([prediction,label,x_in.reshape(28,28)])
print( len(errors))
In [29]:
from time import sleep
In [30]:
#Imports for visualization
import PIL.Image
from io import BytesIO
from IPython.display import clear_output, Image, display
def DisplayArray(a, fmt='png', rng=[0,1]):
"""Display an array as a picture."""
a = (a - rng[0])/float(rng[1] - rng[0])*255
a = np.uint8(np.clip(a, 0, 255))
f = BytesIO()
PIL.Image.fromarray(a).save(f, fmt)
display(Image(data=f.getvalue()))
In [31]:
for er in errors[:12]:
print("zamiast ",er[1], "sieć odczytała",er[0],end="")
DisplayArray(er[2])
sleep(0.25)
clear_output(wait=True)
In [ ]:
In [ ]:
In [32]:
def softmax(x):
return np.exp(x)/np.sum(np.exp(x))
In [33]:
p = np.array([0.1,1,1,3,5,6])
print(p)
print(softmax(p),np.sum(softmax(p)))
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: