【问题标题】:Incremental learning in keraskeras 中的增量学习
【发布时间】:2020-09-15 17:53:37
【问题描述】:

我正在寻找与 scikit-learn 的 partial_fit : https://scikit-learn.org/0.15/modules/scaling_strategies.html#incremental-learning 等效的 keras 用于增量/在线学习。

我终于找到了train_on_batch 方法,但我找不到一个示例来说明如何在 for 循环中正确地为如下所示的数据集实现它:

x = np.array([[0.5, 0.7, 0.8]])  # input data
y = np.array([[0.4, 0.6, 0.33, 0.77, 0.88, 0.71]])  # output data

注意:这是一个多输出回归

到目前为止我的代码:

import keras
import numpy as np

x = np.array([0.5, 0.7, 0.8])
y = np.array([0.4, 0.6, 0.33, 0.77, 0.88, 0.71])
in_dim = x.shape
out_dim = y.shape

model = Sequential()
model.add(Dense(100, input_shape=(1,3), activation="relu"))
model.add(Dense(32, activation="relu"))
model.add(Dense(6))
model.compile(loss="mse", optimizer="adam")

model.train_on_batch(x,y)

我收到此错误: ValueError: 层序 28 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到形状为 [3, 1] 的输入

【问题讨论】:

  • 什么是s和a?
  • 对不起 x 和 y 我改了

标签: python-3.x keras online-machine-learning


【解决方案1】:

您应该分批提供数据。您正在提供单个实例,但模型需要批处理数据。因此,您需要扩展批量大小的输入维度。

import keras
import numpy as np
from keras.models import *
from keras.layers import *
from keras.optimizers import *
x = np.array([0.5, 0.7, 0.8])
y = np.array([0.4, 0.6, 0.33, 0.77, 0.88, 0.71])
x = np.expand_dims(x, axis=0)
y = np.expand_dims(y, axis=0)
# x= np.squeeze(x)
in_dim = x.shape
out_dim = y.shape

model = Sequential()
model.add(Dense(100, input_shape=((1,3)), activation="relu"))
model.add(Dense(32, activation="relu"))
model.add(Dense(6))
model.compile(loss="mse", optimizer="adam")

model.train_on_batch(x,y)

【讨论】: