1. 그래프 안에서 데이터를 나타내는 것은 Tensor이고 그리고 그래프에서 수행하면서 상태를 보존하면서 가지고 있는 것을 나타내는 것이 변수 Variable에 해당합니다.
  2. 마지막으로 계산을 나타내는 것이 Operation입니다. Operation은 추상적인 계산을 나타내는 인터페이스이고, 실제로 해당하는 Operation을 구체적으로 구현한 것을 Kernel이라고 부릅니다.
  3. Kernel은 특정 디바이스 타입에서 수행될 수 있는 구체적인 구현체라고 보시면 되겠습니다.

 

### 모델 정의

Define a graph : h = ReLU(Wx + b)

import tensorflow as tf

b = tf.get_variable('bias', tf.zeros((100,)))
W = tf.get_variable('weights', tf.random_uniform((784, 100), -1, 1))

x = tf.placeholder(tf.float32, (None, 784))

h = tf.nn.relu(tf.matmul(x, W) + b)

코드는 적엇는데 아직 구동방법을 모름

 

### 구동 심볼릭 모드

import tensorflow as tf
import numpy as np

b = tf.get_variable('bias', tf.zeros((100,)))
W = tf.get_variable('weights', tf.random_uniform((784, 100), -1, 1))

x = tf.placeholder(tf.float32, (None, 784))

h = tf.nn.relu(tf.matmul(x, W) + b)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(h, {x: np.random.random(64, 784)})

@@ Linear Regression

import tensorflow as tf
import utils
DATA_FILE = "data/system_cpuutil_applatency.txt"

# Step 1:readin data from the .txt file
# data is a numpy array of shape(100000, 2 ) , each eow is a datapoint
data, n_samples = utils.read_system

# Step 2: create placeholders for X (CPU util) and Y (app latency)
X = tf.placeholder(tf.float32, name='X')
Y = tf.placeholder(tf.float32, name='Y')

# Step 3: create weight and bias, initialized to 0
w = tf.get_variable('weights',initializer=tf.constant(0.0))
b = tf.get_variable('bias',initializer=tf.constant(0.0))

# Step 4: construct model to predict Y (app latency from CPU util)
Y_predicted = w * X + b

# Step 5: use the square error as the loss function
loss = tf.square(Y - Y_predicted, name='loss')

# Step 6: using gradient descent with learning rate of 0.001 to minimize loss
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)

## 수행하는곳
with tf.Session() as sess:
	# Step 7 : initialize the necessary variables, in this case, w and b 
    sess.run(tf.global_variables_initializer()
    
    # Step 8 : train the model
    for i in range(100): #run 100 epochs
    	for x, y in data:
        	# Session runs train_op to minimize loss 
            sess.run(optimizer, feed_dict={X:x,Y:y})
            
    # Step 9 : output the values of w and b
    w_out, b_out = sess.run([w.b])

 

### 구동 이거 모드 eager mode

import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()

x = [[3.]]
m = tf.matmul(x, x)
print(m)
# tf.Tensor([[9.]], -(1, 1), dtype=float32)

 

### 사이즈가 고정된경우 좀더 빨리 할 수 있다

dataset = tf.data.FixedLengthRecordDataset([file1, file2, file3, ...])

iterator = dataset.make_one_shot_iterator()
input, label = iterator.get_next()
...
for i in range(100):
	...
    try:
    	while True:
        	sess.run([optimizer])
    except tf.errors.OutOfRangeError:
    	pass
        
dataset = dataset.shuffle(1000)
dataset = dataset.repeat(100)
dataset = dataset.batch(128)


dataset = dataset.map(lambda x:tf.one_hot(x, 10))
블로그 이미지

Or71nH

,