| 69 | return tf.reduce_mean(tf.cast(tf.equal(pred_idx, y_true), tf.float32), name='acc') |
| 70 | |
| 71 | def create_model(max_word_id, is_test=False): |
| 72 | GO_VALUE = max_word_id + 1 |
| 73 | network = tflearn.input_data(shape=[None, max_seq_len + max_seq_len], dtype=tf.int32, name="XY") |
| 74 | encoder_inputs = tf.slice(network, [0, 0], [-1, max_seq_len], name="enc_in") |
| 75 | encoder_inputs = tf.unpack(encoder_inputs, axis=1) |
| 76 | decoder_inputs = tf.slice(network, [0, max_seq_len], [-1, max_seq_len], name="dec_in") |
| 77 | decoder_inputs = tf.unpack(decoder_inputs, axis=1) |
| 78 | go_input = tf.mul( tf.ones_like(decoder_inputs[0], dtype=tf.int32), GO_VALUE ) |
| 79 | decoder_inputs = [go_input] + decoder_inputs[: max_max_seq_len-1] |
| 80 | num_encoder_symbols = max_word_id + 1 # 从0起始 |
| 81 | num_decoder_symbols = max_word_id + 2 # 包括GO |
| 82 | |
| 83 | cell = rnn_cell.BasicLSTMCell(16*max_seq_len, state_is_tuple=True) |
| 84 | |
| 85 | model_outputs, states = seq2seq.embedding_rnn_seq2seq( |
| 86 | encoder_inputs, |
| 87 | decoder_inputs, |
| 88 | cell, |
| 89 | num_encoder_symbols=num_encoder_symbols, |
| 90 | num_decoder_symbols=num_decoder_symbols, |
| 91 | embedding_size=max_word_id, |
| 92 | feed_previous=is_test) |
| 93 | |
| 94 | network = tf.pack(model_outputs, axis=1) |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | targetY = tf.placeholder(shape=[None, max_seq_len], dtype=tf.float32, name="Y") |
| 100 | |
| 101 | network = tflearn.regression( |
| 102 | network, |
| 103 | placeholder=targetY, |
| 104 | optimizer='adam', |
| 105 | learning_rate=learning_rate, |
| 106 | loss=sequence_loss, |
| 107 | metric=accuracy, |
| 108 | name="Y") |
| 109 | |
| 110 | print "begin create DNN model" |
| 111 | model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path=None) |
| 112 | print "create DNN model finish" |
| 113 | return model |
| 114 | |
| 115 | def print_sentence(list, msg): |
| 116 | sentence = msg |