如何使用优化后的权重训练模型?

我正在进行模型参数的优化工作。大体上,我已经训练了一个具有特定结构的模型的权重。现在我使用这些权重来训练另一个结构完全相同的模型。但我在第一轮训练中无法获得同样的准确率,甚至相差甚远。

以下代码片段解释了整个问题。

from __future__ import print_functionfrom keras.preprocessing import sequencefrom keras.models import Sequentialfrom keras.layers import Dense, Dropout, Activationfrom keras.layers import Embeddingfrom keras.layers import Conv1D, GlobalMaxPooling1Dfrom keras.datasets import imdb# set parameters:max_features = 5000maxlen = 400batch_size = 32embedding_dims = 50filters = 250kernel_size = 3hidden_dims = 250print('Loading data...')(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)print(len(x_train), 'train sequences')print(len(x_test), 'test sequences')print('Pad sequences (samples x time)')x_train = sequence.pad_sequences(x_train, maxlen=maxlen)x_test = sequence.pad_sequences(x_test, maxlen=maxlen)print('x_train shape:', x_train.shape)print('x_test shape:', x_test.shape)print('Build model...')model = Sequential()# we start off with an efficient embedding layer which maps# our vocab indices into embedding_dims dimensionsmodel.add(Embedding(max_features,                    embedding_dims,                    input_length=maxlen))model.add(Dropout(0.2))# we add a Convolution1D, which will learn filters# word group filters of size filter_length:model.add(Conv1D(filters, kernel_size, padding='valid',    activation='relu', strides=1))# we use max pooling:model.add(GlobalMaxPooling1D())# We add a vanilla hidden layer:model.add(Dense(hidden_dims))model.add(Dropout(0.2))model.add(Activation('relu'))# We project onto a single unit output layer, and squash it with a sigmoid:model.add(Dense(1))model.add(Activation('sigmoid'))model.compile(loss='mse',Optimizer='sgd', metrics=['accuracy'])model.fit(x_train, y_train,batch_size=batch_size,epochs=50, validation_data=(x_test, y_test))# Extraction of weightscon_weight=model.layers[2].get_weights()[0]con_bias=model.layers[2].get_weights()[1]mid_weight=model.layers[4].get_weights()[0]mid_bias=model.layers[4].get_weights()[1]pl_weight=model.layers[7].get_weights()[0]pl_bias=model.layers[7].get_weights()[1]

使用权重训练另一个模型

model1 = Sequential()# we start off with an efficient embedding layer which maps# our vocab indices into embedding_dims dimensionsmodel1.add(Embedding(max_features,                    embedding_dims,                    input_length=maxlen))model1.add(Dropout(0.2))# we add a Convolution1D, which will learn filters# word group filters of size filter_length:model1.add(Conv1D(filters, kernel_size, padding='valid',activation='relu', strides=1,weights=[con_weight,con_bias]))# we use max pooling:model1.add(GlobalMaxPooling1D())# We add a vanilla hidden layer:model1.add(Dense(hidden_dims,weights=[mid_weight,mid_bias]))model1.add(Dropout(0.2))model1.add(Activation('relu'))# We project onto a single unit output layer, and squash it with a sigmoid:model1.add(Dense(1,weights=[pl_weight,pl_bias]))model1.add(Activation('sigmoid'))model1.trainable=Falsemodel1.compile(loss='mse', optimizer='sgd', metrics=['accuracy'])model1.fit(x_train, y_train, batch_size=batch_size, epochs=1,validation_data=(x_test, y_test))

回答:

嵌入层确实有权重,而你忘记了它们。

但你的模型是相同的,只需:

model1.set_weights(model.get_weights())

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注