In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
In [2]:
%matplotlib inline
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
In [4]:
mnist = input_data.read_data_sets('/Users/jaegyuhan/PythonEx_1/mnist_data', one_hot=True)
In [5]:
X_test = mnist.test.images
Y_test = mnist.test.labels
In [6]:
sample = X_test[9]
In [7]:
sample = sample.reshape(28,28)
In [8]:
for i in range(sample.shape[0]):
for j in range(sample.shape[1]):
print("{:4.0f}".format(sample[i,j]*255), end='')
print()
In [9]:
plt.imshow(sample)
Out[9]:
In [14]:
sample.shape
Out[14]:
In [ ]:
test_xdata = X_test[0:1,]
test_ydata = Y_test[0]
In [ ]:
ll = test_xdata.reshape(784,)
In [10]:
im = Image.open("../넘파이 연습/temp.png")
In [11]:
im
Out[11]:
In [12]:
img2 = im.resize((28,28))
In [27]:
pixels = img2.resize((28, 28)).tobytes("raw", "A")
In [29]:
pixels_list = [i for i in pixels]
In [33]:
plt.imshow(np.array(pixels_list).reshape(28,28))
Out[33]:
In [13]:
plt.imshow(img2)
Out[13]:
In [18]:
b = img2.tobytes("raw","RGBA")
In [26]:
b
Out[26]:
In [19]:
b = np.array([i for i in b])
In [20]:
np.sum(b)
Out[20]:
In [ ]:
In [21]:
#b의 값을 784,3 로 reshape를 한다.
b = b.reshape(784,4)
In [22]:
np.sum(b,axis=1)
Out[22]:
In [23]:
b = np.array([i for i in b])
In [25]:
b
Out[25]:
In [24]:
b = b.reshape(28,28)
In [ ]:
for i in range(b.shape[0]):
for j in range(b.shape[1]):
print("{:4}".format(b[i,j]), end='')
print()
In [ ]:
ll = [i/255 for i in b ]
In [ ]:
len(ll)
In [ ]:
ll = np.array(ll)
In [ ]:
ll.shape
In [ ]:
ll = ll.reshape(1,784)
In [ ]:
ll.shape
In [ ]:
test_xdata
In [ ]:
print(len(test_xdata[0]))
print(test_ydata)
print(test_xdata.shape)
In [ ]:
X = tf.placeholder(dtype=tf.float32, shape=[None, 784])
Y = tf.placeholder(dtype=tf.float32, shape=[None, 10])
In [ ]:
W1 = tf.Variable(tf.random_normal(shape=[784, 256], stddev=0.01))
B1 = tf.Variable(tf.random_normal(shape=[256], stddev=0.01))
L1 = tf.nn.relu(tf.add(tf.matmul(X, W1), B1))
In [ ]:
W2 = tf.Variable(tf.random_normal(shape=[256, 256], stddev=0.01))
B2 = tf.Variable(tf.random_normal(shape=[256], stddev=0.01))
L2 = tf.nn.relu(tf.add(tf.matmul(L1, W2),B2))
In [ ]:
W3 = tf.Variable(tf.random_normal([256, 10], stddev=0.01))
B3 = tf.Variable(tf.random_normal(shape=[10], stddev=0.01))
model = tf.add(tf.matmul(L2, W3),B3)
In [ ]:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=Y))
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
In [ ]:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
batch_size = 100
total_batch = int(mnist.train.num_examples / batch_size)
for epoch in range(15):
total_cost = 0
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
_, cost_val = sess.run([optimizer, cost], feed_dict = {X: batch_xs, Y: batch_ys})
total_cost += cost_val
print("Epoch:","%04d" % (epoch + 1), "Avg. cost =", "{:.3f}".format(total_cost / total_batch))
print('최적화 완료!')
is_correct = tf.equal(tf.argmax(model, 1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("정확도:", sess.run(accuracy, feed_dict={X:mnist.test.images, Y:mnist.test.labels}))
# predict = sess.run([model], feed_dict={X:ll})
# print(predict)
# predict = np.array(predict)
# print("shape:", predict.shape)
# print("result : ",np.argmax(predict[0], axis=1))
In [ ]:
int(mnist.train.num_examples)
In [ ]:
arr = [-12.51021767, -5.7246809 , -0.31184155, -1.74826264,
-17.8037262 , -9.16267967, -26.60844612, 15.07253742,
-11.10308647, -5.45234251]
In [ ]:
arr = np.array(arr)
In [ ]:
arr
In [ ]:
arr = arr.reshape([1,10])
In [ ]:
print(np.argmax(arr, axis=1))
In [ ]:
arr.shape
In [ ]: