【发布时间】:2020-01-19 17:06:43
【问题描述】:
我正在尝试将旧的 tflearn 模型转换为 keras 模型,因为我已从 TF 1.15 迁移到 TF 2.0,其中不再支持 tflearn。 我的 keras 模型是:
model = tf.keras.Sequential([
tf.keras.layers.Dense(8, input_shape=(None, len(train_x[0]))),
tf.keras.layers.Dense(8),
tf.keras.layers.Dense(len(train_y[0]), activation="softmax"),
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size)
test_loss, test_acc = model.evaluate(train_x, train_y)
print("Tested Acc:", test_acc)
当我运行它时,我收到以下错误:
ValueError:检查输入时出错:预期的 dense_input 为 3 尺寸,但得到形状为 (49, 51) 的数组
我不知道如何解决这个错误。我是否需要以某种方式重新调整模型的尺寸?我做错了什么?
作为参考,我的旧 tflearn 模型是:
tf.reset_default_graph()
net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
net = tflearn.regression(net)
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
model.fit(train_x, train_y, n_epoch=epochs, batch_size=batch_size, show_metric=True)
【问题讨论】:
-
input_shape参数中不需要给出批次维度。见the Keras documentation。 -
很高兴知道。我已经删除了它们,但显然它们并没有解决我的主要问题,valueerror
-
这应该相应地减少了输入形状。你能给出
mode.summary()的输出吗? -
如果我注释掉 model.compile 和 model.fit 并执行 model.summary(),我得到: Model: "sequential" Layer (type) Output Shape Param #dense (Dense) ( None, None, 8) 416 dense_1 (Dense) (None, None, 8) 72 dense_2 (Dense) (None, None, 6) 54 总参数:542 可训练参数:542 不可训练参数:0
-
那么第一个 Dense 层的输入形状是否符合您的预期以及
train_x中数据的形状?
标签: python tensorflow keras