Я попробовал следующий пример:
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: Ошибка при проверке ввода: ожидалось, что dens_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: Ошибка при проверке цели: ожидается, что плотность_2 имеет 4 измерения, но получен массив с формой (30, 1)
Есть идеи о том, что я делаю не так?