데이터뢄석가 κ³Όμ •/Tensorflow

DAY61. Tensorflow LinearRegression (1)function basic (κΈ°λ³Έν•¨μˆ˜)

LEE_BOMB 2021. 12. 16. 16:23
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())