TensorFlow sess.run(init)需要一个feed_dict

时间:2017-08-23 03:29:31

标签: python tensorflow

我正在尝试使用tensorflow创建一个带有一个隐藏层的简单多层感知器。当我尝试运行初始化程序时,我得到一个错误,它预期带有占位符的feed_dict。通常我不需要将任何内容提供给sess.run(init) 这是我的代码:

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

ins = int(input('number of inputs: '))
hiddens = int(input('number of hiddens: '))
outs = int(input('number of outs: '))

learning_rate = .01

x_ = tf.placeholder(tf.float32,shape=[None,ins],name='inputs_placeholder')
y_ = tf.placeholder(tf.float32,shape=[None,outs],name='outputs_placeholder')

W1 = tf.Variable(tf.random_uniform([ins,hiddens],-.5,.5),name='weights_to_hidden')
B1 = tf.Variable(tf.random_uniform([hiddens],-.5,.5),name='bias_to_hidden')
z = tf.sigmoid(tf.matmul(x_,W1) + B1)

W2 = tf.Variable(tf.random_uniform([hiddens,outs],-.5,.5),name='weights_to_out')
B2 = tf.Variable(tf.random_uniform([outs],-.5,.5),name='bias_to_out')
y = tf.sigmoid(tf.matmul(z,W2) + B2)

cost = tf.reduce_mean(( (y_ * tf.log(y)) + ((1 - y_) * tf.log(1.0 - y)) ) * -1)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

我为输入,隐藏和输出输入2,2,然后输入1。当我运行它时,我明白了:

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

为什么它需要feed_dict,为什么它认为有'inputs_placeholder_1'占位符?

0 个答案:

没有答案