我在使用Tensorflow Python时遇到问题。 我尝试训练一个模型(功能),我可以训练,但随后我预测到一个文件,但出现此错误:
ValueError:无法将输入数组从形状(24946,1)广播到形状(1,1)
我尝试在我的名为(prepare)的函数内部的代码中在model.fit下调用一个函数。我不明白该错误消息。
这是我的代码:
In of len(x) = 24946
def prepare(file):
img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(1, 50, 50)
pickle_ind = open("x.pickle", "rb")
x = pickle.load(pickle_ind)
x = np.array(x, dtype=float)
print(len(x))
pickle_ind = open("y.pickle", "rb")
y = pickle.load(pickle_ind)
##model
model = tf.keras.models.Sequential()
n_batch = len(x)
n_batchadd = len(xadd)
# design network
model = Sequential()
print(str(n_batch) + "__" + str(x.shape[1]) + "__" + str(x.shape[2]))
model.add(LSTM(1, batch_input_shape=(n_batch, x.shape[1], x.shape[2]), stateful=True, input_shape=(50, 2)))
model.add(Dense(1))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
history = model.fit(x, y, epochs=1, batch_size=n_batch)
predirction = model.predict([prepare('demo2.jpg')], batch_size=n_batch)
print(predirction)