njs03332 / ml_study

3 stars 0 forks source link

2023/03/24 ~ 2023/03/30 #63

Open njs03332 opened 1 year ago

njs03332 commented 1 year ago
njs03332 commented 1 year ago

assign roles -s 0323 -c 13.1 13.1.1 13.1.2

njs03332 commented 1 year ago
0 1 2
member 주선미 김유리 한단비
chapter 13.1 13.1.1 13.1.2
njs03332 commented 1 year ago

13.1.1 연쇄 변환

# map() : 각 아이템에 변환 적용
dataset = dataset.map(lambda x: x * 2)

# apply() : 데이터셋 전체에 변환 적용
dataset = dataset.apply(tf.data.experimental.unbatch())

# filter() : 데이터셋 필터링
dataset = dataset.filter(lambda x: x < 10)

# take() : 데이터셋에 있는 몇 개의 아이템만 보기
from item in dataset.take(3):
    print(item)
danbi5228 commented 1 year ago

13.1.2 데이터 셔플링

p.508

dataset = tf.data.Dataset.range(10).repeat(3) # 0 ~ 9 까지 세 번 반복 dataset = dataset.shuffle(buffer_size=5, seed=42).batch(7)

for문 반복시 tf.Tensor([0 2 3 6 7 9 4], shape(7,), dtype=int64) 와 같은 형태로 반환

repeat 메서드 호출 시 반복마다 새로운 순서를 생성하지 못하도록 설정; reshuffle_each_iteration=False

- 메모리 용량보다 큰 대규모 데이터셋은 버퍼가 데이터셋에 비해 작으므로
간단한 셔플링 외에 원본 데이터 자체를 섞어주면 셔플링 효과가 크게 향상됨 (ex. 리눅스 shuf 명령어를 통한 텍스트 섞기)

#### 여러 파일에서 한 줄씩 번갈아 읽기
```python

## 1. 파일 경로가 담긴 리스트 정의

## train_filepaths = 'filepath/my_train_*.csv' # 파일 패턴으로도 가능
train_filepaths = ['filepath/my_train_00.csv', 'filepath/my_train_01.csv', ...] # len = 5

## 2. 파일 경로 섞기 - list_files 함수는 파일 경로를 섞은 데이터셋 반환. 섞지 않길 원한다면 shuffle=False 지정

filepath_dataset=tf.data.Dataset.list_files(train_filepaths, seed=42)

## 3. 파일 번갈아 읽기 - 각 파일의 헤더 (첫번째 행)는 제외하기 위히 skip 메서드 사용
## interleave 데이터셋을 반복 구문에 사용해서 TextLineDataset 을 순회. 아이템이 소진될때까지 한번에 한 줄씩 읽음
## interleave 가 잘 동작하려면 파일 길이가 동일한 것이 좋음

n_readers = 5 # 파일 수
dataset = filepath_dataset.interleave(
lambda filepath: tf.data.TextLineDataset(filepath).skip(1), cycle_length=n_readers)

## interleave 병렬화를 사용하려면 num_parallel_calls 로 스레드 개수 지정
## 이 개수를 tf.data.experimental.AUTOTUNE으로 지정시 텐서플로가 가용한 CPU 기반으로 동적으로 적절한 스레드 수 지정
givitallugot commented 1 year ago

13. 텐서플로에서 데이터 적재와 전처리하기

13.1 데이터 API

X = tf.range(10)
dataset = tf.data.Dataset.from_tensor_slices(X)
dataset
# <TensorSliceDataset shapes: (), types: tf.int32>
dataset = tf.data.Dataset.range(10)

for item in dataset:
    print(item)

# tf.Tensor(0, shape=(), dtype=int64)
# tf.Tensor(1, shape=(), dtype=int64)
# tf.Tensor(2, shape=(), dtype=int64)
# tf.Tensor(3, shape=(), dtype=int64)
# tf.Tensor(4, shape=(), dtype=int64)
# tf.Tensor(5, shape=(), dtype=int64)
# tf.Tensor(6, shape=(), dtype=int64)
# tf.Tensor(7, shape=(), dtype=int64)
# tf.Tensor(8, shape=(), dtype=int64)
# tf.Tensor(9, shape=(), dtype=int64)