本文整理汇总了Python中keras.utils.visualize_util.plot方法的典型用法代码示例。如果您正苦于以下问题:Python visualize_util.plot方法的具体用法?Python visualize_util.plot怎么用?Python visualize_util.plot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.utils.visualize_util
的用法示例。
在下文中一共展示了visualize_util.plot方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _plot_graph
# 需要导入模块: from keras.utils import visualize_util [as 别名]
# 或者: from keras.utils.visualize_util import plot [as 别名]
def _plot_graph(self):
from keras.utils import visualize_util
graph_png = '{}/graph-plot.png'.format(self.model_folder)
visualize_util.plot(self.model,
to_file=graph_png,
show_shapes=True,
show_layer_names=True)
示例2: plot
# 需要导入模块: from keras.utils import visualize_util [as 别名]
# 或者: from keras.utils.visualize_util import plot [as 别名]
def plot(self, fn, train_fn):
"""
Plot this model to an image file
Train file is needed as it influences the dimentions of the RNN
"""
from keras.utils.visualize_util import plot
X, Y = self.load_dataset(train_fn)
self.model_fn()
plot(self.model, to_file = fn)
示例3: build
# 需要导入模块: from keras.utils import visualize_util [as 别名]
# 或者: from keras.utils.visualize_util import plot [as 别名]
def build(self):
enc_size = self.size_of_env_observation()
argument_size = IntegerArguments.size_of_arguments
input_enc = InputLayer(batch_input_shape=(self.batch_size, enc_size), name='input_enc')
input_arg = InputLayer(batch_input_shape=(self.batch_size, argument_size), name='input_arg')
input_prg = Embedding(input_dim=PROGRAM_VEC_SIZE, output_dim=PROGRAM_KEY_VEC_SIZE, input_length=1,
batch_input_shape=(self.batch_size, 1))
f_enc = Sequential(name='f_enc')
f_enc.add(Merge([input_enc, input_arg], mode='concat'))
f_enc.add(MaxoutDense(128, nb_feature=4))
self.f_enc = f_enc
program_embedding = Sequential(name='program_embedding')
program_embedding.add(input_prg)
f_enc_convert = Sequential(name='f_enc_convert')
f_enc_convert.add(f_enc)
f_enc_convert.add(RepeatVector(1))
f_lstm = Sequential(name='f_lstm')
f_lstm.add(Merge([f_enc_convert, program_embedding], mode='concat'))
f_lstm.add(LSTM(256, return_sequences=False, stateful=True, W_regularizer=l2(0.0000001)))
f_lstm.add(Activation('relu', name='relu_lstm_1'))
f_lstm.add(RepeatVector(1))
f_lstm.add(LSTM(256, return_sequences=False, stateful=True, W_regularizer=l2(0.0000001)))
f_lstm.add(Activation('relu', name='relu_lstm_2'))
# plot(f_lstm, to_file='f_lstm.png', show_shapes=True)
f_end = Sequential(name='f_end')
f_end.add(f_lstm)
f_end.add(Dense(1, W_regularizer=l2(0.001)))
f_end.add(Activation('sigmoid', name='sigmoid_end'))
f_prog = Sequential(name='f_prog')
f_prog.add(f_lstm)
f_prog.add(Dense(PROGRAM_KEY_VEC_SIZE, activation="relu"))
f_prog.add(Dense(PROGRAM_VEC_SIZE, W_regularizer=l2(0.0001)))
f_prog.add(Activation('softmax', name='softmax_prog'))
# plot(f_prog, to_file='f_prog.png', show_shapes=True)
f_args = []
for ai in range(1, IntegerArguments.max_arg_num+1):
f_arg = Sequential(name='f_arg%s' % ai)
f_arg.add(f_lstm)
f_arg.add(Dense(IntegerArguments.depth, W_regularizer=l2(0.0001)))
f_arg.add(Activation('softmax', name='softmax_arg%s' % ai))
f_args.append(f_arg)
# plot(f_arg, to_file='f_arg.png', show_shapes=True)
self.model = Model([input_enc.input, input_arg.input, input_prg.input],
[f_end.output, f_prog.output] + [fa.output for fa in f_args],
name="npi")
self.compile_model()
plot(self.model, to_file='model.png', show_shapes=True)
示例4: train
# 需要导入模块: from keras.utils import visualize_util [as 别名]
# 或者: from keras.utils.visualize_util import plot [as 别名]
def train(self):
print('Training model ...')
# load params
self.window_size = self.train_data.window_size
self.userTagIntent_vocab_size = self.train_data.userTagIntent_vocab_size
self.agentAct_vocab_size = self.train_data.agentAct_vocab_size
self.id2agentAct = self.train_data.id2agentAct
other_npz = '{}/other_vars.npz'.format(self.model_folder)
train_vars = {'window_size': self.window_size,
'userTagIntent_vocab_size': self.userTagIntent_vocab_size,
'agentAct_vocab_size': self.agentAct_vocab_size,
'id2agentAct': self.id2agentAct}
np.savez_compressed(other_npz, **train_vars)
self.params['window_size'] = self.window_size
self.params['userTagIntent_vocab_size'] = self.userTagIntent_vocab_size
self.params['agentAct_vocab_size'] = self.agentAct_vocab_size
print_params(self.params)
# build model graph, save graph and plot graph
self._build()
self._plot_graph()
graph_yaml = '{}/graph-arch.yaml'.format(self.model_folder)
with open(graph_yaml, 'w') as fyaml:
fyaml.write(self.model.to_yaml())
# load train data
X_train = self.train_data.userTagIntent_vecBin
y_train = self.train_data.agentAct_vecBin
train_utter_txt = self.train_data.userUtter_txt
train_act_txt = self.train_data.agentAct_txt
train_fname = '{}/train.target'.format(self.model_folder)
writeUtterActTxt(train_utter_txt, train_act_txt, train_fname)
# load dev data
X_dev = self.dev_data.userTagIntent_vecBin
y_dev = self.dev_data.agentAct_vecBin
dev_utter_txt = self.dev_data.userUtter_txt
dev_act_txt = self.dev_data.agentAct_txt
dev_fname = '{}/dev.target'.format(self.model_folder)
writeUtterActTxt(dev_utter_txt, dev_act_txt, dev_fname)
for ep in xrange(self.epoch_nb):
print('<Epoch {}>'.format(ep))
self.model.fit(x=X_train, y=y_train, batch_size=self.batch_size, nb_epoch=1, verbose=2)
act_probs = self.model.predict(X_dev)
precision, recall, fscore, accuracy_frame, threshold = eval_intentPredict(act_probs, y_dev)
print('ep={}, precision={:.4f}, recall={:.4f}, fscore={:.4f}, accuracy_frame={:.4f}, threshold={:.4f}'.format(ep, precision, recall, fscore, accuracy_frame, threshold))
dev_pred_txt = getActPred(act_probs, threshold, self.id2agentAct)
dev_results_fname = '{}/dev_results/dev_ep={}.pred'.format(self.model_folder, ep)
writeUtterActTxt(dev_utter_txt, dev_pred_txt, dev_results_fname)
print('Write dev results: {}'.format(dev_results_fname))
weights_fname = '{}/weights/ep={}_f1={:.4f}_frameAcc={:.4f}_th={:.4f}.h5'.format(self.model_folder, ep, fscore, accuracy_frame, threshold)
print('Saving Model: {}'.format(weights_fname))
self.model.save_weights(weights_fname, overwrite=True)