koa73
koa73

Reputation: 881

How to create an empty tf.Variable with None dimension

How to create empty tf.Variable. I try to make this but get errors

def __init__(self):
    super(ConcatLayer, self).__init__(dtype=tf.float64)
    self.total = tf.Variable(shape=tf.TensorShape([None, 3]), dtype=tf.float64)

ValueError: initial_value must be specified.

Which initial_value should I put? Thx

Upvotes: 0

Views: 2077

Answers (2)

koa73
koa73

Reputation: 881

self.total = tf.Variable((np.empty((0, 3), dtype=np.float64)), shape=[None, 3])

Upvotes: 0

thushv89
thushv89

Reputation: 11333

You can do the following (only tested in TF 2.x),

import tensorflow as tf

v = tf.Variable([[0,0,0],[0,0,0]], shape=[None, 3])

As you can see, you must provide an initial value to a tf.Variable. But you can have None dimensions as shown. If you need to change the size of the first dimension (which we defined as None), you can do the following.

v = v.assign([[0,0,0],[0,0,0],[0,0,0]])

Upvotes: 1

Related Questions