How to use .eval() inside the function?


I am running into a problem, I cannot solve by myself.

I am doing this example:

But I would like do use my own stft-function, which i wrote in numpy instead of tf.signal.stft

So I changed the function “def get_spectrogram(waveform)” and I try to convert the input Tensor into a numpy array by using *.eval() and “get_spectrogram” is used in “get_spectrogram_and_label_id(audio, label)” which again is used inside a dataset map

spectrogram_ds =, num_parallel_calls=AUTOTUNE) 

Since this mapping is done in GraphMode, and not EagerlyMode, i cannot use .numpy() and have to use .eval() instead.

However .eval() asked for a session and it has to be the same session the map function is used for the dataset.

Has anybody ran into this problem and can help?


def get_spectrogram(waveform): # Padding for files with less than 16000 samples zero_padding = tf.zeros([16000] - tf.shape(waveform), dtype=tf.float32) # Concatenate audio with padding so that all audio clips will be of the # same length waveform = tf.cast(waveform, tf.float32) equal_length = tf.concat([waveform, zero_padding], 0) equal_length = equal_length.eval() # <-- HERE IS THE PROBLEM spectrogram = do_stft(equal_length, 512, 128) #<-- uses NUMPY return spectrogram def get_spectrogram_and_label_id(audio, label): print(sess.graph) spectrogram = get_spectrogram(audio.eval(session=sess)) spectrogram = tf.expand_dims(spectrogram, -1) label_id = tf.argmax(label == commands) return spectrogram, label_id spectrogram_ds =, num_parallel_calls=AUTOTUNE) 

submitted by /u/alex_bababu
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *