skip to Main Content

I realize that this might be a simple question, but I have been searching for a straightforward answer and cannot find it, neither in the documentation or on forums.
The threads I have found were about very specific questions like converting audio to a tensor via JSON.

So my question is: How do I convert a JSON file to a Tensorflow tensor or tensordataset to then use it to train a neural network with Tensorflow?

2

Answers


  1. Forgive my shallow knowledge,the way I thought is convert json to numpy,then convert numpy to tensor

    import json
    import numpy as np
    import tensorflow as tf
    
    json_input = '{"rings" : [[[-8081441.0, 5685214.0], [-8081446.0, 5685216.0], [-8081442.0, 5685219.0], [-8081440.0, 5685211.0], [-8081441.0, 5685214.0]]]}'
    rings = np.array(dict["rings"])
    rings = tf.convert_to_tensor(rings, tf.float32, name='rings')
    

    I am not sure if there have direct way for json to tensor, hope it can help you 🙂

    Login or Signup to reply.
  2. The answer depends on the structure of your JSON data.

    You can try this:

    import json
    import numpy as np
    import tensorflow as tf
    
    features = np.array(data['features'])
    labels = np.array(data['labels'])
    
    features_tensor = tf.convert_to_tensor(features)
    labels_tensor = tf.convert_to_tensor(labels)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search