0

To simplify my story: I was trying to test dimensionality reduction on my UNLABELED data with the encoder method using keras/tensorflow.

So I looked at the internet and found a nice code that might be useful for me. Here's the link: https://github.com/IvanBongiorni/TensorFlow2.0_Notebooks/blob/master/TensorFlow2.0__02.01_Autoencoder_for_Dimensionality_Reduction.ipynb

Although, I'm interested just in the encoder part. So here I added part of that code to mine, but I can't figure out how the code calculates loss function values if I didn't give any targets/labels. I'm new using keras/tensorflow and thought loss function values could only be generated if you give true and predicted labels.

data = np.random.randint(1, 100, 500)
df = pd.DataFrame({'f1':data, 'f2':data**2, 'f3':data*0.33, 'f4':data/20})

scaler = StandardScaler()
scaled_df = scaler.fit_transform(df)
scaled_df = pd.DataFrame(scaled_df, columns=['f1','f2','f3','f4'])

n_input_layer = scaled_df.shape[1]
n_encoding_layer = 1
n_output_layer = n_input_layer

# AUTOENCODER
autoencoder = tf.keras.models.Sequential([
# ENCODER
    Dense(n_input_layer, input_shape = (n_input_layer,), activation = 'elu'),    
# CENTRAL LAYER
    Dense(n_encoding_layer, activation = 'elu', name = 'central_layer'), 
# DECODER
    Dense(n_output_layer, activation = 'elu')])

n_epochs = 5000
loss = tf.keras.losses.MeanSquaredError()
optimizer = tf.optimizers.Adam(learning_rate = 0.001, decay = 0.0001, clipvalue = 0.5)

loss_history = []  # save loss improvement
for epoch in range(n_epochs):

    with tf.GradientTape() as tape:
        current_loss = loss(autoencoder(scaled_df.values), scaled_df.values)

    gradients = tape.gradient(current_loss, autoencoder.trainable_variables)    
    optimizer.apply_gradients(zip(gradients, autoencoder.trainable_variables)) 

    loss_history.append(current_loss.numpy())  # save current loss in its history

    # show loss improvement every 200 epochs
    if (epoch+1) % 200 == 0:
        print(str(epoch+1) + '.\tLoss: ' + str(current_loss.numpy()))

Could anyone show me what I am missing? Thanks

user026
  • 638
  • 4
  • 14

0 Answers0