Search code examples
pythonpython-3.xtensorflowdeep-learningtflearn

how to shape tensor from fully connected to to 4-D


I am new at using tensorflow . I was trying to add CNN inside auto-encoder. I was using the example code from tflearn. My initial code was

X, Y, testX, testY = mnist.load_data(one_hot=True)
# Building the encoder
encoder = tflearn.input_data(shape=[None, 28* 28*1], name='input')
encoder = tflearn.fully_connected(encoder, 256)

# Building the decoder
decoder = tflearn.fully_connected(encoder, 256)
decoder = tflearn.fully_connected(decoder, 784, activation='sigmoid')

# Regression, with mean square error
net = tflearn.regression(decoder, optimizer='adam', learning_rate=0.001,
                         loss='mean_square', metric=None)
# Training the auto encoder
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(X, X, n_epoch=20, validation_set=(testX, testX),
          run_id="auto_encoder", batch_size=256)

Now i added the CNN code before building the decoder like this .

encoder = tflearn.input_data(shape=[None, 28* 28*1], name='input')
encoder = tflearn.fully_connected(encoder, 256)

# my modification
network = conv_3d(encoder, 32, 3, activation='relu', regularizer="L2")

# Building the decoder
decoder = tflearn.fully_connected(network, 256)
decoder = tflearn.fully_connected(decoder, 784, activation='sigmoid')

But i get the the following error

    network = conv_2d(encoder, 32, 3, activation='relu', regularizer="L2")
  File "/usr/local/lib/python3.5/dist-packages/tflearn/layers/conv.py", line 66, in conv_2d
    assert len(input_shape) == 4, "Incoming Tensor shape must be 4-D"
AssertionError: Incoming Tensor shape must be 4-D

Now how do i convert this encoder variable to 4D Tensor ? Or is there any other way to solve the problem ?


Solution

  • SO that answer was a simple typo correction.

    encoder = tflearn.input_data(shape=[None, 28* 28*1], name='input')
    encoder = tflearn.fully_connected(encoder, 256)
    
    # Correction here 3d to 2d 
    network = conv_2d(encoder, 32, 3, activation='relu', regularizer="L2")
    
    # Building the decoder
    decoder = tflearn.fully_connected(network, 256)
    decoder = tflearn.fully_connected(decoder, 784, activation='sigmoid')