keras中的增量学习 [英] Incremental learning in keras
问题描述
我正在寻找与scikit-learn的partial_fit
等效的keras:
I am looking for a keras equivalent of scikit-learn's partial_fit
: https://scikit-learn.org/0.15/modules/scaling_strategies.html#incremental-learning for incremental/online learning.
我终于找到了train_on_batch
方法,但是找不到一个示例,该示例显示了如何针对这样的数据集在for循环中正确实现它:
I finally found the train_on_batch
method but I can't find an example that shows how to properly implement it in a for loop for a dataset that looks like this :
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
注意:这是一个多输出回归
Note : this is a multi-output regression
到目前为止我的代码:
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:sequence_28层的输入0与该层不兼容:预期输入形状的轴-1具有值3,但接收到形状为[3,1]的输入
I get this Error: ValueError: Input 0 of layer sequential_28 is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape [3, 1]
推荐答案
您应该分批提供数据.您正在提供一个实例,但模型需要批处理数据.因此,您需要扩展输入尺寸以获取批次大小.
You should feed your data batch-wise. You are giving a single instance but model expecting batch data. So, you need to expand the input dimension for batch size.
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)
这篇关于keras中的增量学习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!