배우기 어려움 → 할 줄 알면 엄청 멋있음
다양한 언어, 다양한 platform 활용 가능 → Flexibility 매우 뛰어남(TPU사용 가능)
데이터가 많으면 많을 수록 실력 발휘
파생 플러그인, 연동 Framework 풍부
강력한 High Level API
open ~ 0.9
placeholder, Variable, constant, queue runners, Readers
1.0 ~ 1.2
High Level APIs
1.2 ~ current
cuda Version ups, eager mode, extention estimator, performance optimization
"Engineering mind"가 적극적으로 반영된 Framework
Tensorflow 세계 점유율 압도적 1위 → 기술을 이끄는 것은 산업
이러한 service productivity를 높이기 위한 많은 노력의 산물 Tensorflow
연구자와 개발자 모두가 편리하게 사용 할 수 있는 High Level API를 제공함으로써 협업 능률 극대화
Image('./tensorflow_presentation_images/image1.png')
데이터를 서비스로 모은다면?
서비스 사용자가 늘어난다면?
데이터가 엄청나게 커진다면?
더 향상된 Performance가 요구된다면?
Pipeline, Model의 규모가 커진다면?
Image('./tensorflow_presentation_images/image2.png')
# FFNN
l1 = tf.layers.dense(x, 10, activation=tf.nn.relu)
l2 = tf.layers.dense(l1, 10, activation=tf.nn.relu)
l3 = tf.layers.dense(l2, 10, activation=tf.nn.relu)
out = tf.layers.desne(l3, 3, activation=tf.nn.softmax)
# CNN
conv1 = tf.layers.conv2d(x, filters=16, kernel_size=3)
pool1 = tf.layers.max_pooling2d(conv1, pool_size=2, strides=2)
conv2 = tf.layers.conv2d(pool1, filters=32, kernel_size=[5, 4])
pool2 = tf.layers.max_pooling2d(conv2, pool_size=2, strides=2)
flat = tf.layers.flatten(pool2)
dropout_flat = tf.layers.dropout(flat, rate=0.3, training=is_training)
fc1 = tf.layers.dense(dropout_flat, 1000)
dropout_fc1 = tf.layers.dropout(fc1, rate=0.3, training=is_training)
fc2 = tf.layers.dense(dropout_fc1, 100, activation=tf.nn.relu)
dropout_fc2 = tf.layers.dropout(fc2, rate=0.3, training=is_training)
out = tf.layers.dense(dropout_fc2, 3, activation=tf.nn.softmax)
## RNN
cell = tf.contrib.rnn.BasicRNNCell(128)
output, state = tf.nn.dynamic_rnn(cell, x, sequence_length=lengths, dtype=tf.float32)
### loss functions
mse_loss = tf.losses.mean_squared_error(label, prediction)
abs_loss = tf.losses.absolute_difference(label, prediction)
sum_loss = tf.losses.add_loss(label, prediction)
cosd_loss = tf.losses.cosine_distance(label, prediction)
hinge_loss = tf.losses.hinge_loss(label, prediction)
### metrics
tf.metrics.accuracy(label, prediction)
tf.metrics.auc(label, prediction)
tf.metrics.average_precision_at_k(label, prediction)
tf.metrics.false_negatives(label, prediction)
tf.metrics.false_negatives_at_thresholds(label, prediction)
tf.metrics.false_positives(label, prediction)
tf.metrics.false_positives_at_thresholds(label, prediction)
tf.metrics.mean(label, prediction)
tf.metrics.mean_absolute_error(label, prediction)
tf.metrics.mean_cosine_distance(label, prediction)
tf.contrib.metrics.auc_using_histogram(label, scores)
tf.contrib.metrics.auc_using_histogram(label, scores)
tf.contrib.metrics.confusion_matrix(labels, prediction)
tf.contrib.metrics.recall_at_precision(label, prediction)
한 번의 open으로 training간 reading만 수행 → file open에서 생기는 병목 최소화
data reading에 최적화된 serializing 기술로 scan 속도 매우 빠름
HDFS와 같은 대용량 File System에 적합
Dataset 자체적으로 reading시 multi-processing 수행
pipeline 구성이 매우 간결해짐
### TFRecord Transform
with tf.python_io.TFRecordWriter('/path/to/write/file.tfrecord') as write:
example = tf.train.Example()
example.features.feature['key1'].float_list.value.extend(float_list)
example.features.feature['key2'].int64_list.value.extend(int_list)
example.features.feature['key3'].float_list.value.append(float_value)
example.features.feature['key4'].int64_list.value.append(int_value)
serialized = example.SerializeToString()
writer.write(serialized)
### TFRecord parser
def parser(serialized_example):
feature = {
'key1': tf.FixedLenFeature([10], tf.float32),
'key2': tf.VarLenFeature(tf.int64),
'key3': tf.FixedLenFeature([1], tf.float32),
'key4': tf.FixedLenFeature([1], tf.int64),
}
parsed_feature = tf.parse_single_example(serialized_example, feature)
feature1 = parsed_feature['key1']
feature2 = parsed_feature['key2']
feature3 = parsed_feature['key3']
label = parsed_feature['key4']
return feature1, feature2, feature3, label
### Reading TFRecord
dataset = tf.data.TFRecordDataset('/path/from/read/file.tfrecord')
dataset = dataset.map(parser)
dataset = dataset.batch(32).shuffle(50000).repeat(100)
itr = dataset.make_one_shot_iterator()
feature1, feature2, feature3, label = itr.get_next()
checkpoint 자동 저장, 저장된 모델 자동 restore
자체 export 기능
train, evaluate, prediction 간편하게 관리
pipeline, model, runner을 블럭 조립하듯이 모듈로 사용 가능
코드 줄 대폭 감소, 깔끔한 추상화로 가독성 증가
est = tf.estimator.Estimator(model_fn, model_dir)
est.train(train_input_fn)
est.evaluate(eval_input_fn)
est.predict(pred_input_fn)
def input_fn():
dataset = tf.data.TFRecordDataset('/path/from/read/file.tfrecord')
dataset = dataset.map(parser)
dataset = dataset.batch(32).shuffle(50000).repeat(100)
itr = dataset.make_one_shot_iterator()
feature1, feature2, feature3, label = itr.get_next()
return {'feature1': feature1,
'feature2': feature2,
'feature3': feature3,
}, label
def model_fn(features, label, mode):
### model code here ###
if mode == tf.estimator.ModeKeys.TRAIN:
global_step = tf.train.get_global_step()
loss = ...
train_op = ...minimize(loss, global_step)
estimator_spec = tf.estimator.EstimatorSpec(mode=mode,
train_op=train_op,
loss=loss)
elif mode == tf.estimator.ModeKeys.EVAL:
loss = ...
metirc = ...
eval_metric_ops = {'metric': metric}
estimator_spec = tf.estimator.EstimatorSpec(mode=mode,
loss=loss,
eval_metric_ops=eval_metric_ops)
elif mode == tf.estimator.ModeKeys.PRED:
prediction = {'output1': output1, 'output2': output2}
estimator_spec = tf.estimator.EstimatorSpec(mode=mode,
predictions=predictions)
else:
raise Exception('invalid estimator')
return estimator_spec
C++ 기반 서버
grpc 프로토콜 사용
server down 없이 model update 가능
### Serving receiver function
def _serving_input_receiver_fn():
feature = {
'key1': tf.FixedLenFeature([10], tf.float32),
'key2': tf.VarLenFeature(tf.int64),
'key3': tf.FixedLenFeature([1], tf.float32),
}
serialized_example = tf.placeholder(tf.string)
receiver_tensors = {'example': serialized_example}
features = tf.parse_example(serialized_example, feature)
return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
### export output
elif PRED:
prediction = {'output1': output1, 'output2': output2}
estimator_spec = tf.estimator.EstimatorSpec(
mode=mode,
predictions=predictions,
export_outputs={'response': tf.estimator.export.PredictOutput(predictions)})
est = tf.estimator.Estimator(model_fn, model_dir)
est.export_savedmodel(export_dir_base='/path/to/export',
serving_input_receiver_fn=serving_input_receiver_fn)
$ tensorflow_model_server --port=port-numbers --model_name=model-name-you-wanted --model_base_path=/absolute/path/of/your/saved/model
tensorflow model server api installation guide
https://www.tensorflow.org/serving/setup
client example
https://github.com/.../tensor.../example/inception_client.py
Image('./tensorflow_presentation_images/image3.png')
Image('./tensorflow_presentation_images/image4.png')