ValueError:检查输入时出错:预期density_1_input具有2维 [英] ValueError: Error when checking input: expected dense_1_input to have 2 dimensions

查看:277
本文介绍了ValueError:检查输入时出错:预期density_1_input具有2维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下示例:

from keras.models import Sequential  
from keras.layers import *  
import numpy as np

x_train = np.random.random((30,50,50,3))
y_train = np.random.randint(2, size=(30,1))

model = Sequential()    

#start from the first hidden layer, since the input is not         actually a layer   
#but inform the shape of the input, with 3 elements.    
model.add(Dense(units=4,input_shape=(3,))) #hidden layer 1    with input

#further layers:    
model.add(Dense(units=4)) #hidden layer 2
model.add(Dense(units=1)) #output layer

model.compile(loss='binary_crossentropy',
           optimizer='adam',
           metrics=['accuracy'])

model.fit(x_train, y_train,
       epochs=20,
       batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)

我收到此错误:

ValueError:检查输入时出错:预期density_1_input具有2维,但数组的形状为(30,50,50,3).

ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (30, 50, 50, 3).

因此,我将input_shape更改如下:

Thus, I changed the input_shape as follows:

from keras.models import Sequential  
from keras.layers import *  
import numpy as np

x_train = np.random.random((30,50,50,3))
y_train = np.random.randint(2, size=(30,1))

model = Sequential()    

#start from the first hidden layer, since the input is not         actually a layer   
#but inform the shape of the input, with 3 elements.    
model.add(Dense(units=4,input_shape=(50,50,3))) #hidden layer 1    with input

#further layers:    
model.add(Dense(units=4)) #hidden layer 2
model.add(Dense(units=1)) #output layer

model.compile(loss='binary_crossentropy',
           optimizer='adam',
           metrics=['accuracy'])

model.fit(x_train, y_train,
       epochs=20,
       batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)

但是现在我得到了这个错误:

But now I get this error:

ValueError:检查目标时出错:预期density_2具有4维,但数组的形状为(30,1)

ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (30, 1)

关于我在做什么错的任何想法吗?

Any idea about what am I doing wrong?

推荐答案

问题在于最后一个致密层的输出形状.您可以使用 model.summary()来查看每个图层的输出形状.

the problem is with the output shape of the last dense layer. You can use model.summary() to see the output shape of each layer.

您的输出形状为(None,50,50,1),但要与您的y_train匹配 形状应为(None,1).

your output shape is (None,50,50,1),but to match with your y_train shape it should be in (None,1) shape.

所以我建议您在最后一个致密层之前添加一个 flattern层.请参考此

So i suggest you to add a flattern layer before the last dense layer.Plese refer this link for the definition of flattern layer in keras.

这是您的模型代码的外观

This is how your model code should looks like

model.add(Dense(units=4,input_shape=(50,50,3),name="d1")) #hidden layer 1    with input  
model.add(Dense(units=4,name="d2")) #hidden layer 2
model.add(Flatten())
model.add(Dense(units=1,name="d3")) #output layer

model.compile(loss='binary_crossentropy',
           optimizer='adam',
           metrics=['accuracy'])

model.summary()

为您的图层添加更多的使用名称,您将很容易理解问题所在.祝您好运;-)

Futher more use name for your layers it will be easy for you to understand where the problem is.good luck ;-)

这篇关于ValueError:检查输入时出错:预期density_1_input具有2维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆