0

I am trying to compute the input signal "maximizing" the activation of a given neuron of an encoder NN (the goal is to understand what my latent features are modelling).

I wrote a little python script which loads the .h5 file with the trained encoder model and builds a tensorflow graph to compute iteratively the "best activation signal".

It seems like my tensorflow implementation is not right. Despite the fact that I run tf.initialize_all_variables(), a FailedPreconditionError: Attempting to use uninitialized value X error is raised.

I am a little new in the use of tensorflow without using keras so this may be a trivial mistake but I could really use some help on this. Here is my code. Thanks a lot.

import tensorflow as tf
import tensorflow.keras as keras

import numpy as np

import matplotlib.pyplot as plt

input_sequence_size = 20
input_dim = 4
encoding_dim = 10

model_save = 'siple_autoencoder_encoder.h5'
model = keras.models.load_model(model_save)

lambda_param = 0.1
n_steps = 100

X = tf.Variable(tf.random_uniform([1, input_sequence_size * input_dim], -1.0, 1.0), name = 'X')

prediction = model.predict(X, steps = 1)

y = tf.gather_nd(prediction, [[0]], batch_dims=0, name=None)

gradient = tf.gradients(y, [X])[0]

step = tf.assign(X, X + lambda_param * gradient)

init = tf.initialize_all_variables()

with tf.Session() as sess:
    
    sess.run(init)
    
#    output = y.eval()
    
    for i in range(n_steps):
        sess.run(step)
        
    activation_signal_1 = X.eval()
  • check whether this answers your question: https://stackoverflow.com/questions/34001922/failedpreconditionerror-attempting-to-use-uninitialized-in-tensorflow – Harish Vutukuri Dec 04 '20 at 15:36
  • Thanks for answering. I don't think this topic is the solution. I feel like I am doing what is recommended in the answers. I am already using an initializer in my code. But maybe, am I using it not properly. – Yvart Maxime Dec 06 '20 at 23:29
  • `tf.initialize_all_variables()` is depreciated can you use `tf.global_variables_initializer()` instead. You can also use Tensorflow 2 which runs on a eager mode, where you don't have to initialize any variables. –  Dec 14 '20 at 08:16

0 Answers0