keras v 2.0 class_weight, sample_weight

class_weight

각 클래스의 상대적 가중치에 영향을 주는 방식 imbalence 데이터에서 주로 사용 ex) 스팸메일

sample_weights

동일한 클래스에서 속하는 샘플의 상대적 무게를 추가로 제어 배치의 샘플에 대해 동일한 신뢰도가 없는경우에 사용 ex) 가변 불확실성을 가진 회귀 측정 시

ex) multi class multi label 의 경우
보통 뒤에 zero padding을 붙이는데 이를 학습하지 않기 위해서는 sample_weights를 이용하여 padding 부분 가중치를 0으로 두어 밸런스를 잡을 수 있음

사용 예시
def sample_weight(labels):
    sample_weights = []
    non_weights_list = ["<padding>"]
    for label in labels:
         weight = [0 if i in non_weights_list else 1 for i in label]
         sample_weights.append(np.array(weight))
    weights = np.stack(sample_weights, axis=0)
    return weights    
         
sample_weight = sample_weight(train_data["label"])
self.model.fit(x=(train_data["train"], y=train_data["label"],
               validation_split=0.1,
               batch_size=batch_size,
               shuffle=True,
               epochs=epoch,
               sample_weight = sample_weight)

Tensorflow v1 -> v2 변경

Tensorflow import v1 -> v2

from tensorflow.contrib import layers as contrib_layers
-> 변경 : from tensorflow.keras import layers as contrib_layers

from tensorflow.contrib import tpu as contrib_tpu
-> 제거

tf.train.Optimizer
-> 변경 : tf.keras.optimizers.Optimizer

tf.logging
-> 변경 : tf.compat.v1.logging

tf.gfile.GFile
-> 변경 : tf.io.gfile.GFile

tf.contrib.predictor.from_saved_model
-> 변경 : tf.lite.TFLiteConverter.from_saved_model

tf.python_io.TFRecordWriter(output_file)
-> 변경 : tf.io.TFRecordWriter

tf.get_variable
-> 변경 : tf.Variable

  • parameter initializer -> initial_value
    -> 변경2 : tf.compat.v1.get_variable

tf.GraphDef()
-> 변경 : tf.compat.v1.GraphDef()

tf.gfile.GFile
-> 변경 : tf.compat.v2.io.gfile.GFile

tf.ConfigProto()
-> 변경 : tf.compat.v1.ConfigProto()

optimizer 설정
keras 2.2.4 => lr
keras 2.3.0 => learning_rate

  • Tensorflow 1.X 버전의 contrib은 대체 불가 https://www.tensorflow.org/guide/migrate?hl=ko