I’m experimenting with the Stanford Natural Language Inference Corpus. I want to create a network whose structure is based on the parse trees given in the columns sentence1_parse
and sentence2_parse
, with weights shared between the nodes of the tree. This will convert the sentence to a vector, and I am hoping to train the network so that for a pair of sentences, the vectors produced will be close to parallel if the gold_label
is entailment
, close to antiparallel if it is contradiction
, and close to perpendicular if the gold_label
is neutral
’ My code looks like this so far.
@tf.function
def seminormal(x):
return x/tf.sqrt(1.0+tf.reduce_sum(x**2.0))
class Tree2Vec(object):
def __init__(self,Dictionary):
self.Dictionary = Dictionary
n = len(self.Dictionary)
initialiser = tf.random_normal_initializer()
self.embedding = tf.Variable(initialiser((n,256)))
layer_0_weights = tf.Variable(initialiser((444,768)))
@tf.function
def layer_0(Input):
return tf.nn.silu(tf.Dot(layer_0_weights,Input))
Input = tf.raw_ops.Placeholder(dtype=tf.float32,shape=768)
layer_1_weights = tf.Variable(initialiser((256,444)))
@tf.function
def layer_1(Input):
return seminormal(tf.Dot(layer_1_weights,layer_0(Input)))
self.fn = layer_1(Input)
def __call__(self,node):
level = 0
splits = []
for (i,char) in enumerate(node):
if char == '(':
if level==0:
splits.append(i+1)
level+=1
elif char == ')':
level-=1
if level == 0:
splits.append(i)
tensors = [self.embedding[i]
for i in self.Dictionary.doc2idx(node.split('(')[0].strip().split())]
for i in range(0,len(splits),2):
tensors.append(self(node[splits[i]:splits[i+1]]))
if len(tensors)<3:
tensors.append(tf.convert_to_tensor(numpy.zeros(256)))
return self.fn(tf.concat(tensors,axis=0))
training = pandas.read_csv('/kaggle/input/stanford-natural-language-inference-corpus/snli_1.0_train.csv').fillna('')
dev = pandas.read_csv('/kaggle/input/stanford-natural-language-inference-corpus/snli_1.0_dev.csv').fillna('')
test = pandas.read_csv('/kaggle/input/stanford-natural-language-inference-corpus/snli_1.0_test.csv').fillna('')
dictionary = gensim.corpora.Dictionary(sentence_corpus(read_corpus(training,
dev,
test)))
tree2vec = Tree2Vec(dictionary)
def vectorize(series):
return tf.stack(series.str[1:-1].lower().apply(tree2vec))
left = keras.layers.Input(shape=(1,))
right = keras.layers.Input(shape=(1,))
dotprod = keras.layers.Dot(axes=1)(vectorize(left),vectorize(right))
with tpu_strategy.scope():
#embedding = ConsistencyEmbedding(82,256,len(dictionary))
#model = ConsistencyModel(embedding,82)
model = keras.Model(inputs = [left,right],outputs=[dotprod])
model.compile(optimizer='adamax',
loss=keras.losses.MeanSquaredError(),
metrics=['mse'])
However, I am getting the following error
2023-01-02 16:52:18.814732: W tensorflow/core/distributed_runtime/eager/remote_tensor_handle_data.cc:76] Unable to destroy remote tensor handles. If you are running a tf.function, it usually indicates some op in the graph gets an error: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [768]
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/tmp/ipykernel_20/2348231237.py in <module>
5 dev,
6 test)))
----> 7 tree2vec = Tree2Vec(dictionary)
8
9 def vectorize(series):
/tmp/ipykernel_20/3505782308.py in __init__(self, Dictionary)
14 return tf.nn.silu(tf.Dot(layer_0_weights,Input))
15 Input = tf.raw_ops.Placeholder(dtype=tf.float32,shape=768)
---> 16 layer_1_weights = tf.Variable(initialiser((256,444)))
17 @tf.function
18 def layer_1(Input):
/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/init_ops_v2.py in __call__(self, shape, dtype, **kwargs)
418 shape = kwargs[_PARTITION_SHAPE]
419 return self._random_generator.random_normal(shape, self.mean, self.stddev,
--> 420 dtype)
421
422 def get_config(self):
/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/init_ops_v2.py in random_normal(self, shape, mean, stddev, dtype)
1071 op = random_ops.random_normal
1072 return op(
-> 1073 shape=shape, mean=mean, stddev=stddev, dtype=dtype, seed=self.seed)
1074
1075 def random_uniform(self, shape, minval, maxval, dtype):
/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 """Call target, and fall back on dispatchers if there is a TypeError."""
200 try:
--> 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a
/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/random_ops.py in random_normal(shape, mean, stddev, dtype, seed, name)
93 seed1, seed2 = random_seed.get_seed(seed)
94 rnd = gen_random_ops.random_standard_normal(
---> 95 shape_tensor, dtype, seed=seed1, seed2=seed2)
96 mul = rnd * stddev_tensor
97 value = math_ops.add(mul, mean_tensor, name=name)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_random_ops.py in random_standard_normal(shape, dtype, seed, seed2, name)
635 return _result
636 except _core._NotOkStatusException as e:
--> 637 _ops.raise_from_not_ok_status(e, name)
638 except _core._FallbackException:
639 pass
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
6860 message = e.message + (" name: " + name if name is not None else "")
6861 # pylint: disable=protected-access
-> 6862 six.raise_from(core._status_to_exception(e.code, message), None)
6863 # pylint: enable=protected-access
6864
/opt/conda/lib/python3.7/site-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [768] [Op:RandomStandardNormal]
I have two questions
- How do I resolve this error?
- Is this approach to building the network from the parse trees valid, or is there something I need to revise to make it work?