Tensorflow autoencoder cost not decreasing?

2024/10/5 11:31:57

I am working on unsupervised feature learning using autoencoders using Tensorflow. I have written following code for the Amazon csv dataset and when I am running it the cost is not decreasing at every iteration. Can you please help me find the bug in the code.

from __future__ import division, print_function, absolute_importimport tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df=pd.read_csv('../dataset/amazon_1_b.csv')
df=df.drop(df.columns[0], axis=1)
#df1, df2 = df[:25000, :], df[25000:, :] if len(df) > 25000 else df, None
df1=df.head(25000)
df2=df.tail(len(df)-25000)
trY=df1['ACTION'].as_matrix()
teY=df2['ACTION'].as_matrix()
df1=df1.drop(df.columns[9], axis=1)
df2=df2.drop(df.columns[9], axis=1)
trX=df1.as_matrix()
teX=df2.as_matrix()# Parameters
learning_rate = 0.01
training_epochs = 50
batch_size = 20
display_step = 1
examples_to_show = 10# Network Parameters
n_hidden_1 = 20 # 1st layer num features
n_hidden_2 = 5 # 2nd layer num features
n_input = trX.shape[1] # MNIST data input (img shape: 28*28)# tf Graph input (only pictures)
X = tf.placeholder("float", [None, n_input])weights = {'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),'decoder_h1': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])),'decoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_input])),
}
biases = {'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1])),'encoder_b2': tf.Variable(tf.random_normal([n_hidden_2])),'decoder_b1': tf.Variable(tf.random_normal([n_hidden_1])),'decoder_b2': tf.Variable(tf.random_normal([n_input])),
}# Building the encoder
def encoder(x):# Encoder Hidden layer with sigmoid activation #1layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']),biases['encoder_b1']))# Decoder Hidden layer with sigmoid activation #2layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']),biases['encoder_b2']))return layer_2# Building the decoder
def decoder(x):# Encoder Hidden layer with sigmoid activation #1layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']),biases['decoder_b1']))# Decoder Hidden layer with sigmoid activation #2layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']),biases['decoder_b2']))return layer_2# Construct model
encoder_op = encoder(X)
decoder_op = decoder(encoder_op)# Prediction
y_pred = decoder_op
# Targets (Labels) are the input data.
y_true = X# Define loss and optimizer, minimize the squared error
cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)# Initializing the variables
init = tf.initialize_all_variables()# Launch the graph
# Using InteractiveSession (more convenient while using Notebooks)
sess = tf.InteractiveSession()
sess.run(init)total_batch = int(trX.shape[0]/batch_size)
# Training cycle
for epoch in range(training_epochs):# Loop over all batchesfor i in range(total_batch):batch_xs= trX[batch_size*i:batch_size*(i+1)]# Run optimization op (backprop) and cost op (to get loss value)_, c = sess.run([optimizer, cost], feed_dict={X: batch_xs})# Display logs per epoch stepif epoch % display_step == 0:print("Epoch:", '%04d' % (epoch+1),"cost=", "{:.9f}".format(c))print("Optimization Finished!")# Applying encode and decode over test set
encode_decode = sess.run(y_pred, feed_dict={X: teX})

The link to the dataset is here. The link to the python file is here.

Following is the result untill 31 epoch and it remains same till all 50 epoch.

Epoch: 0001 cost= 18134403072.000000000
Epoch: 0002 cost= 18134403072.000000000
Epoch: 0003 cost= 18134403072.000000000
Epoch: 0004 cost= 18134403072.000000000
Epoch: 0005 cost= 18134403072.000000000
Epoch: 0006 cost= 18134403072.000000000
Epoch: 0007 cost= 18134403072.000000000
Epoch: 0008 cost= 18134403072.000000000
Epoch: 0009 cost= 18134403072.000000000
Epoch: 0010 cost= 18134403072.000000000
Epoch: 0011 cost= 18134403072.000000000
Epoch: 0012 cost= 18134403072.000000000
Epoch: 0013 cost= 18134403072.000000000
Epoch: 0014 cost= 18134403072.000000000
Epoch: 0015 cost= 18134403072.000000000
Epoch: 0016 cost= 18134403072.000000000
Epoch: 0017 cost= 18134403072.000000000
Epoch: 0018 cost= 18134403072.000000000
Epoch: 0019 cost= 18134403072.000000000
Epoch: 0020 cost= 18134403072.000000000
Epoch: 0021 cost= 18134403072.000000000
Epoch: 0022 cost= 18134403072.000000000
Epoch: 0023 cost= 18134403072.000000000
Epoch: 0024 cost= 18134403072.000000000
Epoch: 0025 cost= 18134403072.000000000
Epoch: 0026 cost= 18134403072.000000000
Epoch: 0027 cost= 18134403072.000000000
Epoch: 0028 cost= 18134403072.000000000
Epoch: 0029 cost= 18134403072.000000000
Epoch: 0030 cost= 18134403072.000000000
Epoch: 0031 cost= 18134403072.000000000
Answer

Your optimization method, RMSPropOptimizer, seems really slow in this case.

You may want to try adams' solution, at least that worked for me.

https://en.xdnf.cn/q/70465.html

Related Q&A

Seconds since epoch to relative date

Im working with dates since epoch, and already got, for example:date = 6928727.56235Id like to transform this into another relative format, so that Ill be able to transform this into something relative…

ring buffer with numpy/ctypes

Im developing a client which will receive the [EEG] data over tcp and write it to the ring buffer. I thought it can be very convenient to have the buffer as a ctypes or numpy array because its possible…

Get all available timezones

Im currently working on an application that is required to support multiple timezones.For that, Im using the dateutil library. Now, I need a way to present the user a list of all available timezones th…

Load blob image data into QPixmap

I am writing a program using PyQt4 for front-end GUI and this program accesses a back-end database (which can be either MySQL or SQLite). I need to store some image data in the database and below is th…

Fetch a value of SQLalchemy instrumentedattribute

How can I fetch the value of a InstrumentedAttribute object in SQLalchemy:(Pdb) ResultLine.item_reference_1 <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x793dc90>The above statemen…

python super calling child methods

There are numerous questions on the usage of super() but none of them appears to answer my question. When calling super().__init__() from a subclass, all method calls in the super-constructor are actua…

How to create space between subplots? [duplicate]

This question already has answers here:Manipulation on vertical space in matplotlib subplots(3 answers)Closed 2 years ago.The title pretty much says it all. I have a notebook containing two subplots an…

How to (re)name an empty column header in a pandas dataframe without exporting to csv

I have a pandas dataframe df1 with an index column and an unnamed series of values. I want to assign a name to the unnamed series. The only way to do this that I know so far is to export to df1.csv usi…

Capturing the video stream from a website into a file

For my image classification project I need to collect classified images, and for me a good source would be different webcams around the world streaming video in the internet. Like this one:https://www.…

Recreating time series data using FFT results without using ifft

I analyzed the sunspots.dat data (below) using fft which is a classic example in this area. I obtained results from fft in real and imaginery parts. Then I tried to use these coefficients (first 20) to…