2023-03-11
Below is my example inspired by https://github.com/mostafaalishahi/eICU_Benchmark_updated
Notes:
tf.print() and run_eagerly=True
yield function. which returns an object defining the generator to the caller (see https://docs.python.org/3/reference/expressions.html?highlight=yield%20return#yield-expressions)
# Batch generator for binary classification problems that may be heavily imbalanced
def batch_generator( config, X, Y, rng=np.random.RandomState(0) ):
c1 = np.where(Y.squeeze() ==1 )[0]
c2 = np.where(Y.squeeze() ==0 )[0]
print( 'size of class1:', len(c1),'size of class2:', len(c2) )
all_index = list(range(X.shape[0]))
while len(all_index) > (config.batch_size*0.2):
idx1 = rng.choice( c1, int(batch_size/2) )
idx2 = rng.choice( c2, int(batch_size/2) )
idx = np.hstack( (idx1, idx2))
x_batch = X[idx]
y_batch = Y[idx]
idx = list(set(all_index) - set(idx))
data_selection = list(zip(x_batch, y_batch))
# randomize the subset
random.shuffle(data_selection)
x_batch, y_batch = zip(*data_selection)
x_batch = np.array(x_batch)
y_batch = np.array(y_batch)
yield x_batch, y_batch
def my_custom_metric( y_true, y_pred):
y = y_true[0].numpy(); print( '???\n\n', y ) # print as numpy
tf.print(y_pred[0], output_stream=sys.stdout) # print as tensor
tf.print(y_pred[1], output_stream=sys.stdout)
return -1 # TODO: return something meaningful
model.compile( loss = my_custom_loss( n_intervals ),
optimizer = optim,
metrics=[ my_custom_metric ],
run_eagerly=True ) # to enable tf.printing