Import packages


In [1]:
import pandas as pd
import numpy as np
from keras.callbacks import EarlyStopping, ModelCheckpoint
from CFModel import CFModel


Using Theano backend.
Using gpu device 0: GRID K520 (CNMeM is disabled, cuDNN 5006)

In [2]:
CVSC_TRAIN_CSV_FILE = 'fb15k_cvsc_train.csv'
MODEL_WEIGHTS_FILE = 'fb15k_cvsc_weights.h5'
RNG_SEED = 1446557
K_FACTORS = 50

Load CVSC training triples


In [3]:
triples = pd.read_csv(CVSC_TRAIN_CSV_FILE, sep='\t', usecols=['pid', 'rid']).sample(frac=1., random_state=RNG_SEED)

In [4]:
n_pairs = triples['pid'].max() + 1
m_relations = triples['rid'].max() + 1
print n_pairs, 'pairs,', m_relations, 'relations'


2995738 pairs, 26154 relations

Define model


In [5]:
model = CFModel(n_pairs, m_relations, K_FACTORS)
model.compile(loss='mse', optimizer='adamax')
model.load_weights(MODEL_WEIGHTS_FILE)

Train model


In [6]:
callbacks = [EarlyStopping('val_loss', patience=2), ModelCheckpoint(MODEL_WEIGHTS_FILE, save_best_only=True)]
model.fit([triples['pid'].values, triples['rid'].values], np.ones(len(triples)), 
             nb_epoch=10, validation_split=.1, verbose=1, callbacks=callbacks)


Train on 6185264 samples, validate on 687252 samples
Epoch 1/10
6185264/6185264 [==============================] - 16246s - loss: 0.2084 - val_loss: 0.4326
Epoch 2/10
6185264/6185264 [==============================] - 16242s - loss: 0.1481 - val_loss: 0.4043
Epoch 3/10
2881184/6185264 [============>.................] - ETA: 8669s - loss: 0.1086

KeyboardInterruptTraceback (most recent call last)
<ipython-input-6-920371ff722e> in <module>()
      1 callbacks = [EarlyStopping('val_loss', patience=2), ModelCheckpoint(MODEL_WEIGHTS_FILE, save_best_only=True)]
      2 model.fit([triples['pid'].values, triples['rid'].values], np.ones(len(triples)), 
----> 3              nb_epoch=10, validation_split=.1, verbose=1, callbacks=callbacks)

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, **kwargs)
    411                               shuffle=shuffle,
    412                               class_weight=class_weight,
--> 413                               sample_weight=sample_weight)
    414 
    415     def evaluate(self, x, y, batch_size=32, verbose=1,

/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight)
   1080                               verbose=verbose, callbacks=callbacks,
   1081                               val_f=val_f, val_ins=val_ins, shuffle=shuffle,
-> 1082                               callback_metrics=callback_metrics)
   1083 
   1084     def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in _fit_loop(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics)
    799                 batch_logs['size'] = len(batch_ids)
    800                 callbacks.on_batch_begin(batch_index, batch_logs)
--> 801                 outs = f(ins_batch)
    802                 if type(outs) != list:
    803                     outs = [outs]

/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.pyc in __call__(self, inputs)
    529     def __call__(self, inputs):
    530         assert type(inputs) in {list, tuple}
--> 531         return self.function(*inputs)
    532 
    533 

/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    857         t0_fn = time.time()
    858         try:
--> 859             outputs = self.fn()
    860         except Exception:
    861             if hasattr(self.fn, 'position_of_error'):

KeyboardInterrupt: 

In [ ]: