import tensorflow as tf
def make_variables(k, initializer):
return (tf.Variable(initializer(shape=[k], dtype=tf.float32)),
tf.Variable(initializer(shape=[k, k], dtype=tf.float32)))
v1, v2 = make_variables(3, tf.constant_initializer(2.))
v1
Out[55]: <tf.Variable 'Variable:0' shape=(3,) dtype=float32, numpy=array([2., 2., 2.], dtype=float32)>
Out[56]:
<tf.Variable 'Variable:0' shape=(3, 3) dtype=float32, numpy=
array([[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]], dtype=float32)>
make_variables(4, tf.random_uniform_initializer(minval=-1., maxval=1.))
(<tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([-0.5988991 , 0.7792051 , 0.5964377 , -0.86411357], dtype=float32)>,
<tf.Variable 'Variable:0' shape=(4, 4) dtype=float32, numpy=
array([[ 0.12324381, -0.5521028 , -0.37995243, -0.530148 ],
[-0.97156 , -0.3974259 , -0.95826817, 0.9413047 ],
[-0.36188293, 0.87448907, -0.8246341 , -0.05807495],
[-0.64107823, 0.38210344, 0.48264408, 0.15233326]],
dtype=float32)>)
value = [0, 1, 2, 3, 4, 5, 6, 7]
init = tf.constant_initializer(value)
# Fitting shape
tf.Variable(init(shape=[2, 4], dtype=tf.float32))
<tf.Variable 'Variable:0' shape=(2, 4) dtype=float32, numpy=
array([[0., 1., 2., 3.],
[4., 5., 6., 7.]], dtype=float32)>