Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 误差是神经网络的输出层形状,can';I don’我不能找出尺寸上的错误_Python_Numpy_Keras_Deep Learning - Fatal编程技术网

Python 误差是神经网络的输出层形状,can';I don’我不能找出尺寸上的错误

Python 误差是神经网络的输出层形状,can';I don’我不能找出尺寸上的错误,python,numpy,keras,deep-learning,Python,Numpy,Keras,Deep Learning,我从神经网络开始。我建立了一个模型,并在我的训练数据集上训练它。但是,当我尝试评估模型时,我得到了一个维度不匹配错误:ValueError:检查目标时出错:预期稠密_16具有形状(3),但得到了具有形状(1)的数组。。就我个人而言,我似乎无法找出它在模型定义中的起源。感谢您的帮助 模型定义: 培训: 评估(错误): 代码: # Building a model - build a simple feedforward neural network for this problem. #

我从神经网络开始。我建立了一个模型,并在我的训练数据集上训练它。但是,当我尝试评估模型时,我得到了一个维度不匹配错误:
ValueError:检查目标时出错:预期稠密_16具有形状(3),但得到了具有形状(1)的数组。
。就我个人而言,我似乎无法找出它在模型定义中的起源。感谢您的帮助

模型定义:

培训:

评估(错误):

代码:

# Building a model - build a simple feedforward neural network for this problem.

# Specify all the parameters we will be using in our network
input_num_units = (32, 32, 3)
hidden_num_units = 1000
output_num_units = 3

epochs = 10
batch_size = 128

from keras.models import Sequential
from keras.layers import Dense, Flatten, InputLayer



# Define the network/model
model = Sequential([
  InputLayer(input_shape=input_num_units),
  Flatten(),
  Dense(units=hidden_num_units, activation='relu'),
  Dense(units=output_num_units, activation='softmax'),
])



model.summary()


# Compile our network and let it train for a while, with cross validation

model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_x, train_y, batch_size=batch_size,epochs=epochs,verbose=1, validation_split=0.2)


score = model.evaluate(test_x, test_y, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

我可能错了,但是,您是否也曾经对您试图评估的测试数据进行过分类

y_test = to_categorical(y_test,num_classes)

我认为您应该检查test_x的形状。最重要的一点是缺少:什么是
train_x
test_x
?我刚刚运行了一个旧模型,但缺少了这一部分-并且得到了一个非常类似的消息-检查目标时出错:预期密集型_2具有形状(10),但获得了具有形状(1)的数组num_类可能是3个-我在本例中使用了10个类