20170828

5621 단어

20170828


Re all in ml now.
Re learn re new.

1.tensorflow input mnist data

# Import MNIST
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

# Load data
X_train = mnist.train.images
Y_train = mnist.train.labels
X_test = mnist.test.images
Y_test = mnist.test.labels

# Get the next 64 images array and labels
batch_X, batch_Y = mnist.train.next_batch(64)

2.tensorflow hello world

import tensorflow as tf

hello = tf.constant('hello tensorflow!')
sess = tf.Session()
print(sess.run(hello))

3.tensorflow basic operations

import tensorflow as tf

a = tf.constant(2)
b = tf.constant(3)
with tf.Session() as sess:
    print(sess.run(a))
    print(sess.run(b))
    print(sess.run(a + b))
    print(sess.run(a * b))

a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
add = tf.add(a,b)
mul = tf.multiply(a,b)
with tf.Session() as sess:
    print(sess.run(add,feed_dict={a:2,b:3}))
    print(sess.run(mul,feed_dict={a:2,b:3}))

matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],[2]])
product = tf.matmul(matrix1,matrix2)
with tf.Session() as sess:
    result = sess.run(product)
    print(result)

4.nearest neighbor

import numpy as np
import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist/",one_hot=True)

# take how much datas
Xtr, Ytr = mnist.train.next_batch(5000)
Xte, Yte = mnist.test.next_batch(200)

xtr = tf.placeholder("float", [None, 784])
xte = tf.placeholder("float", [784])

# calcute the min distance
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices = 1)
pred = tf.arg_min(distance, 0)
accuracy = 0
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for i in range(len(Xte)):
        nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})
        print("Test ", i, "Prediction: ", np.argmax(Ytr[nn_index]), "True Class: ", np.argmax(Yte[i]))
        if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
            accuracy += 1 / len(Xte)
    print("Done!")
    print("Accuracy: ",accuracy)

next_batch: 데이터를 얼마나 찾습니까
tf.negative: 마이너스 값 구하기
tf.dd:더하기
tf.abs: 절대치 구하기
tf.reduce_sum:구화
reduction_indices = 1: 첫 번째 요소에 대한 작업
tf.arg_min: 최소값 구하기
np.argmax: 실제 label 값을 예측합니다

5.tensorflow linear regression

import tensorflow as tf
import numpy
import matplotlib.pyplot as plt

rng = numpy.random

learning_rate = 0.01
training_epochs = 1000
display_step = 50

train_X = numpy.asarray([3.3,
                         4.4,
                         5.5,
                         6.71,
                         6.93,
                         4.168,
                         9.779,
                         6.182,
                         7.59,
                         2.167,
                         7.042,
                         10.791,
                         5.313,
                         7.997,
                         5.654,
                         9.27,
                         3.1])
train_Y = numpy.asarray([1.7,
                         2.76,
                         2.09,
                         3.19,
                         1.694,
                         1.573,
                         3.366,
                         2.596,
                         2.53,
                         1.221,
                         2.827,
                         3.465,
                         1.65,
                         2.904,
                         2.42,
                         2.94,
                         1.3])
n_samples = train_X.shape[0]

X = tf.placeholder("float")
Y = tf.placeholder("float")

W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

pred = tf.add(tf.multiply(X, W), b) # x * w + b

cost = tf.reduce_sum(tf.pow(pred - Y, 2)) # (pred - Y)^2
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})
        if (epoch + 1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
            print("Epoch:",'%04d' % (epoch + 1), "cost=", "{:.9f}".format(c), "W=", sess.run(W), "b=", sess.run(b))

    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '
') plt.plot(train_X, train_Y, 'ro', label='Original data') plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line') plt.legend() plt.show()

epoch:시점
numpy.asarray: 입력한 데이터를 행렬로 변환
numpy.shape[0]: 읽기 행렬의 1차원 길이
numpy.randn: 정적 분포 랜덤 수 생성
tf.train.GradientDescentOptimizer(learning_rate).미니미즈(cost): 요구하는 학습 효율에 따라 응용 계단이 낮아진다
사다리꼴 하락: 사다리꼴 하락을 사용하여 함수의 국부 극소값을 찾으면 함수에 있는 현재 점에 대응하는 사다리꼴(또는 근사한 사다리꼴)의 반대 방향의 규정된 보장 거리점을 반복해서 검색해야 한다.
zip: 일련의 교체 가능한 대상을 매개 변수로 받아들여 대상에 대응하는 요소를 하나의tuple (모듈) 로 포장합니다.

좋은 웹페이지 즐겨찾기