【发布时间】:2019-06-04 16:09:45
【问题描述】:
我尝试了以下示例:
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:检查输入时出错:预期 dense_1_input 具有 2 维,但得到的数组形状为 (30, 50, 50, 3)。
因此,我将 input_shape 更改如下:
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)
但现在我得到了这个错误:
ValueError: 检查目标时出错:预期 dense_2 有 4 个维度,但得到的数组形状为 (30, 1)
知道我做错了什么吗?
【问题讨论】:
标签: keras