-
tensorflow v1Computer Science/๊ฐ๋ฐ 2024. 4. 23. 14:49๋ฐ์ํ
์ค์น
๊ฐ๋ฐํ๊ฒฝ: Ubuntu 20.04.6 LTS
1. conda create -n tfv1 python=3.7
(3.8๋ถํฐ๋ v1 ์ค์น๊ฐ ์ง์๋์ง ์๋๋ค)
2. python -m pip install --upgrade pip
3. pip3 install tensorflow==1.15.2
(ํด๋น tensorflow๋ cpu์ฉ์)
4. python3
>> import tensorflow as tf
>> print(tf.__version__)
1.15.2
Basic TensorFlow code (v1)
N, D, H = 64, 1000, 1000 # ๋ฐฐ์น, ์ฐจ์, ๋ ธ๋ x = tf.placeholder(tf.float32, shape=(N,D)) y = tf.placeholder(tf.float32, shape=(N,D)) init = tf.contrib.layers.xavier_initializer() # 2-layer perceptron h = tf.layers.dense(inputs=x, units=H, activation=tf.nn.relu, kernel_initializer=init) y_pred = tf.layers.dense(inputs=h, units=D, kernel_initializer=init) # loss ์ ์, computational graph ๋ง์ ํ์ฑ loss=tf.losses.mean_squared_error(y_pred,y) optimizer = tf.train.GradientDescentOptimizer(1e-5) updates = optimizer.minimize(loss)
tensorflow์์ tf.layers ๋ layer ์ข ๋ฅ์ ๋ฐ๋ผ ์๋์ ์ผ๋ก weight ๋ฐ bias๋ฅผ ์์ฑํด์ค๋ค.
tf.layers.dense์ ๊ฒฝ์ฐ dense layer (fc layer + *activation function)์ ๋ํ ํ๋ผ๋ฏธํฐ๋ฅผ ์์ฑํ๋ค.
์ฒซ๋ฒ์งธ ๋ ์ด์ด:
inputs=x: ๋ ์ด์ด์ ์ ๋ ฅ (์ ๋ ฅ ๋ ธ๋ ๊ฐ์: D๊ฐ)
units=H: ์ถ๋ ฅ๋ ธ๋์ ๊ฐ์๊ฐ H๊ฐ
activation=tf.nn.relu: ๋ ธ๋์ ์ถ๋ ฅ์ ReLU ํจ์๋ฅผ ์ ์ฉ
kernel_initializer=init: ๊ฐ์ค์น ์ด๊ธฐํ ๋ฐฉ๋ฒ ์ ์ฉ
๋๋ฒ์งธ ๋ ์ด์ด:
inputs=h ๋ก ์ฒซ๋ฒ์งธ ๋ ์ด์ด์ ์ถ๋ ฅ๋ ธ๋๋งํผ ์ ๋ ฅ์ผ๋ก ๋ฐ๊ณ ,
D๊ฐ์ ์ถ๋ ฅ ๋ ธ๋๋ฅผ ๊ฐ์ง๋ฉฐ, ๋ณ๋์ ํ์ฑํ ํจ์๋ ์ ์ฉํ์ง ์์
TensorFlow v1์ Pytorch๋ TensorFlow v2์ฒ๋ผ eager execution์ ์ง์ํ์ง ์๊ธฐ๋๋ฌธ์ ์ฌ๊ธฐ๊น์ง๋ Computational graph๋ฅผ ํ์ฑํ๊ธฐ ์ํ Construction phase ์๋ค.
with tf.Session() as sess: sess.run(tf.global_variables_initializer()) values = {x: np.random.randn(N, D), y: np.random.randn(N, D),} losses = [] for t in range(50): loss_val, _ = sess.run([loss, updates], feed_dict=values) print(loss_val)
์ด์ tf.Session ์ผ๋ก ๊ฐ์ธ์ sess.run์ ํด์ฃผ๋ฉด ์์์ ์์ฑํ computational graph์ ์ฐ์ฐ์ด ์คํ์ด ๋๋ค.
์คํํ ์ฐ์ฐ์ ์ธ์๋ก ๋๊ฒจ์ค์ผ ํ๋๋ฐ loss์ updates๋ฅผ ๋ชจ๋ ์ ๊ณตํจ์ผ๋ก์จ
forward pass์ backward pass ์ฐ์ฐ ๋ชจ๋ ์ํํ๊ณ ์๋ค.
'Computer Science > ๊ฐ๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Docker ์ค์น๋ถํฐ ์ด๋ฏธ์ง ๋น๋, ์คํ๊น์ง ๐ (0) 2024.06.06 ModuleNotFoundError: No module named 'mmcv._ext' (0) 2024.05.25 ํ์ด์ฌ ๋ฉ์๋ ์ด๋ฆ ์์ ์ธ๋์ค์ฝ์ด('_') ์๋ฏธ (0) 2024.03.11 pip install ๋ช ๋ ์ด ์ต์ ์ ๋ฆฌ (0) 2024.03.07