TypeError: Argument `fetch` = None has invalid type "NoneType". Cannot be None when calling Session.run() method

Viewed 32

I'm following an online course where the goal is to build a neural network to recognise hand digits, but it's all done with an old version of Tensorflow and I don't know how to do all of this with the latest version instead, so my code looks like

# imports
import tensorflow.compat.v1 as tf1
tf.compat.v1.disable_eager_execution()
tf1.disable_v2_behavior()

# creating the session
sess = tf1.Session()

# Creating the summaries
tf.summary.scalar('accuracy', accuracy)
tf.summary.scalar('cost', loss)

# Setup FileWriter and Merge Summaries
merged_summary = tf1.summary.merge_all()

train_writer = tf1.summary.FileWriter(directory + '/train')
train_writer.add_graph(sess.graph)

# Initialise all the variables
init = tf1.global_variables_initializer()
sess.run(init)

# Batching the Data
size_of_batch = 1000

num_examples = y_train.shape[0]
nr_iterations = int(num_examples/size_of_batch)

index_in_epoch = 0

def next_batch(batch_size, data, labels):
    
    global num_examples
    global index_in_epoch
    
    start = index_in_epoch
    index_in_epoch += batch_size
    
    if index_in_epoch > num_examples:
        start = 0
        index_in_epoch = batch_size
    
    end = index_in_epoch
    
    return data[start:end], labels[start:end]

# Training Loop
for epoch in range(nr_epochs):
    
    for i in range(nr_iterations):
        
        batch_x, batch_y = next_batch(batch_size=size_of_batch, data=x_train, labels=y_train)
        
        # setting up calculations, variables calculations:
        feed_dictionary = {X:batch_x, Y:batch_y}
        # we create this so that we can feed it to our session, which is gonna do the calculations for us
        # the calculation is the training step
        
        # running the calculations, the training step, the optimizer
        sess.run(train_step, feed_dict=feed_dictionary)
        
    s, batch_accuracy = sess.run(fetches=[merged_summary, accuracy], feed_dict=feed_dictionary)
    # fetches are when we ask our session to return some calculation for us
    
    train_writer.add_summary(s, epoch)
        
    print(f'Epoch {epoch} \t| Training Accuracy = {batch_accuracy}')
    
print('Done training!')

But in the end I get the error

TypeError: Argument `fetch` = None has invalid type "NoneType". Cannot be None

And actually I noticed that printing [merged_summary, accuracy] shows

[None, <tf.Tensor 'Mean_3:0' shape=() dtype=float32>]

But I wouldn't know what else to do.

0 Answers
Related