Search code examples
pythontensorflowdeep-learningautoencoder

Cast string to float is not supported - Denoising Autoencoder for time series data


I am trying to build a denoising autoencoder for time series data. But I am getting, "Cast string to float is not supported" when I try to train the model.

The data that I have used has the following shape:

print(np.shape(noisy_samples))
print(np.shape(samples))

Output:

(98, 8000, 2) (98, 8000, 2)

Data type has been changed to numpy array:

data_noisy=np.array(noisy_samples)
data_pure=np.array(samples)
print(type(data_pure))
print(type(data_noisy))

Output:

<class 'numpy.ndarray'> <class 'numpy.ndarray'>

Autoencoder Model:

import tensorflow.keras
from tensorflow.keras.models import Sequential, save_model, Model
from tensorflow.keras.layers import Conv1D, Conv1DTranspose, Input, MaxPooling1D, Flatten, Dense, Reshape, UpSampling1D
from tensorflow.keras.constraints import max_norm
import matplotlib.pyplot as plt
import numpy as np
import math

batch_size = 5
no_epochs = 5
train_test_split = 0.3
validation_split = 0.2
verbosity = 1
max_norm_value = 2.0
input_sig = Input(batch_shape=(1,8000,2))

x = Conv1D(16, 3, activation="relu", padding="same")(input_sig) 
#x = BatchNormalization()(x)
x = MaxPooling1D(2, padding="same")(x)
x = Conv1D(1, 3, activation="relu", padding="same")(x) 
#x = BatchNormalization()(x)
encoded = MaxPooling1D(2, padding="same")(x) 

encoder = Model(input_sig, encoded)


x = Conv1D(1, 3, activation="relu", padding="same")(encoded) 
#x = BatchNormalization()(x)
x = UpSampling1D(2)(x) 
x = Conv1D(16, 2, activation='relu', padding="same")(x) 
#x = BatchNormalization()(x)
x = UpSampling1D(2)(x) 
decoded = Conv1D(2, 3, activation='sigmoid', padding='same')(x) 
model = Model(input_sig, decoded)
model.summary()
model.compile(optimizer='adam', loss='binary_crossentropy')
model.fit(data_noisy, data_pure, epochs = no_epochs, batch_size =batch_size )

Output:

Model: "model_1" _________________________________________________________________ Layer (type) Output Shape Param #
================================================================= input_1 (InputLayer) [(1, 8000, 2)] 0
_________________________________________________________________ conv1d (Conv1D) (1, 8000, 16) 112
_________________________________________________________________ max_pooling1d (MaxPooling1D) (1, 4000, 16) 0
_________________________________________________________________ conv1d_1 (Conv1D) (1, 4000, 1) 49
_________________________________________________________________ max_pooling1d_1 (MaxPooling1 (1, 2000, 1) 0
_________________________________________________________________ conv1d_2 (Conv1D) (1, 2000, 1) 4
_________________________________________________________________ up_sampling1d (UpSampling1D) (1, 4000, 1) 0
_________________________________________________________________ conv1d_3 (Conv1D) (1, 4000, 16) 48
_________________________________________________________________ up_sampling1d_1 (UpSampling1 (1, 8000, 16) 0
_________________________________________________________________ conv1d_4 (Conv1D) (1, 8000, 2) 98
================================================================= Total params: 311 Trainable params: 311 Non-trainable params: 0 _________________________________________________________________ Epoch 1/5 --------------------------------------------------------------------------- UnimplementedError Traceback (most recent call last) in () 46 model.summary() 47 model.compile(optimizer='adam', loss='binary_crossentropy') ---> 48 model.fit(data_noisy, data_pure, epochs = no_epochs, batch_size =batch_size ) 49

6 frames /usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 58 ctx.ensure_initialized() 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, ---> 60 inputs, attrs, num_outputs) 61 except core._NotOkStatusException as e: 62 if name is not None:

UnimplementedError: Cast string to float is not supported [[node model_1/Cast (defined at :48) ]] [Op:__inference_train_function_79115]

Function call stack: train_function

I have tried checking my data samples, it does not contain any string values.


Solution

  • Just run a check on your arrays to see where you might have a string value. Do something like (if arr is your array):

    q = []   
    for i in arr:
        try:
            q.append(np.float(i))
        except ValueError as e:
            q.append(np.nan)
    baz = np.where(np.isnan(q))