首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将tflearn模型转换为keras

将tflearn模型转换为keras
EN

Stack Overflow用户
提问于 2020-01-19 17:06:43
回答 1查看 1.2K关注 0票数 1

我正在尝试将我的旧tflearn模型转换为keras模型,因为我已经从Tf1.15迁移到Tf2.0,那里不再支持tflearn。我的角膜模型是:

代码语言:javascript
复制
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模型是:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-19 19:08:21

正如我们在注释中所发现的,您的代码有两个问题。

首先,不能在input_shape参数cf中给出批处理维度。Dense

第二,由于train_y.shape = (?, 6),您需要使用categorical_crossentropy而不是sparse_categorical_crossentropy。有一个Keras文档中的说明,它详细描述了不同之处。

以下是修正后的代码:

代码语言:javascript
复制
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)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59812388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档