def save_model_tfjs(model: ks.models.Model, save_loc: str) -> None:
    word_pred_model = ks.models.Model(
        inputs=model.inputs,
        outputs=[model.get_layer('word_preds').output],
    )
    word_pred_model.compile(optimizer='sgd', loss='mse')
    tfjs.converters.save_keras_model(
        word_pred_model,
        save_loc,
    )
Beispiel #2
0
def get_excitations(model: ks.models.Model, num_sentences: int,
                    dataset: Dataset, save_loc: str) -> None:
    conv_len = model.get_layer('convs').get_weights()[0].shape[0]
    conv_output_model = ks.models.Model(
        inputs=model.inputs,
        outputs=[model.get_layer('convs').output],
    )
    preds = conv_output_model.predict(dataset.x_train[:num_sentences])

    def get_preds(idxs: Matrix, preds: Matrix):
        num_padding = np.sum(idxs == 0)
        idxs, preds = idxs[num_padding:], preds[num_padding + 1:]
        words = dataset.decode(idxs)
        return {
            'words': words,
            'preds': [[float(j) for j in i] for i in preds.T],
        }

    excitations = {
        'conv_len':
        conv_len,
        'activations': [
            get_preds(i, p)
            for i, p in zip(dataset.x_train[:num_sentences], preds)
        ],
    }
    utils.save_json(excitations, save_loc, 'excitations.json')

    with open(os.path.join(save_loc, 'excitations_show.txt'), 'w') as f:
        for example in excitations['activations']:
            for i in range(3):
                ordered_sentences = get_ordered_sentences(
                    example['words'],
                    example['preds'][i],
                    conv_len,
                )
                f.write('Layer {}\n'.format(i))
                f.write('\n'.join('{}: {}'.format(*g)
                                  for g in ordered_sentences))
                f.write('\n\n')