How to use existing scales (in ndarray format) for tf.layers.dense in python?

I want to use an existing ndarray formatted matrix as starting weight to create a fully connected level using tensorflow.layers.dense

. I'm not sure how to do this. can anyone help? ideally I want to do the following:

weight = np.array([1,2,3],[1,2,3]) # as example
fully_connected = tf.layers.dense(input, hidden_unit, initializer = weight)

      

But I'm not sure if possible I can do it directly.

+3


source to share


1 answer


You need to specify a custom kernel initializer. The docs for tf.layers.dense

don't help explain this, but show that you have the option at least. You can use:

init = np.array([[1, 2, 3], [4, 5, 6]])
x = tf.placeholder(tf.float32, shape=[1, 2])
fc = tf.layers.dense(x, 3, kernel_initializer=tf.constant_initializer(init, dtype=tf.float32))

      

And to make sure it works:



with tf.Session() as sess:
    for v in vars:
        print('{}\n{}'.format(v.name, sess.run(v)))

# dense_7/kernel:0
# [[ 1.  2.  3.]
#  [ 4.  5.  6.]]
# dense_7/bias:0
# [ 0.  0.  0.]

      

Documents for tf.constant_initializer

.

Note that you must provide an input tensor tf.layers.dense

that defines your input shape, thus x

in the above example, and you must provide a second argument that tells it the dimension of your output; 3

in the above. The shape x

and dimension of your output will depend on your problem and what shape your weight matrix should take.

+3


source







All Articles