<jupyter_start><jupyter_text>lecture 2<jupyter_code>vec, mat = tf.constant([2,2], name="vector"), tf.constant([[0,1],[3,4]], name="matrix")
tf.zeros(vec, tf.int32)
with tf.Session() as s:
    print(tf.zeros(vec, tf.int32).eval())
    print(vec, vec.eval())
    print(mat, mat.eval())
with tf.Session() as s:
    print(tf.zeros_like(vec), tf.zeros_like(vec).eval())
tf.InteractiveSession()
tf.ones([5,2]), tf.ones([5,2]).eval()
tf.fill([2,5],7).eval(), tf.fill([2,5],7)
tf.lin_space(1,9,0.8).eval()
tf.lin_space(1.0,9.0,0.8).eval()
print(tf.lin_space(1.0,9.0,8).eval())
len(tf.lin_space(1.0,9.0,8).eval())
print(tf.range(0., 11, 2).eval())
print(tf.range(0., 12., 2).eval())
for m in range(9,3,-1):
    print(tf.range(0., 12 + 10**(-m), 2).eval(), 12+10**(-m))
print(mat.eval())
tf.random_shuffle(mat).eval()
mat = tf.random_normal([3,2,2])
mat = tf.constant(mat.eval())
print(mat.eval())
# tf.random_shuffle은 첫 번째 디멘션으로만 섞는다.
import numpy as np
#mat = tf.constant([[[5,2],[2,1]],[[1,1],[3,2]]])
print(mat.eval())
temp = [tf.random_shuffle(mat).eval()]
for _ in range(50):
    sfd = tf.random_shuffle(mat).eval()
    if not (sum(1 for e in temp if np.all(sfd==e))):
        temp.append(sfd)
len(temp), temp
# truncated는 2 std 밖의 값은 뽑지 않는다.
max_truncated, max_normal = -9, -9
for _ in range(100):
    max_truncated = max(np.max(tf.truncated_normal([1,10]).eval()), max_truncated)
    max_normal = max(np.max(tf.random_normal([1,10]).eval()), max_normal)
max_truncated, max_normal
print(mat.eval(),[3,2,2])
print(tf.random_crop(mat,tf.constant([2,1,2])).eval(),'2,1,2')
print(tf.random_crop(mat,tf.constant([3,1,1])).eval(),'3,1,1')
print(tf.random_crop(mat,tf.constant([2,2,1])).eval(),'2,2,1')
with tf.Session() as s:
    print(s.run(tf.report_uninitialized_variables()))
tf.get_variable?
W = tf.Variable(10)
with tf.Session() as sess:
    sess.run(W.initializer)
    print(W.eval())
    W.assign(100).eval()
    print(W.eval())
    sess.run(W.assign(1001))
    print(W.eval())
W = tf.Variable(123)
W.assign(100).eval()
W.eval()
tf.Variable.initialized_value?<jupyter_output><empty_output><jupyter_text>lecture 3<jupyter_code>import tensorflow as tf


import utils


DATA_FILE = "data/birth_life_2010.txt"


# Step 1: read in data from the .txt file

# data is a numpy array of shape (190, 2), each row is a datapoint

data, n_samples = utils.read_birth_life_data(DATA_FILE)


# Step 2: create placeholders for X (birth rate) and Y (life expectancy)

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 (life expectancy from birth rate)
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.01 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]) 
    print('loss', loss, w_out, b_out)
def huber_loss(labels,predictions, delta=14.0):
    residual = tf.abs(labels - predictions)
    def f1(): return 0.5 * tf.square(residual)
    def f2(): return delta * residual - 0.5 * tf.square(delta)
    return tf.cond(residual < delta, f1, f2)

import tensorflow as tf


import utils


DATA_FILE = "data/birth_life_2010.txt"


# Step 1: read in data from the .txt file

# data is a numpy array of shape (190, 2), each row is a datapoint

data, n_samples = utils.read_birth_life_data(DATA_FILE)


# Step 2: create placeholders for X (birth rate) and Y (life expectancy)

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 (life expectancy from birth rate)
Y_predicted = w * X + b 


# Step 5: use the square error as the loss function

#loss = tf.square(Y - Y_predicted, name='loss')
loss = huber_loss(Y, Y_predicted)


# Step 6: using gradient descent with learning rate of 0.01 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]) 
    print('loss', loss, w_out, b_out)
data, n_samples = utils.read_birth_life_data('data/birth_life_2010.txt')

#dataset 이용
dataset = tf.data.Dataset.from_tensor_slices((data[:,0], data[:,1]))
print(dataset.output_types, dataset.output_shapes)
#...
iterator = dataset.make_one_shot_iterator()
X, Y = iterator.get_next()
with tf.Session() as sess:
    for i in range(5):
        print(sess.run([X, Y]))
# gradient 직접 수정하기
'''
optimizer.compute_gradients(loss, list of variables)
-> [(gradient, variable), ... ]  반환. 여기에 수정 가한 뒤
optimizer.apply_gradients(적용할 변수)

'''

# gradient 멈추기
'''
tf.stop_gradient 사용.
'''

# 특정 gradient만 계산하기
'''
tf.gradients 사용
'''
mnist_folder = 'data/mnist'
utils.download_mnist(mnist_folder)
train, val, test = utils.read_mnist(mnist_folder, flatten = True)

train_data = tf.data.Dataset.from_tensor_slices(train)
test_data = tf.data.Dataset.from_tensor_slices(test)

batch_size = 64

train_data = train_data.batch(batch_size)
test_data = test_data.batch(batch_size)

iterator = tf.data.Iterator.from_structure(train_data.output_types, train_data.output_shapes)

img, label = iterator.get_next()

train_init = iterator.make_initializer(train_data)
test_init = iterator.make_initializer(test_data)

with tf.Session() as sess:
    #tf.nn.conv2d()<jupyter_output><empty_output><jupyter_text>lecture 4<jupyter_code>import tensorflow as tf
tf.enable_eager_execution()

print("TensorFlow version: {}".format(tf.VERSION))
print("Eager execution: {}".format(tf.executing_eagerly()))
x = [[2.]]
m = tf.matmul(x, x)
print(m)
import numpy as np 

x = tf.constant([1., 2., 3.])
assert type(x.numpy()) == np.ndarray
squared = np.square(x)

squared = tf.square(squared)

for i in x:
    print(i)
    
print(x.shape)
for i in range(x.shape[0]):
    for j in range(x.shape[1]):
        print(x[i,j])
def square(x):
    return x ** 2

import tensorflow.contrib.eager as tfe
grad = tfe.gradients_function(square)

print(square(3.))
print(grad(3.))
x = tf.Variable(2.)
def loss(y):
    return (y - x ** 2) ** 2

grad = tfe.implicit_gradients(loss)

print(loss(7.))
print(grad(7.))
i = tf.Variable([[1.],[2.]])
w1 = tf.Variable([1.,1.])
w2 = tf.multiply(w1,i)
grad = tfe.implicit_gradients(w2)
grad(1.)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1609072715143429, 'fraction_numerical': 0.03308872581721147, 'mean_word_length': 4.256661991584853, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '552570', 'license': 'Apache-2.0', 'n_tokens_mistral': 3077, 'n_tokens_neox': 2910, 'n_words': 674, 'path': '01.tf.overview/lecture 2, 3, 4 practice note.ipynb', 'repo_name': 'pseudoUsername/modu-tensorflow-v2', 'size': '7495'}