如何在自动编码器的无监督学习中找到损失函数值?

时间:2020-05-06 16:26:12

标签: python-3.x tensorflow keras autoencoder loss-function

为了简化我的故事:我正在尝试使用使用keras / tensorflow的编码器方法对我的UNLABELED数据进行降维测试。

因此,我查看了互联网,发现了一个不错的代码,可能对我有用。这是链接:https://github.com/IvanBongiorni/TensorFlow2.0_Notebooks/blob/master/TensorFlow2.0__02.01_Autoencoder_for_Dimensionality_Reduction.ipynb

尽管,我只对编码器部分感兴趣。因此,在这里我添加了部分代码,但是如果我不给出任何目标/标签,我将无法弄清楚代码如何计算损失函数值。我是新来的使用keras / tensorflow的人,只有在提供真实和预测的标签时,才能生成思想损失函数值。

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()))

有人可以告诉我我所缺少的吗?谢谢

0 个答案:

没有答案