我正在尝试将我的旧tflearn模型转换为keras模型,因为我已经从Tf1.15迁移到Tf2.0,那里不再支持tflearn。我的角膜模型是:
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具有三维,但得到形状为(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)发布于 2020-01-19 19:08:21
正如我们在注释中所发现的,您的代码有两个问题。
首先,不能在input_shape参数cf中给出批处理维度。Dense。
第二,由于train_y.shape = (?, 6),您需要使用categorical_crossentropy而不是sparse_categorical_crossentropy。有一个Keras文档中的说明,它详细描述了不同之处。
以下是修正后的代码:
model = tf.keras.Sequential([
tf.keras.layers.Dense(8, input_shape=(len(train_x[0]))),
tf.keras.layers.Dense(8),
tf.keras.layers.Dense(len(train_y[0]), activation="softmax"),
])
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size)https://stackoverflow.com/questions/59812388
复制相似问题