问题描述
我已经编写了一个多层神经网络,但是在向其输入尺寸时遇到错误.我遇到了价值错误.
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.
这是代码:
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}
)
我遇到以下错误:
ValueError: Cannot feed value of shape (41,) for Tensor 'Placeholder_24:0', which has shape '(?, 41)'
我想我需要重塑.
推荐答案
您是对的,您只需要重塑输入值即可使其与占位符的形状兼容.
You're right, you just have to reshape your input values in order to make them compatible with the placeholder's shape.
您的占位符的形状为(?,41)
,表示任何批量大小,具有41个值.您输入的形状为41
.
Your placeholder has shape (?,41)
that means any batch size, with 41 values. Your input is, instead, with a shape of 41
.
很明显,批次尺寸缺失.只需在输入中添加1维,就可以了:
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)
请注意,您可能还必须在batch_y
变量中添加1维. (出于上述相同的原因)
Note that probably you have to add a 1 dimension to your batch_y
variable too. (for the same reason described above)
这篇关于馈入神经网络时出现值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!