问题
I have programmed a multi-layer neural network but I'm getting an error while feeding my dimension into it. I'm getting a Value Error.
Here is The Code:
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn import metrics
from sklearn import model_selection
from sklearn import preprocessing
# In[207]:
df =pd.read_csv("train_data.csv")
# In[252]:
target = df["target"]
feat=df.drop(['target','connection_id'],axis=1)
target[189]
# In[209]:
len(feature.columns)
# In[210]:
logs_path="Server_attack"
# In[211]:
#Hyperparameters
batch_size=100
learning_rate=0.5
training_epochs=10
# In[244]:
X=tf.placeholder(tf.float32,[None,41])
Y_=tf.placeholder(tf.float32,[None,3])
lr=tf.placeholder(tf.float32)
# In[245]:
#5Layer Neural Network
L=200
M=100
N=60
O=30
# In[257]:
#Weights and Biases
W1=tf.Variable(tf.truncated_normal([41,L],stddev=0.1))
B1=tf.Variable(tf.ones([L]))
W2=tf.Variable(tf.truncated_normal([L,M],stddev=0.1))
B2=tf.Variable(tf.ones([M]))
W3=tf.Variable(tf.truncated_normal([M,N],stddev=0.1))
B3=tf.Variable(tf.ones([N]))
W4=tf.Variable(tf.truncated_normal([N,O],stddev=0.1))
B4=tf.Variable(tf.ones([O]))
W5=tf.Variable(tf.truncated_normal([O,3],stddev=0.1))
B5=tf.Variable(tf.ones([3]))
# In[247]:
Y1=tf.nn.relu(tf.matmul(X,W1)+B1)
Y2=tf.nn.relu(tf.matmul(Y1,W2)+B2)
Y3=tf.nn.relu(tf.matmul(Y2,W3)+B3)
Y4=tf.nn.relu(tf.matmul(Y3,W4)+B4)
Ylogits=tf.nn.relu(tf.matmul(Y4,W5)+B5)
Y=tf.nn.softmax(Ylogits)
# In[216]:
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Ylogits,labels=Y_)
cross_entropy = tf.reduce_mean(cross_entropy)
# In[217]:
correct_prediction=tf.equal(tf.argmax(Y,1),tf.argmax(Y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
# In[218]:
train_step=tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
# In[219]:
#TensorBoard Parameters
tf.summary.scalar("cost",cross_entropy)
tf.summary.scalar("accuracy",accuracy)
summary_op=tf.summary.merge_all()
# In[220]:
init = tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
# In[253]:
with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter(logs_path,graph=tf.get_default_graph())
for epoch in range(training_epochs):
batch_count=int(len(feature)/batch_size)
for i in range(batch_count):
batch_x,batch_y=feature.iloc[i, :].values.tolist(),target[i]
_,summary = sess.run([train_step,summary_op],
{X:batch_x,Y:batch_y,learning_rate:0.001}
)
I'm getting the following error:
ValueError: Cannot feed value of shape (41,) for Tensor 'Placeholder_24:0', which has shape '(?, 41)'
I need to reshape I guess.
回答1:
You're right, you just have to reshape your input values in order to make them compatible with the placeholder's shape.
Your placeholder has shape (?,41) that means any batch size, with 41 values. Your input is, instead, with a shape of 41.
It's clear that the batch dimension is missing. Just add a 1 dimension to your input and you'll be fine:
batch_x = np.expand_dims(np.array(feature.iloc[i, :].values.tolist()), axis=0)
Note that probably you have to add a 1 dimension to your batch_y variable too. (for the same reason described above)
回答2:
Your data format is incompatible with the placeholder defined as
X=tf.placeholder(tf.float32,[None,41])
It is probably easier to reformat your data which you feed during training/evaluation. I don't see where you import it but you are going to need to either reshape or swap axes so that it has format (index, 41) and not (41, index)
来源:https://stackoverflow.com/questions/47062932/value-error-while-feeding-in-neural-network