0

I am trying to build a basic network,

    # Suppress OS related warnings
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'


# Import tensorflow library
import tensorflow as tf
import numpy as np

sess = tf.Session()


# Input Data X : of placeholder Value 1.0 tf.float32
x = tf.placeholder(tf.float32, name="input")

# Variable Weight : Arbitary Value
w = tf.Variable(0.8, name='weight')

# Neuron : y = w * x
with tf.name_scope('operation'):
    y = tf.multiply(w, x, name='output')

# Actual Output
actual_output = tf.constant(0.0, name="actual_output")

# Loss function , delta square
with tf.name_scope("loss"):
    loss = tf.pow(y - actual_output, 2, name='loss')

# Training Step : Algorithm -> GradientDescentOptimizer
with tf.name_scope("training"):
    train_step = tf.train.GradientDescentOptimizer(0.025).minimize(loss)

# Ploting graph : Tensorboard
for value in [x, w, y, actual_output, loss]:
    tf.summary.scalar(value.op.name, value)

# Merging all summaries : Tensorboard
summaries = tf.summary.merge_all()

# Printing the graph : Tensorboard
summary_writer = tf.summary.FileWriter('log_simple_stats', sess.graph)

# Initialize all variables
sess.run(tf.global_variables_initializer())

for i in range(300):
    summary_writer.add_summary(sess.run(summaries), i)
    sample = np.random.uniform(low=0.0, high=400.0)
    print(sample)
    sess.run(train_step, feed_dict={x: sample})

# Output
print(sess.run([w]))

And the error is

You must feed a value for placeholder tensor 'input' with dtype float [[Node: input = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

1 Answer 1

1

The key for the feed dict should be the placeholder itself, not a string. Another problem is that you are not using the same feed data when you are running summaries as when you are training.

x = tf.placeholder(tf.float32, name="input")
... more code...
_, merged = sess.run([train_step, summaries], feed_dict={x: sample})
summary_writer.add_summary(merged, i)
Sign up to request clarification or add additional context in comments.

8 Comments

Just ran your code. Your other problem is that you are not feeding the same data when you are running summaries. Lemme edit the answer to include that.
Great solved that , but code is not working as expected, do you have a working code , Can you post it ?
I am getting "w" value as "nan"
No, all I did was fix the errors. Let me see if I can provide some additional insight.
Your learning rate is waaaaaaaay too high. If you run and print your loss or your output, you will see that instead of converging your network diverges very very quickly, going beyond the type range (hence NaN values everywhere). I got it to converge on my laptop by setting learning rate to 0.00001.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.