0

I'm new to Tensorflow. I followed some online posts and wrote code to get data from a generator. The code looks like this:

def gen(my_list_of_files):
    for fl in my_list_of_files:
        with open(fl) as f:
            for line in f.readlines():
                json_line = json.loads(line)
                features = json_line['features']
                labels = json_line['labels']
                yield features, labels

def get_dataset():
     generator = lambda: gen()
     return tf.data.Dataset.from_generator(generator, (tf.float32, tf.float32))

def get_input():
     dataset = get_dataset()
     dataset = dataset.shuffle(buffer_size=buffer_size)
     dataset = dataset.repeat().unbatch(tf.contrib.data.unbatch())
     dataset = dataset.batch(batch_size, drop_remainder=False)

     # This is where the problem is
     features, labels = dataset.make_one_shot_iterator().get_next()

     return features, labels

When I run this, I get the error:

InvalidArgumentError (see above for traceback): Input element must have a non-scalar value in each component.
     [[node IteratorGetNext (defined at /blah/blah/blah) ]]

Values I'm yielding look like:

[1, 2, 3, 4, 5, 6] # features
7 # label

My understanding of the error was that it cannot iterate over the dataset because it is not a vector. Is my understanding correct? How do I fix this?

c3p0
  • 105
  • 2
  • 11
  • If anyone else runs into this: This worked for me when I returned labels in a list, even though I'm still not sure why it didn't work in the first place. – c3p0 Mar 09 '19 at 04:35

1 Answers1

0
{
   "features": ["1","2"],
   "labels": "2"

}

I don't see your error when I execute this code.

def gen():
    with open('jsondataset') as f:
        data = json.load(f)
        features = data['features']
        labels = data['labels']
        print( features)
        yield features, labels

def get_dataset():
     generator = lambda: gen()
     return tf.data.Dataset.from_generator(generator, (tf.float32, tf.float32))

def get_input():
     dataset = get_dataset()
     dataset = dataset.shuffle(buffer_size=5)
     dataset = dataset.batch(5, drop_remainder=False)

     # This is where the problem is
     iter = dataset.make_one_shot_iterator()
     features, labels = iter.get_next()

     with tf.Session() as sess:
         print(sess.run([features,labels]))


def main():
    get_input()

if __name__ == "__main__":
    main()

[array([[1., 2.]], dtype=float32), array([2.], dtype=float32)]

Mohan Radhakrishnan
  • 2,410
  • 4
  • 19
  • 36