DAY61. Tensorflow LinearRegression (1)function basic (κΈ°λ³Έν¨μ)
Tensorflow κΈ°λ³Έν¨μ
μμ μμ± ν¨μ
tf.constant(value, dtype, shape) : μ§μ ν κ°(value)μΌλ‘ μμ μμ±
tf.zeros(shape, dtype) : λͺ¨μκ³Ό νμ μΌλ‘ λͺ¨λ μμκ° 0μΌλ‘ μμ±
tf.ones(shape, dtype) : λͺ¨μκ³Ό νμ μΌλ‘ λͺ¨λ μμκ° 1λ‘ μμ±
tf.identity(input) : λ΄μ©κ³Ό λͺ¨μμ΄ λμΌν μμ μμ±
tf.fill(shape, value) : μ£Όμ΄μ§ scalarκ°μΌλ‘ μ΄κΈ°νλ μμ μμ±
tf.linspace(start, stop, num) : start~stop λ²μμμ numμ λ§νΌ μμ±
tf.range(start, limit, delta) : μμ, μ’ λ£, μ¦κ° μΌλ‘ μμ μμ±
μνκ΄λ ¨ ν¨μ : tf.math.ν¨μ()
tf.math.add() : λ§μ ν¨μ
tf.math.subtract() : λΊμ ν¨μ
tf.math.multiply() : κ³±μ ν¨μ
tf.math.divide() : λλμ (λͺ« κ³μ°) ν¨μ
tf.math.mod() : λλμ (λλ¨Έμ§ κ³μ°) ν¨μ
tf.math.abs() : μ λκ° λ³κ²½ ν¨μ
tf.math.square() : μ κ³± κ³μ° ν¨μ
tf.math.sqrt() : μ κ³±κ·Ό κ³μ° ν¨μ
tf.math.round() : λ°μ¬λ¦Ό ν¨μ
tf.math.pow() : κ±°λμ κ³± ν¨μ
tf.math.exp() : μ§μ ν¨μ
tf.math.log() : λ‘κ·Έ ν¨μ
μ°¨μμΆμ ν΅κ³ν¨μ : tf.reduce_xxx()
tf.reduce_sum() : μ§μ ν μ°¨μμ λμμΌλ‘ μμλ€μ ν©κ³
tf.reduce_mean() : μ§μ ν μ°¨μμ λμμΌλ‘ μμλ€μ νκ·
tf.reduce_prod() : μ§μ ν μ°¨μμ λμμΌλ‘ μμλ€μ κ³±μ
tf.reduce_min() : μ§μ ν μ°¨μμ λμμΌλ‘ μ΅μκ° κ³μ°
tf.reduce_max() : μ§μ ν μ°¨μμ λμμΌλ‘ μ΅λκ° κ³μ°
tf.reduce_all() : μμκ° μ λΆ True -> True λ°ν
tf.reduce_any() : μμκ° νλλΌλ True -> True λ°ν
λμ κ΄λ ¨ ν¨μ : tf.random.ν¨μ()
tf.random.normal(shape, mean, stddev) : νκ· ,νμ€νΈμ°¨ μ κ·λΆν¬ μμ±
tf.truncated.normal(shape, mean, stddev) : νμ€νΈμ°¨μ 2λ°° μλ³΄λ€ ν° κ° μ μ κ±°νμ¬ μ κ·λΆν¬ μμ±
tf.random.uniform(shape, minval, maxval) : κ· λ±λΆν¬ λμ μμ±
tf.random.shuffle(value) : 첫 λ²μ§Έ μ°¨μ κΈ°μ€μΌλ‘ ν μμ μμ μκΈ°
tf.random.set_seed(seed) : λμ seedκ° μ€μ
1. μμ μμ±ν¨μ
import tensorflow as tf # ver2.x
a = tf.constant(10, tf.int32, (2, 3))
print(a)
b = tf.zeros( (2, 3) )
print(b) #sess.run()
c = tf.ones( (2, 3) )
print(c)
d = tf.identity(c)
print(d)
e = tf.fill((2, 3), 5) #(shape, value)
print(e)
f = tf.linspace(15.2, 22.3, 5) #(start, stop, num)
print(f)
g = tf.range(10, 1.5, -2.5) #(start, stop, step)
print(g)
2. μ°¨μμΆμ ν΅κ³/μν ν¨μ
import tensorflow as tf # er2.x
data = [[1.5, 1.5], [2.5, 2.5], [3.5, 3.5]] #μ€μ²© list
data
print(tf.reduce_sum(data, axis=0)) #νμΆ ν©κ³:μ΄ λ¨μ ν©κ³
#[7.5 7.5]
print(tf.reduce_sum(data, axis=1)) #μ΄μΆ ν©κ³:ν λ¨μ ν©κ³
#[3. 5. 7.]
#μ 체 data μ°μ°
print(tf.reduce_mean(data)) #μ 체 data = 2.5
print(tf.reduce_max(data)) #3.5
print(tf.reduce_min(data)) #1.5
bool_data = [[True, True], [False, False]]
print(tf.reduce_all(bool_data)) #False
print(tf.reduce_any(bool_data)) #True
csv file read
κ°μνκ²½μ pandas μ€μΉ
(base) > conda activate tensorflow
(tensorflow) > conda install pandas
import pandas as pd
iris = pd.read_csv(r'C:\ITWILL\5_Tensorflow\data\iris.csv')
iris.info()
cols = list(iris.columns)
cols
x_data = iris[cols[:-1]]
x_data.shape #(150, 4) - data = pandas κ°μ²΄
tf.reduce_mean(x_data, axis=0) #μ΄λ¨μ νκ·
#[5.84333333, 3.05733333, 3.758 , 1.19933333] - 1d
row_sum = tf.reduce_mean(x_data, axis=1) #νλ¨μ νκ·
print(row_sum.numpy())
print(len(row_sum.numpy())) #150
3. λμ μμ± ν¨μ
κ°μνκ²½μ matplotlib μ€μΉ
(base) > conda activate tensorflow
(tensorflow) > conda install matplotlib
import tensorflow as tf #ver2.x
import matplotlib.pyplot as plt #chart
2ν3μ΄ κ΅¬μ‘°μ νμ€μ κ·λΆν¬λ₯Ό λ°λ₯΄λ λμ μμ±
norm = tf.random.normal([2,3], mean=0, stddev=1) #default : 0, 1
print(norm) #κ°μ²΄ 보기
uniform = tf.random.uniform([2,3], minval=0, maxval=1) #default : 0, 1
print(uniform) #κ°μ²΄ 보기
matrix = [[1,2], [3,4], [5,6]] #(3, 2)
shuff = tf.random.shuffle(matrix)
print(shuff) #첫λ²μ§Έ μ°¨μ(3ν λ¨μλ‘ μμ)
seedκ° μ§μ : λμΌν λμ μμ±
tf.random.set_seed(1234)
a = tf.random.uniform([1])
b = tf.random.normal([1])
print('a=',a.numpy())
print('b=',b.numpy())