I need to load from text files rows that contain string representations of 2D arrays, for later use in training a Tensorflow CNN, but I cannot get the strings converted into a format Tensorflow likes. I have tried all sorts of combinations of apply/map/various functions, but always get some cryptic error. Below is a toy example code that is close to working, but still throws an error:
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
import tensorflow as tf
import numpy as np
import pandas as pd
from ast import literal_eval
def df_to_dataset(dataframe):
Y = tf.convert_to_tensor( dataframe['Y'].values )
X = tf.convert_to_tensor(
dataframe['X'].apply(literal_eval).apply(np.array).values
)
return tf.data.Dataset.from_tensor_slices( ( X , Y )
)
data = [[ 1, "[[0,1],[0,1]]" ] , [ 0 , "[[1,0],[1,0]]" ]]
df = pd.DataFrame(data, columns=['Y','X'])
dataset = df_to_dataset(df)
for feature in dataset.take(1):
print( feature )