我使用下面的tflearn github存储库中的示例,我保存了它,并且我想用不同的优化器重新加载模型。请帮忙。感谢。
init2=False
from __future__ import division, print_function, absolute_import
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
X1=sanit_tr
Y1=sanit_trlabels
testX=sanit_te
testY=sanit_telabels
# valid_dataset2=sanit_va
# valid_labels2=sanit_valabels
network = input_data(shape=[None, 28, 28, 1], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='Adagrad', learning_rate=0.1,
loss='categorical_crossentropy', name='target')
model = tflearn.DNN(network, tensorboard_verbose=0)
if not init2:
model.load('tflearn_model')
model.fit({'input': X1}, {'target': Y1}, n_epoch=2,
validation_set=({'input': testX}, {'target': testY}),
snapshot_step=100, show_metric=True, run_id='convnet_mnist')
model.save('tflearn_model')
这是我想加载新优化器的地方:
model.load('tflearn_model')
model.fit({'input': X1}, {'target': Y1}, n_epoch=2,
validation_set=({'input': testX}, {'target': testY}),
snapshot_step=100, show_metric=True, run_id='convnet_mnist')
model.save('tflearn_model')
答案 0 :(得分:0)
我说你只想重新训练模型。您只需更改完全连接的图层,然后在加载模型之前重新定义tflearn.DNN()。整个代码是:
init2=False
from __future__ import division, print_function, absolute_import
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
X1=sanit_tr
Y1=sanit_trlabels
testX=sanit_te
testY=sanit_telabels
# valid_dataset2=sanit_va
# valid_labels2=sanit_valabels
network = input_data(shape=[None, 28, 28, 1], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='tanh')
network = dropout(network, 0.8)
end_network = fully_connected(network, 10, activation='softmax')
network = regression(end_network, optimizer='Adagrad', learning_rate=0.1,
loss='categorical_crossentropy', name='target')
model = tflearn.DNN(network, tensorboard_verbose=0)
if not init2:
model.load('tflearn_model')
model.fit({'input': X1}, {'target': Y1}, n_epoch=2,
validation_set=({'input': testX}, {'target': testY}),
snapshot_step=100, show_metric=True, run_id='convnet_mnist')
model.save('tflearn_model')
network = regression(end_network, optimizer='RMSprop', learning_rate=0.1,
loss='categorical_crossentropy', name='target')
model = tflearn.DNN(network, tensorboard_verbose=0)
model.load('tflearn_model')
model.fit({'input': X1}, {'target': Y1}, n_epoch=2,
validation_set=({'input': testX}, {'target': testY}),
snapshot_step=100, show_metric=True, run_id='convnet_mnist')
model.save('tflearn_model')