This is a implementation of Neural Tensor Network as described in Reasoning With Neural Tensor Networks for Knowledge Base Completion.
For detailed description please refer to my blog post NEURAL TENSOR NETWORK: EXPLORING RELATIONS AMONG TEXT ENTITIES.
Neural Tensor Layer is defined as
This implementation uses Python 2.7, Keras (2.0 or above) with Theano backend, and Scikit Learn.
Download the data files for wordbase and freebase dataset - Data. Extract the files and keep them in the same folder with the files in this repository (inside the data folder).
>>> import ntn_input
>>> from ntn import *
>>> from ntn_model import *
>>> negative_samples = 1
>>> e1,e2,labels_train,t1,t2,labels_dev,num_relations = prepare_data(negative_samples)
>>> e, t, labels_train, labels_dev = aggregate(e1,e2,labels_train,t1,t2,labels_dev,num_relations)
>>> model = build_model(num_relations, tensor_slices) # tensor_slices = number of tensor parameters
>>> model.fit(e,labels_train,
nb_epoch=10,
batch_size=100,verbose=1)