Advertisement
jack06215

[TensorFlow] custom loss test

Jul 28th, 2020
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. from keras import objectives
  4. from keras import backend as K
  5. def pull_away_loss(g):
  6.  
  7.     # normalizatrion (using vectorization method)
  8.     Nor = tf.norm(g, axis=1)
  9.     Nor_mat = tf.tile(tf.expand_dims(Nor, axis=1), [1, tf.shape(g)[1]])
  10.     X = tf.divide(g, Nor_mat)
  11.  
  12.     # Multiply and squared
  13.     X_X = tf.square(tf.matmul(X, tf.transpose(X)))
  14.    
  15.     # create mask where diagonal are 0 (i.e. where i != j)
  16.     mask = tf.subtract(tf.ones_like(X_X),
  17.                        tf.linalg.diag(tf.ones([tf.shape(X_X)[0]])))
  18.    
  19.     # multiply: zero out with the mask
  20.     # reduce_sum: calculate the sum of all angle differences
  21.     # divided by N * (N-1), as shown in the formula
  22.     pt_loss = tf.divide(tf.reduce_sum(tf.multiply(X_X, mask)),
  23.                         tf.multiply(
  24.                             tf.cast(tf.shape(X_X)[0], tf.float32),
  25.                             tf.cast(tf.shape(X_X)[0]-1, tf.float32)))
  26.  
  27.     return pt_loss
  28. _EPSILON = K.epsilon()
  29.  
  30. def _loss_tensor(y_true, y_pred):
  31.     y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
  32.     out = -(y_true * K.log(y_pred) + (1.0 - y_true) * K.log(1.0 - y_pred))
  33.     return K.mean(out) * pull_away_loss(y_pred)
  34.  
  35. def _loss_np(y_true, y_pred):
  36.     y_pred = np.clip(y_pred, _EPSILON, 1.0-_EPSILON)
  37.     out = -(y_true * np.log(y_pred) + (1.0 - y_true) * np.log(1.0 - y_pred))
  38.     return np.mean(out, axis=-1)
  39.  
  40. def check_loss(_shape):
  41.     if _shape == '2d':
  42.         shape = (6, 7)
  43.     elif _shape == '3d':
  44.         shape = (5, 6, 7)
  45.     elif _shape == '4d':
  46.         shape = (8, 5, 6, 7)
  47.     elif _shape == '5d':
  48.         shape = (9, 8, 5, 6, 7)
  49.  
  50.     y_a = np.random.random(shape)
  51.     y_b = np.random.random(shape)
  52.  
  53.     out1 = K.eval(_loss_tensor(K.variable(y_a), K.variable(y_b)))
  54.     print(out1)
  55.     # out2 = _loss_np(y_a, y_b)
  56.  
  57.     # assert out1.shape == out2.shape
  58.     # assert out1.shape == shape[:-1]
  59.     #print(np.linalg.norm(out1))
  60.     # print(np.linalg.norm(out2))
  61.     # print(np.linalg.norm(out1-out2))
  62.  
  63.  
  64. def test_loss():
  65.     shape_list = ['2d']
  66.     for _shape in shape_list:
  67.         check_loss(_shape)
  68.         print('======================')
  69.  
  70. if __name__ == '__main__':
  71.     test_loss()
  72.  
  73. import tensorflow as tf
  74.  
  75. from tensorflow.keras.layers import Dense, LSTM, Masking, TimeDistributed, RepeatVector
  76. from tensorflow.keras.preprocessing.sequence import pad_sequences
  77. from tensorflow.keras.models import Sequential
  78.  
  79. import numpy as np
  80.  
  81. #(samples,timesteps,features) samples=4,features=3, timesteps=variable length
  82. train_X = np.array([
  83. [[0, 1, 2], [9, 8, 7]],
  84. [[3, 4, 5]],
  85. [[6, 7, 8], [6, 5, 4]],
  86. [[9, 0, 1], [3, 7, 4]]
  87. ])
  88.  
  89. train_Y = np.array([0, 1, 1, 0])
  90.  
  91. n_in = 3
  92. n_feat = 3
  93. n_out = 1
  94.  
  95. # padding
  96. '''
  97. train_X = np.array([
  98. [[0, 1, 2], [9, 8, 7],[3, 6, 8]],
  99. [[3, 4, 5], [0, 0, 0],[0, 0, 0]],
  100. [[6, 7, 8], [6, 5, 4],[1, 7, 4]],
  101. [[9, 0, 1], [3, 7, 4],[0, 0, 0]]
  102. ])
  103. '''
  104. train_X = pad_sequences(train_X, padding='post')
  105.  
  106. model = Sequential()
  107. inputs = tf.keras.Input(shape=(n_in, n_feat))
  108.  
  109. # Masking
  110. masked_input = Masking(mask_value=0)(inputs)
  111.  
  112. # encoder
  113. encoder = LSTM(100, activation='relu')(masked_input)
  114.  
  115. # decoder 1: reconstruct input sequence
  116. decoder1 = RepeatVector(n_in)(encoder)
  117. decoder1 = LSTM(100, activation='relu', return_sequences=True)(decoder1)
  118. decoder1 = TimeDistributed(Dense(3))(decoder1)
  119.  
  120. # decoder 2: reconstruct output sequence
  121. decoder2 = RepeatVector(n_out)(encoder)
  122. decoder2 = LSTM(100, activation='relu', return_sequences=True)(decoder2)
  123. decoder2 = TimeDistributed(Dense(1))(decoder2)
  124.  
  125. model = tf.keras.Model(inputs=inputs, outputs=[decoder1, decoder2])
  126. model.compile(optimizer='rmsprop', loss=_loss_tensor)
  127. # print(model.summary())
  128.  
  129. model.fit(train_X, [train_X, train_Y], epochs=100, verbose=2)
  130. yhat = model.predict(train_X, verbose=0)
  131. print(yhat)
  132. print('==========================================')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement