<jupyter_start><jupyter_text>Lab 3 Minimizing Cost  Lab03-X-minimizing_cost_tf_gradient 참고만!!<jupyter_code>import tensorflow as tf
tf.set_random_seed(777)  # for reproducibility
# tf Graph Input
X = [1, 2, 3]
Y = [1, 2, 3]
# Set wrong model weights
W = tf.Variable(5.)
# Linear model
hypothesis = X * W
# Manual gradient (SSE를 X에 대해 한 번 미분)
gradient = tf.reduce_mean((W * X - Y) * X) * 2
# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Minimize: Gradient Descent 내장 함수
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
# Get gradients (tf 내장된 gradient descent optimize 함수에서 자동으로 계산된 gradient 값 가져오기)
gvs = optimizer.compute_gradients(cost, [W])
# Optional: modify gradient if necessary (이 방법을 통해 gradient 값을 변화시켜 최적화 할 때 사용할 수 있음)
# gvs = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gvs]
# Apply gradients
apply_gradients = optimizer.apply_gradients(gvs)    # 여기서는 얻은 gradient 값을 변화시키지 않고 그냥 사용
# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
for step in range(100):
    print(step, sess.run([gradient, W, gvs]))
    sess.run(apply_gradients)   # gradient 값을 변화시키지 않았기 때문에 sess.run(train)과 결과 동일
    # sess.run(train)<jupyter_output>0 [37.333332, 5.0, [(37.333336, 5.0)]]
1 [33.848888, 4.6266665, [(33.848888, 4.6266665)]]
2 [30.689657, 4.2881775, [(30.689657, 4.2881775)]]
3 [27.825287, 3.9812808, [(27.825287, 3.9812808)]]
4 [25.228262, 3.703028, [(25.228262, 3.703028)]]
5 [22.873621, 3.4507453, [(22.873623, 3.4507453)]]
6 [20.738752, 3.2220092, [(20.73875, 3.2220092)]]
7 [18.803137, 3.0146217, [(18.803137, 3.0146217)]]
8 [17.048176, 2.8265903, [(17.048176, 2.8265903)]]
9 [15.457013, 2.6561086, [(15.457014, 2.6561086)]]
10 [14.014359, 2.5015385, [(14.01436, 2.5015385)]]
11 [12.706352, 2.3613949, [(12.706352, 2.3613949)]]
12 [11.520427, 2.2343314, [(11.520427, 2.2343314)]]
13 [10.445186, 2.119127, [(10.445185, 2.119127)]]
14 [9.4703016, 2.0146751, [(9.4703016, 2.0146751)]]
15 [8.5864067, 1.9199722, [(8.5864067, 1.9199722)]]
16 [7.7850089, 1.8341081, [(7.7850089, 1.8341081)]]
17 [7.0584083, 1.756258, [(7.0584083, 1.756258)]]
18 [6.3996239, 1.685674, [(6.3996239, 1.685674)]]
19 [5.8023257, 1.6216778, [(5.8023252, 1.621[...]
{'fraction_non_alphanumeric': 0.17601380500431407, 'fraction_numerical': 0.28731665228645387, 'mean_word_length': 4.587951807228916, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '551814', 'license': 'MIT', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1161, 'n_words': 249, 'path': 'Python/tensorflow/DeepLearningZeroToAll/Lab03-X-minimizing_cost_tf_gradient.ipynb', 'repo_name': 'statKim/TIL', 'size': '2318'}