티스토리 뷰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import numpy as np import tensorflow as tf import tensorflow_hub as hub import tensorflow_datasets as tfds class ImdbModel: def __init__(self): self.train_validation_split = None self.train_data = None self.validation_data = None self.test_data = None self.model = None self.train_examples_batch = None def execute(self): # self.env_info() self.download_data() # IMDB 데이터셋 마이닝 self.create_sample() # 샘플데이터 생성 self.create_model() # 모델 생성 self.train_model() # 모델 훈련 self.eval_model() # 모델 평가 def download_data(self): self.train_validation_split = tfds.Split.TRAIN.subsplit([6, 4]) (self.train_data, self.validation_data), self.test_data = tfds.load( name="imdb_reviews", split=(self.train_validation_split, tfds.Split.TEST), as_supervised=True) @staticmethod def env_info(): print("버전: ", tf.__version__) print("즉시 실행 모드: ", tf.executing_eagerly()) print("허브 버전: ", hub.__version__) print("GPU ", "사용 가능" if tf.config.experimental.list_physical_devices("GPU") else "사용 불가능") def create_sample(self): self.train_examples_batch, train_labels_batch \ = next(iter(self.train_data.batch(10))) print(self.train_examples_batch) def create_model(self): embedding = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1" hub_layer = hub.KerasLayer(embedding, input_shape=[], dtype=tf.string, trainable=True) hub_layer(self.train_examples_batch[:3]) self.model = tf.keras.Sequential() self.model.add(hub_layer) self.model.add(tf.keras.layers.Dense(16, activation='relu')) self.model.add(tf.keras.layers.Dense(1, activation='sigmoid')) print(self.model.summary()) self.model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) def train_model(self): history = self.model.fit(self.train_data.shuffle(10000).batch(512), epochs=20, validation_data=self.validation_data.batch(512), verbose=1) def eval_model(self): results = self.model.evaluate(self.test_data.batch(512), verbose=2) for name, value in zip(self.model.metrics_names, results): print("%s: %.3f" % (name, value)) if __name__ == '__main__': m = ImdbModel() m.execute() | cs |
'5. 파이썬' 카테고리의 다른 글
[텐서플로] 모델의 저장과 로드 save_load.py (0) | 2020.05.14 |
---|---|
[텐서플로] 패션 MNIST 예제 fashion_mnist.py (0) | 2020.05.14 |
[텐서플로] 네이버 증권 분석 naver_stock.py (0) | 2020.05.14 |
[텐서플로] RNN LSTM 샘플 rnn_lstm.py (0) | 2020.05.14 |
[텐서플로] 케라스 샘플 (0) | 2020.05.14 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ERD
- Oracle
- springMVC
- jQuery
- JPA
- intellij
- JUnit
- maven
- AWS
- Mlearn
- Java
- SpringBoot
- Eclipse
- Django
- tensorflow
- mariadb
- database
- vscode
- docker
- React
- Algorithm
- Git
- COLAB
- terms
- Mongo
- FLASK
- SQLAlchemy
- KAFKA
- nodejs
- Python
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함