##1.问题
程序报错,提示:Invalid argument: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,24]
##2.代码
import time
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
# import dataset
input_Dir = 'E:/data/input_H.csv'
output_Dir = 'E:/data/output_H.csv'
x_data = pd.read_csv(input_Dir, header = None)
y_data = pd.read_csv(output_Dir, header = None)
x_data = x_data.values
y_data = y_data.values
x_data = x_data.astype('float32')
y_data = y_data.astype('float32')
print("DATASET READY")
#
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.2, random_state=1)
row, column = x_train.shape
row = float(row)
# define structure of neural network
n_hidden_1 = 250
n_hidden_2 = 128
n_input = 250
n_classes = 24
#initialize parameters
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32)
stddev = 0.1
weights = {
'w1': tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=stddev)),
'w2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev=stddev)),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes], stddev=stddev))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1], stddev=stddev)),
'b2': tf.Variable(tf.random_normal([n_hidden_2], stddev=stddev)),
'out': tf.Variable(tf.random_normal([n_classes], stddev=stddev))
}
print("NETWORK READY")
# forward propagation
def multilayer_perceptron(_X, _weights, _biases):
layer_1 = tf.nn.leaky_relu(tf.add(tf.matmul(_X, _weights['w1']), _biases['b1']))
layer_2 = tf.nn.leaky_relu(tf.add(tf.matmul(layer_1, _weights['w2']), _biases['b2']))
return (tf.add(tf.matmul(layer_2, _weights['out']), _biases['out']))
#
pred = multilayer_perceptron(x, weights, biases)
cost = tf.reduce_mean(tf.square(y - pred))
optm = tf.train.GradientDescentOptimizer(learning_rate=0.03).minimize(cost)
init = tf.global_variables_initializer()
print("FUNCTIONS READY")
n_epochs = 100000
batch_size = 512
n_batches = np.int(np.ceil(row / batch_size))
def fetch_batch(epoch, batch_index, batch_size):
# 随机获取小批量数据
np.random.seed(epoch * n_batches + batch_index)
indices = np.random.randint(row, size = batch_size)
return x_train[indices], y_train[indices]
iter = 10000
sess = tf.Session()
sess.run(tf.global_variables_initializer())
feeds_test = {x: x_test, y: y_test, keep_prob: 1}
for epoch in range(n_epochs): # 总共循环次数
for batch_index in range(n_batches):
x_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
feeds_train = {x: x_batch, y: y_batch, keep_prob: 1}
sess.run(optm, feed_dict=feeds_train)
print("EPOCH %d HAS FINISHED" % (epoch))
print("COST %d :" % (epoch))
print(sess.run(cost),feed_dict=feeds_train)
print("\n")
sess.close()
print("FINISHED")
##3.报错信息
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1356, in _do_call
return fn(*args)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1341, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1429, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,24]
[[{{node Placeholder_1}}]]
[Mean/_7] Invalid argument: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,24]
[[{{node Placeholder_1}}]]
0 successful operations.
0 derived errors ignored.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
runfile('C:/Users/Administrator/Desktop/main/demo3.py', wdir='C:/Users/Administrator/Desktop/main')
File "E:\Program Files\PyCharm 2019.1.3\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile
#求问问题出在什么地方?