QuarterShotofEspresso
QuarterShotofEspresso

Reputation: 143

Why is tensorflow not initializing variables?

At the very start of declaring the Session with tf.Session(), I declare both tf.global_variables_initializer and tf.local_variables_initializer functions and unfortunately, keep receiving error messages detailing the use of "Uninitialized value Variable_1." Why?

I did some searching around and found this StackExchange Question, but the answer doesn't help my situation. So I looked through the TensorFlow API and found an operation that should return any uninitialized variables, tf.report_uninitialized_variables(). I printed the results and received an empty pair of square brackets, which doesn't make any sense considering the description of my error messages. So what's going on? I've been clawing my eyes out for a day now. Any help is appreciated.

import tensorflow as tf
import os
from tqdm import tqdm

#hyperparam
training_iterations = 100
PATH = "C:\\Users\\ratno\\Desktop\\honest chaos\\skin cam\\drive-download-20180205T055458Z-001"

#==================================import training_data=================================
def import_data(image_path):
    image_contents = tf.read_file(filename=image_path)
    modified_image = tf.image.decode_jpeg(contents=image_contents, channels=1)
    image_tensor = tf.cast(tf.reshape(modified_image, [1, 10000]), dtype=tf.float32)
    return image_tensor

#========================neural network================================
def neural_network(input_layer):

    Weight_net_1 = {'weights': tf.Variable(tf.random_normal(shape=(10000, 16))),
                'bias': tf.Variable(tf.random_normal(shape=(1, 1)))}
    Weight_net_2 = {'weights': tf.Variable(tf.random_normal(shape=(16, 16))),
                'bias': tf.Variable(tf.random_normal(shape=(1, 1)))}
    Weight_net_3 = {'weights': tf.Variable(tf.random_normal(shape=(16, 16))),
                'bias': tf.Variable(tf.random_normal(shape=(1, 1)))}
    Weight_net_4 = {'weights': tf.Variable(tf.random_normal(shape=(16, 1))),
                'bias': tf.Variable(tf.random_normal(shape=(1, 1)))}
    #Input Layer
    hypothesis = input_layer; x = hypothesis

    #Hidden Layer 1
    hypothesis = tf.nn.relu(tf.matmul(x, Weight_net_1['weights']) + Weight_net_1['bias']); x = hypothesis

    #Hidden Layer 2
    hypothesis = tf.nn.relu(tf.matmul(x, Weight_net_2['weights']) +     Weight_net_2['bias']); x = hypothesis

    #Hidden Layer 3
    hypothesis = tf.nn.relu(tf.matmul(x, Weight_net_3['weights']) + Weight_net_3['bias']); x = hypothesis

    # output cell
    hypothesis = tf.nn.relu(tf.matmul(x, Weight_net_4['weights']) + Weight_net_4['bias'])

    return hypothesis

#============================training the network=========================
def train(hypothesis):
    LOSS = tf.reduce_sum(1 - hypothesis)
    tf.train.AdamOptimizer(0.01).minimize(LOSS)

#Session==================================================================

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())

    image_list = [os.path.join(PATH, file_name) for file_name in os.listdir(PATH)]

    for iteration in tqdm(range(training_iterations), desc="COMPLETION", ncols=80):
        for i in image_list:
            modified_image_tensor = sess.run(import_data(image_path=i))
            hypo = sess.run(neural_network(input_layer=modified_image_tensor))
            sess.run(train(hypothesis=hypo))

    print("\n\nTraining completed.\nRunning test prediction.\n")
    DIRECTORY = input("Directory: ")
    test_input = sess.run(import_data(DIRECTORY))
    prediction = sess.run(neural_network(input_layer=test_input))
    print(prediction)

    if prediction >= 0.5:
        print ("Acne")
    else:
        print ("What")

And as for the error message:

Caused by op 'Variable/read', defined at:
  File "C:/Users/ratno/Desktop/honest chaos/Hotdog/HDogntoHDog.py", line 75, in <module>
    hypo = sess.run(neural_network(input_layer=modified_image_tensor))
  File "C:/Users/ratno/Desktop/honest chaos/Hotdog/HDogntoHDog.py", line 23, in neural_network
    Weight_net_1 = {'weights': tf.Variable(tf.random_normal(shape=(10000, 16))),
  File "C:\Users\ratno\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\variables.py", line 199, in __init__
expected_shape=expected_shape)
  File "C:\Users\ratno\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\variables.py", line 330, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
  File "C:\Users\ratno\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1400, in identity
    result = _op_def_lib.apply_op("Identity", input=input, name=name)
  File "C:\Users\ratno\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
op_def=op_def)
  File "C:\Users\ratno\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
  File "C:\Users\ratno\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\framework\ops.py", line 1204, in __init__
self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable
     [[Node: Variable/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]]

Upvotes: 0

Views: 624

Answers (1)

mikkola
mikkola

Reputation: 3476

Let's take a look at your main function, starting from with tf.Session() as sess:. This will be the first line executed when you run your program. The next thing that happens is that you are calling the variables_initializers -- but, you have not yet declared any variables! This is because you have not called any of the other functions you have defed. So this is why, when you then call, e.g., neural_network inside a sess.run call, it will create the (uninitialized) variables as neural_networkis called, and then attempt to use them for sess.run. Obviously this will not work since you have not initialized these newly-created variables.

You have to create your network and all necessary variables in the computational graph before calling the initializers. You could try something along these lines:

data = import_data(image_path)
out = neural_network(data)
tr = train(hypothesis=out)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

By the way, your function train also has no return value so it is unlikely it will work as you are expecting it to. Please re-read the tutorials for tensorflow to understand how to operate an optimizer.

Upvotes: 1

Related Questions